2025-11-09 12:28:27 +01:00
|
|
|
'use client';
|
|
|
|
|
|
|
|
|
|
import * as React from 'react';
|
|
|
|
|
import { Moon, Sun } from 'lucide-react';
|
|
|
|
|
import { Button } from '@/components/ui/Button';
|
|
|
|
|
|
|
|
|
|
export function ThemeToggle() {
|
|
|
|
|
const [theme, setTheme] = React.useState<'light' | 'dark'>('light');
|
|
|
|
|
|
2025-11-09 12:42:40 +01:00
|
|
|
const toggleTheme = React.useCallback(() => {
|
|
|
|
|
const newTheme = theme === 'light' ? 'dark' : 'light';
|
|
|
|
|
setTheme(newTheme);
|
|
|
|
|
localStorage.setItem('theme', newTheme);
|
|
|
|
|
document.documentElement.classList.toggle('dark', newTheme === 'dark');
|
|
|
|
|
}, [theme]);
|
|
|
|
|
|
2025-11-09 12:28:27 +01:00
|
|
|
React.useEffect(() => {
|
2025-11-09 13:34:49 +01:00
|
|
|
// Read the current theme from the DOM (set by blocking script)
|
|
|
|
|
const isDark = document.documentElement.classList.contains('dark');
|
|
|
|
|
setTheme(isDark ? 'dark' : 'light');
|
2025-11-09 12:28:27 +01:00
|
|
|
}, []);
|
|
|
|
|
|
2025-11-09 12:42:40 +01:00
|
|
|
// Keyboard shortcut: Ctrl/Cmd + D
|
|
|
|
|
React.useEffect(() => {
|
|
|
|
|
const handleKeyDown = (e: KeyboardEvent) => {
|
|
|
|
|
if (e.key === 'd' && (e.ctrlKey || e.metaKey)) {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
toggleTheme();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
window.addEventListener('keydown', handleKeyDown);
|
|
|
|
|
return () => window.removeEventListener('keydown', handleKeyDown);
|
|
|
|
|
}, [toggleTheme]);
|
2025-11-09 12:28:27 +01:00
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<Button
|
|
|
|
|
variant="outline"
|
|
|
|
|
size="icon"
|
|
|
|
|
onClick={toggleTheme}
|
|
|
|
|
aria-label="Toggle theme"
|
|
|
|
|
title={`Switch to ${theme === 'light' ? 'dark' : 'light'} mode`}
|
|
|
|
|
>
|
|
|
|
|
{theme === 'light' ? (
|
|
|
|
|
<Moon className="h-4 w-4" />
|
|
|
|
|
) : (
|
|
|
|
|
<Sun className="h-4 w-4" />
|
|
|
|
|
)}
|
|
|
|
|
</Button>
|
|
|
|
|
);
|
|
|
|
|
}
|