| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- import { useEffect, useState, useCallback } from "react";
- export type Theme = "light" | "dark" | "zai-light" | "zai-dark" | "system";
- export type ResolvedTheme = "light" | "dark";
- const STORAGE_KEY = "zcode-theme";
- const BROWSER_THEME_SURFACE_ATTRIBUTE = "data-zcode-browser-theme-surface";
- function getSystemTheme(): ResolvedTheme {
- return window.matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light";
- }
- export function resolveTheme(theme: Theme): ResolvedTheme {
- if (theme === "system") {
- return getSystemTheme();
- }
- return theme === "dark" || theme === "zai-dark" ? "dark" : "light";
- }
- export function normalizeThemePreference(theme: Theme): Theme {
- if (theme === "dark") return "zai-dark";
- if (theme === "light") return "zai-light";
- return theme;
- }
- function setThemeMetaContent(name: "theme-color" | "color-scheme", content: string) {
- let meta = document.querySelector<HTMLMetaElement>(`meta[name="${name}"]`);
- if (!meta) {
- meta = document.createElement("meta");
- meta.name = name;
- document.head.append(meta);
- }
- meta.content = content;
- }
- function syncBrowserThemeSurface(resolved: ResolvedTheme) {
- const root = document.documentElement;
- if (
- typeof root.hasAttribute !== "function" ||
- !root.hasAttribute(BROWSER_THEME_SURFACE_ATTRIBUTE)
- ) {
- return;
- }
- // Electron 为 vibrancy 保持透明根背景,但普通浏览器需要从文档根和标准 meta
- // 获得页面主题。只切换 React 的 dark class 会让浏览器工具栏、原生控件和 overscroll 留在旧主题。
- root.setAttribute(BROWSER_THEME_SURFACE_ATTRIBUTE, resolved);
- root.style.colorScheme = resolved;
- setThemeMetaContent("color-scheme", resolved);
- const background = getComputedStyle(root).getPropertyValue("--color-background").trim();
- if (background) {
- setThemeMetaContent("theme-color", background);
- }
- }
- export function applyTheme(theme: Theme) {
- const resolved = resolveTheme(theme);
- const appliedTheme =
- theme === "system"
- ? resolved === "dark"
- ? "zai-dark"
- : "zai-light"
- : normalizeThemePreference(theme);
- document.documentElement.classList.toggle("dark", resolved === "dark");
- document.documentElement.classList.toggle("theme-zai-light", appliedTheme === "zai-light");
- document.documentElement.classList.toggle("theme-zai-dark", appliedTheme === "zai-dark");
- syncBrowserThemeSurface(resolved);
- }
- function isTheme(value: string | null): value is Theme {
- return (
- value === "light" ||
- value === "dark" ||
- value === "zai-light" ||
- value === "zai-dark" ||
- value === "system"
- );
- }
- export function useTheme() {
- const [theme, setThemeState] = useState<Theme>(() => {
- const saved = localStorage.getItem(STORAGE_KEY);
- // 默认主题统一收敛到 Zai dark,避免旧 hook 兜底值和 Zustand store 默认值分叉。
- return isTheme(saved) ? normalizeThemePreference(saved) : "zai-dark";
- });
- const setTheme = useCallback((t: Theme) => {
- const normalizedTheme = normalizeThemePreference(t);
- localStorage.setItem(STORAGE_KEY, normalizedTheme);
- setThemeState(normalizedTheme);
- applyTheme(normalizedTheme);
- }, []);
- // 初始化 + system 模式下监听系统偏好变化
- useEffect(() => {
- applyTheme(theme);
- if (theme !== "system") return;
- const mq = window.matchMedia("(prefers-color-scheme: dark)");
- const handler = () => applyTheme("system");
- mq.addEventListener("change", handler);
- return () => mq.removeEventListener("change", handler);
- }, [theme]);
- return { theme, setTheme } as const;
- }
|