Template
30 lines
1.1 KiB
TypeScript
30 lines
1.1 KiB
TypeScript
"use client"
|
|
|
|
import { RiMoonLine, RiSunLine } from "@remixicon/react"
|
|
import { useTheme } from "next-themes"
|
|
import { useSyncExternalStore } from "react"
|
|
|
|
const emptySubscribe = () => () => {}
|
|
|
|
export function ThemeToggle() {
|
|
const mounted = useSyncExternalStore(emptySubscribe, () => true, () => false)
|
|
const { resolvedTheme, setTheme } = useTheme()
|
|
|
|
const isDark = mounted && resolvedTheme === "dark"
|
|
|
|
return (
|
|
<button
|
|
type="button"
|
|
aria-label={mounted ? `Switch to ${isDark ? "light" : "dark"} theme` : "Toggle theme"}
|
|
className="inline-flex size-9 items-center justify-center rounded-sm border bg-background/80 text-muted-foreground transition-colors hover:border-foreground/20 hover:text-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-blue-500/60"
|
|
onClick={() => setTheme(isDark ? "light" : "dark")}
|
|
>
|
|
{mounted ? (
|
|
isDark ? <RiSunLine className="size-4" /> : <RiMoonLine className="size-4" />
|
|
) : (
|
|
<span className="size-4" aria-hidden="true" />
|
|
)}
|
|
</button>
|
|
)
|
|
}
|