示例工程 22-theming-and-native-appearance

22-theming-and-native-appearance/app/.tsc/src/App.js

javascript · 13 KB

import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { useCallback, useEffect, useState } from "react";
const SOURCES = [
    { id: "system", label: "跟随系统" },
    { id: "light", label: "浅色" },
    { id: "dark", label: "深色" },
];
const tokens = [
    { name: "surface", light: "#ffffff", dark: "#0f172a" },
    { name: "panel", light: "#f8fafc", dark: "#1e293b" },
    { name: "border", light: "#e2e8f0", dark: "#334155" },
    { name: "text", light: "#0f172a", dark: "#e2e8f0" },
    { name: "muted", light: "#64748b", dark: "#94a3b8" },
    { name: "primary", light: "#2563eb", dark: "#60a5fa" },
];
function initialTheme() {
    return {
        source: "system",
        shouldUseDarkColors: document.documentElement.classList.contains("dark"),
        platform: "unknown",
    };
}
export default function App() {
    const [state, setState] = useState(initialTheme);
    const [showRegions, setShowRegions] = useState(false);
    const [busy, setBusy] = useState(false);
    const [mediaDark, setMediaDark] = useState(() => window.matchMedia("(prefers-color-scheme: dark)").matches);
    useEffect(() => {
        document.documentElement.classList.toggle("dark", state.shouldUseDarkColors);
    }, [state.shouldUseDarkColors]);
    useEffect(() => {
        const query = window.matchMedia("(prefers-color-scheme: dark)");
        const onChange = (event) => {
            setMediaDark(event.matches);
        };
        query.addEventListener("change", onChange);
        return () => query.removeEventListener("change", onChange);
    }, []);
    useEffect(() => {
        let alive = true;
        const unsubscribe = window.api.theme.onChange((next) => {
            if (alive)
                setState(next);
        });
        void window.api.theme.get().then((next) => {
            if (alive)
                setState(next);
        });
        return () => {
            alive = false;
            unsubscribe();
        };
    }, []);
    const setSource = useCallback(async (source) => {
        setBusy(true);
        try {
            const next = await window.api.theme.set(source);
            setState(next);
        }
        finally {
            setBusy(false);
        }
    }, []);
    useEffect(() => {
        window.__setTheme = async (source) => {
            const next = await window.api.theme.set(source);
            setState(next);
        };
        window.__setRegions = (value) => setShowRegions(value);
        window.__setSource = (source) => {
            void setSource(source);
        };
        window.__smoke = async () => {
            const wait = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
            const surface = () => getComputedStyle(document.documentElement)
                .getPropertyValue("--color-surface")
                .trim();
            const before = await window.api.theme.get();
            const dark = await window.api.theme.set("dark");
            setState(dark);
            await wait(180);
            const darkSurface = surface();
            const darkApplied = document.documentElement.classList.contains("dark") &&
                dark.shouldUseDarkColors === true;
            const light = await window.api.theme.set("light");
            setState(light);
            await wait(180);
            const lightSurface = surface();
            const lightApplied = !document.documentElement.classList.contains("dark") &&
                light.shouldUseDarkColors === false;
            const system = await window.api.theme.set("system");
            setState(system);
            await wait(180);
            return {
                initialSource: before.source,
                darkApplied,
                lightApplied,
                systemSource: system.source,
                darkSurface,
                lightSurface,
                mediaDark,
                platform: system.platform,
            };
        };
    }, [mediaDark, setSource]);
    const activeBadge = state.shouldUseDarkColors ? "深色生效" : "浅色生效";
    return (_jsxs("div", { className: `min-h-screen bg-surface text-text ${showRegions ? "show-regions" : ""}`, "data-theme": state.shouldUseDarkColors ? "dark" : "light", children: [_jsxs("header", { className: "titlebar flex h-14 items-center justify-between border-b border-border pl-[92px] pr-4", "data-testid": "titlebar", children: [_jsxs("div", { className: "flex items-center gap-3", children: [_jsx("span", { className: "text-sm font-semibold", children: "\u4E3B\u9898\u4E0E\u539F\u751F\u5916\u89C2" }), _jsx("span", { className: "rounded-full border border-border px-2 py-0.5 text-[11px] text-muted", children: state.platform === "darwin" ? "hiddenInset" : "default" }), _jsx("span", { className: "text-[11px] text-muted", "data-testid": "active-badge", children: activeBadge })] }), _jsx("div", { className: "no-drag flex items-center gap-1 rounded-full border border-border p-0.5", children: SOURCES.map((item) => (_jsx("button", { className: `rounded-full px-3 py-1 text-xs ${state.source === item.id
                                ? "bg-primary text-primary-foreground"
                                : "text-muted hover:text-text"}`, "aria-pressed": state.source === item.id, "data-testid": `theme-${item.id}`, disabled: busy, onClick: () => void setSource(item.id), children: item.label }, item.id))) })] }), _jsxs("div", { className: "flex flex-wrap items-center gap-3 border-b border-border px-5 py-2 text-[11px] text-muted", children: [_jsxs("code", { "data-testid": "current-source", children: ["source=", state.source] }), _jsxs("code", { "data-testid": "should-dark", children: ["shouldUseDarkColors=", String(state.shouldUseDarkColors)] }), _jsxs("code", { "data-testid": "system-pref", children: ["prefersDark=", String(mediaDark)] }), _jsxs("code", { "data-testid": "platform", children: ["platform=", state.platform] }), _jsxs("span", { className: "ml-auto flex items-center gap-2", children: [_jsx("span", { children: "userData/theme.json" }), _jsx("button", { className: "no-drag rounded border border-border px-2 py-1", "data-testid": "regions-toggle", onClick: () => setShowRegions((value) => !value), children: showRegions ? "隐藏拖拽区" : "显示拖拽区" })] })] }), _jsxs("div", { className: "grid gap-4 p-5 lg:grid-cols-[minmax(0,1.05fr)_minmax(0,0.95fr)]", children: [_jsxs("div", { className: "flex flex-col gap-4", children: [_jsxs("section", { className: "rounded-card border border-border", children: [_jsxs("div", { className: "flex items-center justify-between border-b border-border px-4 py-3", children: [_jsx("h1", { className: "text-sm font-semibold", children: "\u4E3B\u9898\u4EE4\u724C" }), _jsx("span", { className: "text-[11px] text-muted", children: "CSS \u53D8\u91CF \u00B7 @theme + .dark" })] }), _jsxs("div", { className: "p-4", children: [_jsx("div", { className: "swatch-grid", children: tokens.map((token) => (_jsxs("div", { className: "rounded border border-border p-2", "data-testid": `swatch-${token.name}`, children: [_jsx("div", { className: "h-8 rounded", style: {
                                                                backgroundColor: state.shouldUseDarkColors
                                                                    ? token.dark
                                                                    : token.light,
                                                            } }), _jsx("div", { className: "mt-1 font-mono text-[11px]", children: token.name }), _jsx("div", { className: "font-mono text-[10px] text-muted", children: state.shouldUseDarkColors ? token.dark : token.light })] }, token.name))) }), _jsxs("p", { className: "mt-3 text-xs text-muted", "data-testid": "surface-value", children: ["\u5F53\u524D surface\uFF1A", state.shouldUseDarkColors ? "#0f172a" : "#ffffff"] })] })] }), _jsxs("section", { className: "rounded-card border border-border", children: [_jsx("div", { className: "border-b border-border px-4 py-3", children: _jsx("h2", { className: "text-sm font-semibold", children: "\u7EC4\u4EF6\u9884\u89C8" }) }), _jsxs("div", { className: "flex flex-col gap-3 p-4 text-sm", children: [_jsxs("div", { className: "flex flex-wrap items-center gap-2", children: [_jsx("button", { className: "rounded bg-primary px-3 py-1.5 text-primary-foreground", children: "\u4E3B\u8981\u64CD\u4F5C" }), _jsx("button", { className: "rounded border border-border px-3 py-1.5", children: "\u6B21\u8981\u64CD\u4F5C" }), _jsx("span", { className: "rounded-full bg-primary-soft px-2 py-0.5 text-[11px] text-primary", children: "\u72B6\u6001\u5FBD\u7AE0" })] }), _jsx("input", { className: "w-full rounded border border-border bg-panel px-3 py-2 text-sm outline-none focus:ring-2 focus:ring-ring", placeholder: "\u8F93\u5165\u6846\u5728\u6697\u8272\u4E0B\u4E5F\u8981\u53EF\u8BFB", "data-testid": "preview-input" }), _jsxs("div", { className: "rounded-card border border-border bg-panel p-3", children: [_jsx("div", { className: "text-xs text-muted", children: "\u5BF9\u8BDD\u6846\u9884\u89C8" }), _jsx("p", { className: "mt-1 text-sm", children: "\u4E3B\u9898\u5207\u6362\u540E\uFF0C\u8FB9\u6846\u3001\u9762\u677F\u4E0E\u6B63\u6587\u90FD\u5E94\u540C\u6B65\u53D8\u5316\uFF0C\u800C\u4E0D\u662F\u53EA\u6539\u80CC\u666F\u3002" })] })] })] })] }), _jsxs("div", { className: "flex flex-col gap-4", children: [_jsxs("section", { className: "rounded-card border border-border", children: [_jsx("div", { className: "border-b border-border px-4 py-3", children: _jsx("h2", { className: "text-sm font-semibold", children: "\u539F\u751F\u5916\u89C2" }) }), _jsx("dl", { className: "divide-y divide-border text-xs", children: [
                                            [
                                                "titleBarStyle",
                                                state.platform === "darwin" ? "hiddenInset" : "default",
                                            ],
                                            ["trafficLightPosition", "{ x: 18, y: 20 }"],
                                            ["vibrancy", "未启用(保持截图不透明)"],
                                            ["backgroundMaterial", "仅 Windows 11 生效"],
                                            [
                                                "建议窗口底色",
                                                state.shouldUseDarkColors ? "#0f172a" : "#ffffff",
                                            ],
                                        ].map(([label, value]) => (_jsxs("div", { className: "flex justify-between gap-3 px-4 py-2", children: [_jsx("dt", { className: "text-muted", children: label }), _jsx("dd", { className: "text-right font-mono", children: value })] }, label))) })] }), _jsxs("section", { className: "rounded-card border border-border", children: [_jsx("div", { className: "border-b border-border px-4 py-3", children: _jsx("h2", { className: "text-sm font-semibold", children: "\u4E3B\u9898\u6765\u6E90" }) }), _jsxs("div", { className: "flex flex-col gap-2 p-4 text-xs", children: [SOURCES.map((item) => (_jsxs("button", { className: `flex items-center justify-between rounded border px-3 py-2 text-left ${state.source === item.id
                                                    ? "border-primary bg-primary-soft text-primary"
                                                    : "border-border bg-panel hover:text-text"}`, onClick: () => void setSource(item.id), "data-testid": `source-row-${item.id}`, children: [_jsx("span", { children: item.label }), _jsx("code", { children: item.id })] }, item.id))), _jsx("p", { className: "mt-1 text-muted", children: "`system` \u8DDF\u968F `nativeTheme`\uFF1B`light` / `dark` \u4F1A\u8986\u76D6\u7CFB\u7EDF\u5E76\u5199\u5165 userData\uFF0C\u4E0B\u4E00\u6B21\u542F\u52A8\u9996\u5E27\u76F4\u63A5\u4F7F\u7528\u3002" })] })] }), _jsxs("section", { className: "rounded-card border border-border", children: [_jsx("div", { className: "border-b border-border px-4 py-3", children: _jsx("h2", { className: "text-sm font-semibold", children: "\u542F\u52A8\u65F6\u5E8F" }) }), _jsxs("ol", { className: "flex list-decimal flex-col gap-1 p-4 pl-8 text-xs text-muted", children: [_jsx("li", { children: "\u4E3B\u8FDB\u7A0B\u8BFB\u53D6 theme.json\uFF0C\u8BBE\u7F6E nativeTheme.themeSource" }), _jsx("li", { children: "\u521B\u5EFA\u7A97\u53E3\uFF0C\u6309 shouldUseDarkColors \u8BBE backgroundColor" }), _jsx("li", { children: "\u52A0\u8F7D\u9875\u9762\u65F6\u9644\u52A0 ?theme=light|dark" }), _jsx("li", { children: "head \u5185\u8054\u811A\u672C\u5728\u9996\u5E27\u524D\u7ED9 html \u52A0 .dark" }), _jsx("li", { children: "React \u6302\u8F7D\u540E\u8BA2\u9605 theme:changed" })] })] })] })] }), showRegions && (_jsx("div", { className: "mx-5 mb-5 rounded border border-dashed border-[#2563eb] px-4 py-2 text-[11px] text-muted", "data-testid": "drag-legend", children: "\u84DD\u8272\u865A\u7EBF = \u53EF\u62D6\u62FD\u6807\u9898\u680F\uFF08-webkit-app-region: drag\uFF09\uFF1B\u6A59\u8272\u865A\u7EBF = \u63A7\u4EF6\u533A\uFF08no-drag\uFF09\u3002" }))] }));
}
//# sourceMappingURL=App.js.map