feat: cycle the theme from the header toggle instead of a dropdown
The theme button now advances light -> dark -> system on every click, keeping the sun/moon style animation driven by the selected value so the system option animates too.
This commit is contained in:
@@ -2,38 +2,45 @@ import { MonitorIcon, MoonIcon, SunIcon } from 'lucide-react'
|
||||
|
||||
import { useTheme } from '@/components/theme-provider'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import {
|
||||
DropdownMenu,
|
||||
DropdownMenuContent,
|
||||
DropdownMenuItem,
|
||||
DropdownMenuTrigger,
|
||||
} from '@/components/ui/dropdown-menu'
|
||||
import { cn } from '@/lib/utils'
|
||||
|
||||
const OPTIONS = [
|
||||
{ value: 'light', label: 'Claro', icon: SunIcon },
|
||||
{ value: 'dark', label: 'Oscuro', icon: MoonIcon },
|
||||
{ value: 'system', label: 'Sistema', icon: MonitorIcon },
|
||||
{ value: 'light', label: 'Claro', icon: SunIcon, out: '-rotate-90' },
|
||||
{ value: 'dark', label: 'Oscuro', icon: MoonIcon, out: 'rotate-90' },
|
||||
{ value: 'system', label: 'Sistema', icon: MonitorIcon, out: 'rotate-90' },
|
||||
] as const
|
||||
|
||||
type Option = (typeof OPTIONS)[number]
|
||||
|
||||
function nextOption(current: Option['value']): Option {
|
||||
const index = OPTIONS.findIndex((option) => option.value === current)
|
||||
return OPTIONS[(index + 1) % OPTIONS.length]
|
||||
}
|
||||
|
||||
export function ThemeToggle() {
|
||||
const { setTheme } = useTheme()
|
||||
const { theme, setTheme } = useTheme()
|
||||
|
||||
const current = OPTIONS.find((option) => option.value === theme) ?? OPTIONS[2]
|
||||
const next = nextOption(current.value)
|
||||
|
||||
return (
|
||||
<DropdownMenu>
|
||||
<DropdownMenuTrigger asChild>
|
||||
<Button variant="ghost" size="icon" aria-label="Cambiar tema">
|
||||
<SunIcon className="size-4 scale-100 rotate-0 transition-all dark:scale-0 dark:-rotate-90" />
|
||||
<MoonIcon className="absolute size-4 scale-0 rotate-90 transition-all dark:scale-100 dark:rotate-0" />
|
||||
</Button>
|
||||
</DropdownMenuTrigger>
|
||||
<DropdownMenuContent align="end">
|
||||
{OPTIONS.map(({ value, label, icon: Icon }) => (
|
||||
<DropdownMenuItem key={value} onClick={() => setTheme(value)}>
|
||||
<Icon />
|
||||
{label}
|
||||
</DropdownMenuItem>
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="icon"
|
||||
className="relative"
|
||||
aria-label={`Tema: ${current.label} — cambiar a ${next.label}`}
|
||||
onClick={() => setTheme(next.value)}
|
||||
>
|
||||
{OPTIONS.map(({ value, icon: Icon, out }) => (
|
||||
<Icon
|
||||
key={value}
|
||||
aria-hidden="true"
|
||||
className={cn(
|
||||
'absolute size-4 transition-all',
|
||||
value === current.value ? 'scale-100 rotate-0' : `scale-0 ${out}`,
|
||||
)}
|
||||
/>
|
||||
))}
|
||||
</DropdownMenuContent>
|
||||
</DropdownMenu>
|
||||
</Button>
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user