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 { useTheme } from '@/components/theme-provider'
|
||||||
import { Button } from '@/components/ui/button'
|
import { Button } from '@/components/ui/button'
|
||||||
import {
|
import { cn } from '@/lib/utils'
|
||||||
DropdownMenu,
|
|
||||||
DropdownMenuContent,
|
|
||||||
DropdownMenuItem,
|
|
||||||
DropdownMenuTrigger,
|
|
||||||
} from '@/components/ui/dropdown-menu'
|
|
||||||
|
|
||||||
const OPTIONS = [
|
const OPTIONS = [
|
||||||
{ value: 'light', label: 'Claro', icon: SunIcon },
|
{ value: 'light', label: 'Claro', icon: SunIcon, out: '-rotate-90' },
|
||||||
{ value: 'dark', label: 'Oscuro', icon: MoonIcon },
|
{ value: 'dark', label: 'Oscuro', icon: MoonIcon, out: 'rotate-90' },
|
||||||
{ value: 'system', label: 'Sistema', icon: MonitorIcon },
|
{ value: 'system', label: 'Sistema', icon: MonitorIcon, out: 'rotate-90' },
|
||||||
] as const
|
] 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() {
|
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 (
|
return (
|
||||||
<DropdownMenu>
|
<Button
|
||||||
<DropdownMenuTrigger asChild>
|
variant="ghost"
|
||||||
<Button variant="ghost" size="icon" aria-label="Cambiar tema">
|
size="icon"
|
||||||
<SunIcon className="size-4 scale-100 rotate-0 transition-all dark:scale-0 dark:-rotate-90" />
|
className="relative"
|
||||||
<MoonIcon className="absolute size-4 scale-0 rotate-90 transition-all dark:scale-100 dark:rotate-0" />
|
aria-label={`Tema: ${current.label} — cambiar a ${next.label}`}
|
||||||
</Button>
|
onClick={() => setTheme(next.value)}
|
||||||
</DropdownMenuTrigger>
|
>
|
||||||
<DropdownMenuContent align="end">
|
{OPTIONS.map(({ value, icon: Icon, out }) => (
|
||||||
{OPTIONS.map(({ value, label, icon: Icon }) => (
|
<Icon
|
||||||
<DropdownMenuItem key={value} onClick={() => setTheme(value)}>
|
key={value}
|
||||||
<Icon />
|
aria-hidden="true"
|
||||||
{label}
|
className={cn(
|
||||||
</DropdownMenuItem>
|
'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