mirror of
https://github.com/ANewProfile/breadcrumbs.git
synced 2026-09-23 07:19:56 +02:00
68 lines
1.8 KiB
TypeScript
68 lines
1.8 KiB
TypeScript
"use client";
|
|
|
|
import { useEffect, useState } from "react";
|
|
|
|
type Theme = "light" | "dark";
|
|
|
|
const STORAGE_KEY = "breadcrumbs_theme";
|
|
|
|
function applyTheme(theme: Theme) {
|
|
document.documentElement.classList.toggle("dark", theme === "dark");
|
|
window.localStorage.setItem(STORAGE_KEY, theme);
|
|
}
|
|
|
|
function SunIcon() {
|
|
return (
|
|
<svg viewBox="0 0 24 24" fill="none" className="w-5 h-5" aria-hidden="true">
|
|
<circle cx="12" cy="12" r="4" stroke="currentColor" strokeWidth="1.6" />
|
|
<path
|
|
d="M12 2.5v2.2M12 19.3v2.2M4.2 4.2l1.6 1.6M18.2 18.2l1.6 1.6M2.5 12h2.2M19.3 12h2.2M4.2 19.8l1.6-1.6M18.2 5.8l1.6-1.6"
|
|
stroke="currentColor"
|
|
strokeWidth="1.6"
|
|
strokeLinecap="round"
|
|
/>
|
|
</svg>
|
|
);
|
|
}
|
|
|
|
function MoonIcon() {
|
|
return (
|
|
<svg viewBox="0 0 24 24" fill="none" className="w-5 h-5" aria-hidden="true">
|
|
<path
|
|
d="M20 14.3A8.5 8.5 0 1 1 9.7 4a7 7 0 0 0 10.3 10.3Z"
|
|
fill="currentColor"
|
|
/>
|
|
</svg>
|
|
);
|
|
}
|
|
|
|
export function ThemeToggle() {
|
|
const [theme, setTheme] = useState<Theme | null>(null);
|
|
|
|
useEffect(() => {
|
|
setTheme(document.documentElement.classList.contains("dark") ? "dark" : "light");
|
|
}, []);
|
|
|
|
function toggle() {
|
|
const next = theme === "dark" ? "light" : "dark";
|
|
setTheme(next);
|
|
applyTheme(next);
|
|
}
|
|
|
|
// Avoid rendering with a guessed theme before we've read the real one from the DOM.
|
|
if (theme === null) {
|
|
return <div className="w-9 h-9" />;
|
|
}
|
|
|
|
return (
|
|
<button
|
|
type="button"
|
|
onClick={toggle}
|
|
aria-label={theme === "dark" ? "Switch to light mode" : "Switch to dark mode"}
|
|
className="p-2 rounded-lg text-zinc-500 hover:text-zinc-700 hover:bg-zinc-100 dark:text-zinc-400 dark:hover:text-zinc-200 dark:hover:bg-zinc-800 transition-colors"
|
|
>
|
|
{theme === "dark" ? <MoonIcon /> : <SunIcon />}
|
|
</button>
|
|
);
|
|
}
|