Compare commits
1 Commits
v0.0.2-dev
...
v0.0.3-dev
| Author | SHA1 | Date | |
|---|---|---|---|
| 15b138cdd7 |
+16
-10
@@ -8,7 +8,8 @@ import { useEffect, useSyncExternalStore } from "react";
|
|||||||
* sound store (module 23525):
|
* sound store (module 23525):
|
||||||
*
|
*
|
||||||
* - theme ∈ {light, dark, system}, persisted to localStorage "theme"
|
* - theme ∈ {light, dark, system}, persisted to localStorage "theme"
|
||||||
* (original key, kept verbatim for parity); default "light".
|
* (original key, kept verbatim for parity); default "system" — follow the
|
||||||
|
* device (auto), per product decision (original defaulted to "light").
|
||||||
* - resolvedTheme = theme === "system"
|
* - resolvedTheme = theme === "system"
|
||||||
* ? (matchMedia("(prefers-color-scheme: dark)").matches ? dark : light)
|
* ? (matchMedia("(prefers-color-scheme: dark)").matches ? dark : light)
|
||||||
* : theme;
|
* : theme;
|
||||||
@@ -16,7 +17,8 @@ import { useEffect, useSyncExternalStore } from "react";
|
|||||||
* CSS tokens flip via the .dark block in globals.css.
|
* CSS tokens flip via the .dark block in globals.css.
|
||||||
* - "system" follows OS changes live (original matchMedia change listener).
|
* - "system" follows OS changes live (original matchMedia change listener).
|
||||||
* - soundEnabled persisted to localStorage "sound" ("on"/"off", original
|
* - soundEnabled persisted to localStorage "sound" ("on"/"off", original
|
||||||
* keys).
|
* keys); default ON — BGM starts (autoplay policies defer actual playback
|
||||||
|
* to the first user gesture, handled in useBgm's recovery effect).
|
||||||
*
|
*
|
||||||
* Implemented as a module-singleton external store (useSyncExternalStore)
|
* Implemented as a module-singleton external store (useSyncExternalStore)
|
||||||
* instead of the original context: the header and the canvas components
|
* instead of the original context: the header and the canvas components
|
||||||
@@ -38,9 +40,9 @@ type ShellMediaState = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
let state: ShellMediaState = {
|
let state: ShellMediaState = {
|
||||||
theme: "light",
|
theme: "system",
|
||||||
resolvedTheme: "light",
|
resolvedTheme: "light",
|
||||||
soundEnabled: false,
|
soundEnabled: true,
|
||||||
};
|
};
|
||||||
|
|
||||||
const listeners = new Set<() => void>();
|
const listeners = new Set<() => void>();
|
||||||
@@ -90,16 +92,18 @@ function hydrate() {
|
|||||||
if (hydrated || typeof window === "undefined") return;
|
if (hydrated || typeof window === "undefined") return;
|
||||||
hydrated = true;
|
hydrated = true;
|
||||||
const stored = readStored(THEME_KEY);
|
const stored = readStored(THEME_KEY);
|
||||||
|
// 无有效存储 → 默认 system(自动跟随设备)
|
||||||
const theme: ThemeSetting = SETTINGS.includes(stored as ThemeSetting)
|
const theme: ThemeSetting = SETTINGS.includes(stored as ThemeSetting)
|
||||||
? (stored as ThemeSetting)
|
? (stored as ThemeSetting)
|
||||||
: "light";
|
: "system";
|
||||||
mediaQuery = window.matchMedia("(prefers-color-scheme: dark)");
|
mediaQuery = window.matchMedia("(prefers-color-scheme: dark)");
|
||||||
mediaQuery.addEventListener("change", onSystemChange);
|
mediaQuery.addEventListener("change", onSystemChange);
|
||||||
state = { ...state, theme, resolvedTheme: resolve(theme) };
|
state = { ...state, theme, resolvedTheme: resolve(theme) };
|
||||||
applyDocument(theme);
|
applyDocument(theme);
|
||||||
state = {
|
state = {
|
||||||
...state,
|
...state,
|
||||||
soundEnabled: readStored(SOUND_KEY) === "on",
|
// 无有效存储 → 默认开启 BGM;显式 "off" 才关闭
|
||||||
|
soundEnabled: readStored(SOUND_KEY) !== "off",
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -142,12 +146,14 @@ function subscribe(listener: () => void) {
|
|||||||
const getSnapshot = () => state;
|
const getSnapshot = () => state;
|
||||||
|
|
||||||
// Hydration-safe SSR snapshot: must equal what the server rendered
|
// Hydration-safe SSR snapshot: must equal what the server rendered
|
||||||
// (light/paused defaults). React uses this for the hydration render, then
|
// (system/light defaults per product decision; sound defaults ON — the
|
||||||
// re-renders with the live snapshot once the client store hydrates.
|
// audio only starts after the first user gesture, see useBgm). React uses
|
||||||
|
// this for the hydration render, then re-renders with the live snapshot
|
||||||
|
// once the client store hydrates.
|
||||||
const SERVER_SNAPSHOT: ShellMediaState = {
|
const SERVER_SNAPSHOT: ShellMediaState = {
|
||||||
theme: "light",
|
theme: "system",
|
||||||
resolvedTheme: "light",
|
resolvedTheme: "light",
|
||||||
soundEnabled: false,
|
soundEnabled: true,
|
||||||
};
|
};
|
||||||
|
|
||||||
function getServerSnapshot(): ShellMediaState {
|
function getServerSnapshot(): ShellMediaState {
|
||||||
|
|||||||
Reference in New Issue
Block a user