返回 CodeWhale
theme-toggle.tsx
根目录 / web / components / theme-toggle.tsx
1 "use client";
2
3 /**
4 * <ThemeToggle> — an icon-only Light / Dark control in the site nav, shown
5 * on every page. The current mode is in its accessible name.
6 *
7 * Paper is the site's appearance (paper above, sea below the horizon), so
8 * the site no longer follows the OS. Dark pins the navy ground through
9 * `data-theme="dark"`; light removes the pin.
10 *
11 * One storage contract, shared with the web app: the `cw-theme` key holds
12 * `light | dark`. A stored `system` or `auto` (earlier modes) reads as
13 * light. The server-inserted boot script applies a stored pin before paint;
14 * locale navigation reapplies that choice from the same storage contract.
15 */
16
17 import { useLayoutEffect, useRef, useState } from "react";
18 import { useServerInsertedHTML } from "next/navigation";
19 import { fill } from "@/lib/i18n/dictionaries";
20 import { Icon, type IconName } from "./icon";
21
22 type Mode = "light" | "dark";
23 const ORDER: Mode[] = ["light", "dark"];
24 const KEY = "cw-theme";
25
26 function load(): Mode {
27 try {
28 const stored = localStorage.getItem(KEY);
29 return stored === "dark" ? "dark" : "light";
30 } catch {
31 return "light";
32 }
33 }
34
35 function apply(mode: Mode) {
36 const el = document.documentElement;
37 if (mode === "light") el.removeAttribute("data-theme");
38 else el.setAttribute("data-theme", mode);
39 try {
40 localStorage.setItem(KEY, mode);
41 } catch {
42 /* private mode / storage disabled — the choice applies until reload */
43 }
44 }
45
46 export function ThemeToggle({
47 lightLabel,
48 darkLabel,
49 ariaTemplate,
50 }: {
51 lightLabel: string;
52 darkLabel: string;
53 /** "Theme: {mode} (click to cycle)" — interpolated with fill(). */
54 ariaTemplate: string;
55 }) {
56 const [mode, setMode] = useState<Mode>("light");
57 const [mounted, setMounted] = useState(false);
58
59 const inserted = useRef(false);
60 useServerInsertedHTML(() => {
61 if (inserted.current) return null;
62 inserted.current = true;
63 return (
64 <script
65 dangerouslySetInnerHTML={{
66 __html:
67 "(function(){try{var t=localStorage.getItem('cw-theme');if(t==='light'||t==='dark'){document.documentElement.setAttribute('data-theme',t);}}catch(e){}})();",
68 }}
69 />
70 );
71 });
72
73 useLayoutEffect(() => {
74 const selected = load();
75 apply(selected);
76 setMode(selected);
77 setMounted(true);
78 }, []);
79
80 const cycle = () => {
81 const next = ORDER[(ORDER.indexOf(mode) + 1) % ORDER.length];
82 setMode(next);
83 apply(next);
84 };
85
86 const labels: Record<Mode, string> = {
87 light: lightLabel,
88 dark: darkLabel,
89 };
90 const glyph: Record<Mode, IconName> = { light: "sun", dark: "moon" };
91 const shown = mounted ? mode : "light";
92
93 return (
94 <button
95 type="button"
96 onClick={cycle}
97 className="nav-icon-button"
98 aria-label={fill(ariaTemplate, { mode: labels[shown] })}
99 suppressHydrationWarning
100 >
101 <Icon name={glyph[shown]} className="nav-icon" />
102 </button>
103 );
104 }
105
105 lines Plain Text