forked from cardosofelipe/fast-next-template
Add theme toggle with light, dark, and system support
- **Header:** Integrate `ThemeToggle` component into the user menu area. - **Theme Provider:** Introduce `ThemeProvider` context for managing and persisting theme preferences. - **New Components:** Add `ThemeToggle` for switching themes and `ThemeProvider` to handle state and system preferences. - Ensure responsive updates and localStorage persistence for user-selected themes.
This commit is contained in:
51
frontend/src/components/theme/ThemeToggle.tsx
Normal file
51
frontend/src/components/theme/ThemeToggle.tsx
Normal file
@@ -0,0 +1,51 @@
|
||||
/**
|
||||
* Theme Toggle Button
|
||||
* Switches between light, dark, and system themes
|
||||
*/
|
||||
|
||||
'use client';
|
||||
|
||||
import { Moon, Sun, Monitor } from 'lucide-react';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import {
|
||||
DropdownMenu,
|
||||
DropdownMenuContent,
|
||||
DropdownMenuItem,
|
||||
DropdownMenuTrigger,
|
||||
} from '@/components/ui/dropdown-menu';
|
||||
import { useTheme } from './ThemeProvider';
|
||||
|
||||
export function ThemeToggle() {
|
||||
const { theme, setTheme, resolvedTheme } = useTheme();
|
||||
|
||||
return (
|
||||
<DropdownMenu>
|
||||
<DropdownMenuTrigger asChild>
|
||||
<Button variant="ghost" size="icon" aria-label="Toggle theme">
|
||||
{resolvedTheme === 'dark' ? (
|
||||
<Moon className="h-5 w-5" />
|
||||
) : (
|
||||
<Sun className="h-5 w-5" />
|
||||
)}
|
||||
</Button>
|
||||
</DropdownMenuTrigger>
|
||||
<DropdownMenuContent align="end">
|
||||
<DropdownMenuItem onClick={() => setTheme('light')}>
|
||||
<Sun className="mr-2 h-4 w-4" />
|
||||
Light
|
||||
{theme === 'light' && <span className="ml-auto">✓</span>}
|
||||
</DropdownMenuItem>
|
||||
<DropdownMenuItem onClick={() => setTheme('dark')}>
|
||||
<Moon className="mr-2 h-4 w-4" />
|
||||
Dark
|
||||
{theme === 'dark' && <span className="ml-auto">✓</span>}
|
||||
</DropdownMenuItem>
|
||||
<DropdownMenuItem onClick={() => setTheme('system')}>
|
||||
<Monitor className="mr-2 h-4 w-4" />
|
||||
System
|
||||
{theme === 'system' && <span className="ml-auto">✓</span>}
|
||||
</DropdownMenuItem>
|
||||
</DropdownMenuContent>
|
||||
</DropdownMenu>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user