示例工程 16-shadcn-ui-component-layer
16-shadcn-ui-component-layer/app/.tsc/src/App.js
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { useCallback, useEffect, useState } from "react";
import { Toast } from "@base-ui-components/react/toast";
import { Button } from "./components/ui/button";
import { Dialog, DialogClose, DialogContent, DialogDescription, DialogTitle, DialogTrigger, } from "./components/ui/dialog";
import { Field } from "./components/ui/field";
import { Input } from "./components/ui/input";
import { Select } from "./components/ui/select";
import { Tabs, TabsList, TabsPanel, TabsTab } from "./components/ui/tabs";
import { cn } from "./lib/utils";
const options = [
{ value: "sans", label: "Sans-serif(系统默认)" },
{ value: "serif", label: "Serif(衬线)" },
{ value: "mono", label: "Monospace(等宽)" },
];
function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
export default function App() {
const toast = Toast.useToastManager();
const [tab, setTab] = useState("buttons");
const [dialogOpen, setDialogOpen] = useState(false);
const [selectOpen, setSelectOpen] = useState(false);
const [selectValue, setSelectValue] = useState("sans");
const [name, setName] = useState("智慧笔记本");
const addToast = useCallback(() => {
toast.add({
id: "demo-toast",
title: "设置已保存",
description: "组件层示例:通知归属当前窗口。",
timeout: 0,
});
}, [toast]);
useEffect(() => {
window.__setTab = (next) => setTab(next);
window.__openDialog = () => setDialogOpen(true);
window.__closeDialog = () => setDialogOpen(false);
window.__openSelect = () => setSelectOpen(true);
window.__toast = addToast;
window.__clearToasts = () => {
for (const item of toast.toasts)
toast.close(item.id);
};
window.__snapshot = () => ({ tab, dialogOpen, selectValue });
window.__smoke = async () => {
const q = (selector) => document.querySelector(selector);
setTab("buttons");
await sleep(60);
const primary = q('[data-testid="primary-button"]');
const merge = q('[data-testid="merge-demo"]');
const primaryBg = primary
? getComputedStyle(primary).backgroundColor
: "missing";
const mergePadding = merge
? getComputedStyle(merge).paddingLeft
: "missing";
setTab("dialog");
await sleep(80);
setDialogOpen(true);
await sleep(180);
const popup = q('[data-testid="dialog-popup"]');
const dialogOpened = Boolean(popup);
const focusInside = popup
? popup.contains(document.activeElement)
: false;
setDialogOpen(false);
await sleep(220);
const dialogClosed = !q('[data-testid="dialog-popup"]');
const trigger = q('[data-testid="dialog-trigger"]');
const focusReturned = document.activeElement === trigger;
setTab("select");
await sleep(80);
setSelectOpen(true);
await sleep(180);
const listbox = q('[role="listbox"]');
const optionCount = listbox
? listbox.querySelectorAll('[role="option"]').length
: 0;
const serif = q('[data-testid="select-item-serif"]');
if (serif) {
const rect = serif.getBoundingClientRect();
const move = {
bubbles: true,
clientX: rect.x + 8,
clientY: rect.y + 8,
};
serif.dispatchEvent(new MouseEvent("mousemove", move));
serif.dispatchEvent(new PointerEvent("pointermove", move));
await sleep(80);
serif.dispatchEvent(new PointerEvent("pointerdown", { bubbles: true, button: 0 }));
serif.click();
}
await sleep(220);
const triggerText = q('[data-testid="select-trigger"]')?.textContent;
const selectValueOk = Boolean(triggerText?.includes("Serif"));
const stateValue = q('[data-testid="select-value"]')?.textContent ?? "";
addToast();
await sleep(150);
const toastVisible = Boolean(q('[data-testid="toast"]'));
return {
primaryBg,
mergePadding,
dialogOpened,
focusInside,
dialogClosed,
focusReturned,
options: optionCount,
selectValue: selectValueOk,
triggerText: triggerText ?? "",
stateValue,
toastVisible,
};
};
}, [addToast, dialogOpen, selectValue, tab]);
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: "\u7EC4\u4EF6\u5C42\u5DE5\u4F5C\u53F0" }), _jsx("p", { className: "mt-1 text-sm text-muted", children: "Base UI \u00B7 cva \u00B7 cn() \u00B7 \u7EC4\u4EF6\u6E90\u7801\u5165\u5E93" })] }), _jsxs("span", { className: "rounded-full border border-border px-3 py-1 text-xs text-muted", children: [window.appInfo?.platform ?? "unknown", " \u00B7 Electron", " ", window.appInfo?.versions.electron ?? "—"] })] }), _jsxs(Tabs, { value: tab, onValueChange: (value) => setTab(value), className: "mt-5", children: [_jsxs(TabsList, { children: [_jsx(TabsTab, { value: "buttons", "data-testid": "tab-buttons", children: "\u6309\u94AE\u4E0E\u8868\u5355" }), _jsx(TabsTab, { value: "dialog", "data-testid": "tab-dialog", children: "\u5BF9\u8BDD\u6846" }), _jsx(TabsTab, { value: "select", "data-testid": "tab-select", children: "\u9009\u62E9\u4E0E\u901A\u77E5" })] }), _jsxs(TabsPanel, { value: "buttons", children: [_jsxs("div", { className: "grid grid-cols-2 gap-4", children: [_jsxs("div", { className: "rounded-card border border-border bg-panel p-5", children: [_jsx("h2", { className: "text-sm font-semibold", children: "Button \u53D8\u4F53" }), _jsx("p", { className: "mt-1 text-xs text-muted", children: "cva \u7BA1\u53D8\u4F53\uFF0Ccn() \u7BA1\u8C03\u7528\u65B9\u8986\u76D6\u3002" }), _jsxs("div", { className: "mt-4 flex flex-wrap gap-2", children: [_jsx(Button, { "data-testid": "primary-button", children: "\u4E3B\u64CD\u4F5C" }), _jsx(Button, { variant: "secondary", children: "\u6B21\u8981" }), _jsx(Button, { variant: "outline", children: "\u63CF\u8FB9" }), _jsx(Button, { variant: "ghost", children: "\u5E7D\u7075" }), _jsx(Button, { variant: "destructive", children: "\u5371\u9669" })] }), _jsxs("div", { className: "mt-4 flex gap-2", children: [_jsx(Button, { size: "sm", children: "\u5C0F" }), _jsx(Button, { size: "md", children: "\u4E2D" }), _jsx(Button, { size: "icon", "aria-label": "\u6DFB\u52A0", children: "+" })] })] }), _jsxs("div", { className: "rounded-card border border-border bg-panel p-5", children: [_jsx("h2", { className: "text-sm font-semibold", children: "Input \u4E0E\u5408\u5E76" }), _jsxs("div", { className: "mt-3 flex flex-col gap-3", children: [_jsx(Field, { label: "\u5DE5\u4F5C\u533A\u540D\u79F0", description: "Field \u628A label \u4E0E\u8BF4\u660E\u5305\u5728\u4E00\u8D77\u3002", children: _jsx(Input, { "data-testid": "name-input", value: name, onChange: (event) => setName(event.target.value) }) }), _jsx(Button, { "data-testid": "merge-demo", className: "w-fit px-6", variant: "secondary", children: "px-2 \u88AB px-6 \u8986\u76D6" })] })] })] }), _jsxs("div", { className: "mt-4 rounded-card border border-border bg-surface p-4 text-xs text-muted", children: ["\u7EC4\u4EF6\u6E90\u7801\u5728 ", _jsx("code", { children: "src/components/ui/" }), "\uFF0C\u6BCF\u4E2A\u6587\u4EF6\u5934\u8BB0\u5F55 provenance\uFF1B\u5347\u7EA7\u65F6\u9010\u6587\u4EF6 diff\uFF0C\u4E0D\u8DD1\u8FD0\u884C\u65F6\u7EC4\u4EF6\u5E93\u3002"] })] }), _jsxs(TabsPanel, { value: "dialog", children: [_jsxs("div", { className: "rounded-card border border-border bg-panel p-5", children: [_jsx("h2", { className: "text-sm font-semibold", children: "Dialog \u4E0E\u7126\u70B9" }), _jsx("p", { className: "mt-1 text-sm text-muted", children: "Base UI \u8D1F\u8D23\u7126\u70B9\u9677\u9631\u3001Esc \u4E0E\u7126\u70B9\u5F52\u8FD8\uFF1B\u6211\u4EEC\u53EA\u5199\u6837\u5F0F\u4E0E\u5185\u5BB9\u3002" }), _jsxs(Dialog, { open: dialogOpen, onOpenChange: setDialogOpen, children: [_jsx(DialogTrigger, { "data-testid": "dialog-trigger", className: "mt-4 h-9 rounded-card bg-primary px-4 text-sm font-medium text-primary-foreground", children: "\u6253\u5F00\u5BF9\u8BDD\u6846" }), _jsxs(DialogContent, { children: [_jsx(DialogTitle, { className: "text-base font-semibold", children: "\u53D1\u5E03\u53D8\u66F4" }), _jsx(DialogDescription, { className: "mt-1 text-sm text-muted", children: "\u8FD9\u662F\u4E00\u4E2A\u53D7\u63A7 Dialog\uFF1B\u5173\u95ED\u540E\u7126\u70B9\u5E94\u56DE\u5230\u89E6\u53D1\u6309\u94AE\u3002" }), _jsx("div", { className: "mt-4", children: _jsx(Field, { label: "\u53D8\u66F4\u8BF4\u660E", children: _jsx(Input, { "data-testid": "dialog-input", autoFocus: true, defaultValue: "\u66F4\u65B0\u7EC4\u4EF6\u5C42" }) }) }), _jsxs("div", { className: "mt-5 flex justify-end gap-2", children: [_jsx(DialogClose, { "data-testid": "dialog-cancel", children: "\u53D6\u6D88" }), _jsx(Button, { "data-testid": "dialog-confirm", onClick: () => setDialogOpen(false), children: "\u786E\u8BA4" })] })] })] })] }), _jsxs("div", { className: "mt-4 rounded-card border border-border bg-surface p-4 text-sm text-muted", children: ["Electron \u6CE8\u610F\uFF1A\u65E0\u8FB9\u6846\u7A97\u53E3\u7684 ", _jsx("code", { children: "-webkit-app-region: drag" }), " ", "\u4F1A\u76D6\u4F4F\u5F39\u5C42\u6309\u94AE\uFF0C\u5F39\u5C42\u533A\u57DF\u8981\u663E\u5F0F ", _jsx("code", { children: "no-drag" }), "\uFF1BEsc \u4E5F\u8981\u907F\u514D\u4E0E\u9000\u51FA\u5168\u5C4F\u3001\u5168\u5C40\u5FEB\u6377\u952E\u51B2\u7A81\u3002"] })] }), _jsxs(TabsPanel, { value: "select", children: [_jsxs("div", { className: "grid grid-cols-2 gap-4", children: [_jsxs("div", { className: "rounded-card border border-border bg-panel p-5", children: [_jsx("h2", { className: "text-sm font-semibold", children: "Select" }), _jsxs("p", { className: "mt-1 text-xs text-muted", children: ["Base UI \u7528 ", _jsx("code", { children: "render" }), " \u7EC4\u5408\uFF0C\u4E0D\u662F Radix \u7684", " ", _jsx("code", { children: "asChild" }), "\u3002"] }), _jsx("div", { className: "mt-4", children: _jsx(Select, { label: "\u754C\u9762\u5B57\u4F53", value: selectValue, options: options, open: selectOpen, onOpenChange: setSelectOpen, onValueChange: (value) => {
setSelectValue(value);
setSelectOpen(false);
} }) }), _jsxs("p", { className: "mt-3 text-xs text-muted", children: ["\u5F53\u524D\u9009\u62E9\uFF1A", _jsx("code", { "data-testid": "select-value", children: selectValue })] })] }), _jsxs("div", { className: "rounded-card border border-border bg-panel p-5", children: [_jsx("h2", { className: "text-sm font-semibold", children: "Toast" }), _jsx("p", { className: "mt-1 text-xs text-muted", children: "\u901A\u77E5\u5F52\u5C5E\u5F53\u524D\u7A97\u53E3\uFF0C\u591A\u7A97\u53E3\u65F6\u4E0D\u8981\u5E7F\u64AD\u5230\u6240\u6709\u7A97\u53E3\u3002" }), _jsx(Button, { className: "mt-4", variant: "secondary", "data-testid": "toast-button", onClick: addToast, children: "\u663E\u793A\u901A\u77E5" }), _jsxs("ul", { className: "mt-4 list-disc pl-5 text-xs text-muted", children: [_jsx("li", { children: "IME \u7EC4\u5408\u8F93\u5165\u65F6\u4E0D\u8981\u89E6\u53D1\u5FEB\u6377\u952E" }), _jsx("li", { children: "Toast \u4E0D\u62A2\u7126\u70B9\uFF0C\u4F46\u8981\u6709 aria-live" }), _jsx("li", { children: "\u5173\u95ED\u6309\u94AE \u2265 24px \u70B9\u51FB\u533A" })] })] })] }), _jsxs("div", { className: cn("mt-4 rounded-card border border-dashed border-border p-4 text-xs text-muted", "nodrag"), "data-testid": "drag-note", children: ["\u65E0\u8FB9\u6846\u7A97\u53E3\u62D6\u62FD\u533A\u4E0E\u5F39\u5C42\u51B2\u7A81\u65F6\uFF0C\u7ED9\u5F39\u5C42\u52A0 ", _jsx("code", { children: "no-drag" }), "\uFF1B \u8FD9\u4E2A\u793A\u4F8B\u4E0D\u662F\u65E0\u8FB9\u6846\u7A97\u53E3\uFF0C\u53EA\u5C55\u793A\u7EA6\u5B9A\u3002"] })] })] })] }));
}
//# sourceMappingURL=App.js.map