Default to dark mode outside 6:30-21:00 based on the visitor's clock

Only applies as the initial value when no theme preference is stored
yet - a manual toggle still sticks afterward, same precedent as the
browser-language auto-detection.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
Maaxxs 2026-07-07 02:17:48 +02:00
parent 24df7a602c
commit 6392c77de4

View file

@ -2,11 +2,20 @@ import { createContext, useContext, useEffect, useState } from 'react';
const ThemeContext = createContext(null);
// Hellmodus 6:30-21:00 Uhr, sonst Dunkelmodus - gilt nur als Startwert, solange noch
// keine manuelle Wahl gespeichert ist (die manuelle Umschaltung bleibt danach bestehen,
// genau wie bei der Spracherkennung).
function detectDefaultTheme() {
const now = new Date();
const hour = now.getHours() + now.getMinutes() / 60;
return hour >= 6.5 && hour < 21 ? 'light' : 'dark';
}
export function ThemeProvider({ children }) {
const [theme, setTheme] = useState(() => {
const stored = localStorage.getItem('hifi-theme');
if (stored) return stored;
return 'light';
if (stored === 'light' || stored === 'dark') return stored;
return detectDefaultTheme();
});
useEffect(() => {