示例工程 15-tailwind-css-and-design-tokens
15-tailwind-css-and-design-tokens/app/.tsc/src/App.js
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { useCallback, useEffect, useState } from "react";
const tabs = [
{ id: "tokens", label: "设计 token" },
{ id: "dark", label: "暗色" },
{ id: "csp", label: "CSP 与体积" },
];
const tokens = [
{ name: "--color-primary", value: "#2563eb", swatch: "bg-primary" },
{ name: "--color-primary-soft", value: "#dbeafe", swatch: "bg-primary-soft" },
{ name: "--color-surface", value: "#ffffff", swatch: "bg-surface" },
{ name: "--color-panel", value: "#f8fafc", swatch: "bg-panel" },
{ name: "--color-border", value: "#e2e8f0", swatch: "bg-border" },
{ name: "--color-text", value: "#0f172a", swatch: "bg-text" },
{ name: "--color-muted", value: "#64748b", swatch: "bg-muted" },
{ name: "--radius-card", value: "8px", swatch: "bg-primary-soft" },
];
const cspRows = [
{
env: "开发(Vite dev server)",
style: "style-src 'self' 'unsafe-inline'",
reason: "样式通过 <style> 注入",
},
{
env: "生产(构建产物)",
style: "style-src 'self'",
reason: "CSS 抽成独立文件",
},
{ env: "脚本", style: "script-src 'self'", reason: "不需要 unsafe-eval" },
{ env: "HMR 连接", style: "connect-src 'self' ws:", reason: "仅开发态需要" },
];
function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
function applyThemeClass(dark) {
document.documentElement.classList.toggle("dark", dark);
}
export default function App() {
const [tab, setTab] = useState("tokens");
const [theme, setTheme] = useState({
source: "system",
dark: false,
});
const apply = useCallback((next) => {
setTheme(next);
applyThemeClass(next.dark);
}, []);
const setThemeSource = useCallback(async (source) => {
const next = await window.api.theme.set(source);
apply(next);
}, [apply]);
useEffect(() => {
void window.api.theme.get().then(apply);
return window.api.theme.onChange(apply);
}, [apply]);
useEffect(() => {
window.__setTab = (next) => setTab(next);
window.__setTheme = async (source) => {
await setThemeSource(source);
};
window.__snapshot = () => ({
tab,
source: theme.source,
dark: theme.dark,
});
window.__smoke = async () => {
await setThemeSource("dark");
await sleep(150);
const darkApplied = document.documentElement.classList.contains("dark");
await setThemeSource("light");
await sleep(150);
const lightApplied = !document.documentElement.classList.contains("dark");
const button = document.querySelector('[data-testid="primary-button"]');
const primary = button
? getComputedStyle(button).backgroundColor
: "missing";
return {
darkApplied,
lightApplied,
primary,
tokens: document.querySelectorAll("[data-token]").length,
source: (await window.api.theme.get()).source,
};
};
}, [setThemeSource, tab, theme.source, theme.dark]);
return (_jsxs("div", { className: "min-h-screen bg-surface px-8 py-7 text-text", children: [_jsxs("header", { className: "flex items-end justify-between gap-6 border-b border-border pb-4", children: [_jsxs("div", { children: [_jsx("h1", { className: "text-2xl font-semibold", "data-testid": "app-title", children: "Tailwind \u8BBE\u8BA1 token" }), _jsx("p", { className: "mt-1 text-sm text-muted", children: "@theme \u00B7 \u6697\u8272\u53D8\u4F53 \u00B7 CSP \u00B7 \u4F53\u79EF\u9884\u7B97" })] }), _jsx("nav", { className: "flex gap-1", children: tabs.map((item) => (_jsx("button", { className: `rounded-md border px-3 py-1.5 text-sm ${tab === item.id
? "border-border bg-panel font-semibold text-primary"
: "border-transparent text-muted hover:bg-panel"}`, "data-testid": `tab-${item.id}`, onClick: () => setTab(item.id), children: item.label }, item.id))) })] }), _jsxs("div", { className: "mt-4 flex items-center gap-2 text-sm", children: [_jsx("span", { className: "text-muted", children: "\u4E3B\u9898\uFF1A" }), ["system", "light", "dark"].map((source) => (_jsx("button", { className: `rounded-md border px-2.5 py-1 ${theme.source === source
? "border-primary text-primary"
: "border-border text-muted"}`, "data-testid": `theme-${source}`, onClick: () => void setThemeSource(source), children: source }, source))), _jsxs("span", { className: "ml-2 text-muted", "data-testid": "dark-state", children: ["dark: ", String(theme.dark)] })] }), _jsxs("main", { className: "mt-6", children: [tab === "tokens" && (_jsxs("section", { children: [_jsx("h2", { className: "text-lg font-semibold", children: "token \u662F\u4E09\u5C42\u7684\u4E2D\u95F4\u5C42" }), _jsxs("p", { className: "mt-1 max-w-3xl text-sm text-muted", children: ["token \u2192 \u5DE5\u5177\u7C7B \u2192 \u7EC4\u4EF6\u3002\u7EC4\u4EF6\u53EA\u5199 ", _jsx("code", { children: "bg-primary" }), "\u3001", _jsx("code", { children: "rounded-card" }), "\uFF0C\u4E0D\u5199\u6B7B\u989C\u8272\u4E0E\u5706\u89D2\u3002"] }), _jsx("div", { className: "mt-4 grid grid-cols-4 gap-3", children: tokens.map((token) => (_jsxs("div", { className: "rounded-card border border-border bg-panel p-3", "data-token": true, children: [_jsx("div", { className: `h-8 rounded-md border border-border ${token.swatch}` }), _jsx("div", { className: "mt-2 font-mono text-xs text-muted", children: token.name }), _jsx("div", { className: "font-mono text-xs", children: token.value })] }, token.name))) }), _jsxs("div", { className: "mt-5 grid grid-cols-2 gap-4", children: [_jsxs("div", { className: "rounded-card border border-border bg-panel p-5", children: [_jsx("div", { className: "text-xs uppercase tracking-wide text-muted", children: "Card / panel" }), _jsx("p", { className: "mt-2 text-sm", children: "\u80CC\u666F\u3001\u8FB9\u6846\u3001\u6B63\u6587\u4E0E\u5F31\u5316\u6587\u5B57\u90FD\u6765\u81EA token\uFF1B\u6539\u4E3B\u9898\u53EA\u6539\u53D8\u91CF\u3002" }), _jsx("button", { className: "mt-4 rounded-card bg-primary px-4 py-2 text-sm font-medium text-white", "data-testid": "primary-button", children: "\u4E3B\u64CD\u4F5C\u6309\u94AE" })] }), _jsxs("div", { className: "rounded-card border border-border bg-surface p-5", children: [_jsx("div", { className: "text-xs uppercase tracking-wide text-muted", children: "Surface / muted" }), _jsxs("p", { className: "mt-2 text-sm text-muted", children: ["\u6B21\u7EA7\u6587\u5B57\u7528 ", _jsx("code", { children: "text-muted" }), "\uFF0C\u4E0D\u8981\u4E34\u65F6\u8C03\u900F\u660E\u5EA6\u3002"] }), _jsxs("div", { className: "mt-4 flex gap-2", children: [_jsx("span", { className: "rounded-full bg-primary-soft px-2 py-1 text-xs text-primary", children: "primary-soft" }), _jsx("span", { className: "rounded-full border border-border px-2 py-1 text-xs text-muted", children: "border" })] })] })] })] })), tab === "dark" && (_jsxs("section", { children: [_jsx("h2", { className: "text-lg font-semibold", children: "\u6697\u8272\u53EA\u8986\u76D6\u53D8\u91CF" }), _jsxs("p", { className: "mt-1 max-w-3xl text-sm text-muted", children: [_jsx("code", { children: ".dark" }), " \u8986\u76D6 ", _jsx("code", { children: "--color-*" }), "\uFF0C\u7EC4\u4EF6\u7C7B\u4E0D\u53D8\uFF1B", _jsx("code", { children: "dark:" }), " \u53D8\u4F53\u7528\u4E8E\u5C11\u91CF\u7ED3\u6784\u6027\u5DEE\u5F02\u3002"] }), _jsxs("div", { className: "mt-4 grid grid-cols-2 gap-4", children: [_jsxs("div", { className: "rounded-card border border-border bg-surface p-5", children: [_jsx("div", { className: "text-sm font-medium", children: "\u5F53\u524D\u4E3B\u9898\u9884\u89C8" }), _jsxs("p", { className: "mt-2 text-sm text-muted", children: ["\u7CFB\u7EDF\u3001\u6D45\u8272\u3001\u6DF1\u8272\u7531\u4E3B\u8FDB\u7A0B\u7684 ", _jsx("code", { children: "nativeTheme" }), " \u51B3\u5B9A\u3002"] }), _jsx("button", { className: "mt-4 rounded-card bg-primary px-4 py-2 text-sm text-white", children: "\u4E3B\u6309\u94AE" })] }), _jsxs("div", { className: "rounded-card border border-border bg-panel p-5", children: [_jsx("div", { className: "text-sm font-medium", children: "\u8BB0\u4F4F\u4E09\u4EF6\u4E8B" }), _jsxs("ul", { className: "mt-2 list-disc pl-5 text-sm text-muted", children: [_jsx("li", { children: "\u6697\u8272\u4E0D\u65B0\u589E\u7B2C\u4E8C\u5957 CSS \u6587\u4EF6" }), _jsx("li", { children: "\u4E0D\u4F7F\u7528\u5185\u8054 style \u5207\u6362\u989C\u8272" }), _jsx("li", { children: "\u4E0D\u6301\u4E45\u5316\u7528\u6237\u7684\u4E34\u65F6\u9884\u89C8" })] })] })] })] })), tab === "csp" && (_jsxs("section", { children: [_jsx("h2", { className: "text-lg font-semibold", children: "CSP \u4E0E\u4F53\u79EF\u9884\u7B97" }), _jsxs("table", { className: "mt-4 w-full border-collapse text-sm", children: [_jsx("thead", { children: _jsxs("tr", { className: "bg-panel text-left text-xs text-muted", children: [_jsx("th", { className: "border border-border px-3 py-2", children: "\u73AF\u5883" }), _jsx("th", { className: "border border-border px-3 py-2", children: "\u6307\u4EE4" }), _jsx("th", { className: "border border-border px-3 py-2", children: "\u539F\u56E0" })] }) }), _jsx("tbody", { children: cspRows.map((row) => (_jsxs("tr", { children: [_jsx("td", { className: "border border-border px-3 py-2", children: row.env }), _jsx("td", { className: "border border-border px-3 py-2 font-mono text-xs", children: row.style }), _jsx("td", { className: "border border-border px-3 py-2 text-muted", children: row.reason })] }, row.env))) })] }), _jsxs("div", { className: "mt-4 grid grid-cols-3 gap-3", children: [_jsxs("div", { className: "rounded-card border border-border bg-panel p-4", children: [_jsx("div", { className: "text-xs text-muted", children: "\u751F\u4EA7 CSS\uFF08gzip\uFF09" }), _jsx("div", { className: "mt-1 font-mono text-xl", "data-testid": "css-gzip", children: "1.8 KB" })] }), _jsxs("div", { className: "rounded-card border border-border bg-panel p-4", children: [_jsx("div", { className: "text-xs text-muted", children: "\u624B\u5199 CSS\uFF08gzip\uFF09" }), _jsx("div", { className: "mt-1 font-mono text-xl", children: "0.3 KB" })] }), _jsxs("div", { className: "rounded-card border border-border bg-panel p-4", children: [_jsx("div", { className: "text-xs text-muted", children: "\u5185\u8054 style" }), _jsx("div", { className: "mt-1 font-mono text-xl", children: "\u751F\u4EA7 0" })] })] }), _jsxs("p", { className: "mt-3 text-sm text-muted", children: ["\u6570\u5B57\u7531 ", _jsx("code", { children: "demo-03-csp-style" }), " \u7684\u672C\u5730\u751F\u4EA7\u6784\u5EFA\u5B9E\u6D4B\uFF1B\u5347\u7EA7 Tailwind \u6216\u65B0\u589E\u7EC4\u4EF6\u540E\u8981\u91CD\u6D4B\u3002"] })] }))] })] }));
}
//# sourceMappingURL=App.js.map