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"; type TabId = "buttons" | "dialog" | "select"; declare global { interface Window { appInfo: { platform: string; versions: { electron: string; chrome: string; node: string }; }; __setTab?: (tab: TabId) => void; __openDialog?: () => void; __closeDialog?: () => void; __openSelect?: () => void; __toast?: () => void; __clearToasts?: () => void; __snapshot?: () => unknown; __smoke?: () => Promise; } } const options = [ { value: "sans", label: "Sans-serif(系统默认)" }, { value: "serif", label: "Serif(衬线)" }, { value: "mono", label: "Monospace(等宽)" }, ]; function sleep(ms: number): Promise { 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: string): T | null => 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 (

组件层工作台

Base UI · cva · cn() · 组件源码入库

{window.appInfo?.platform ?? "unknown"} · Electron{" "} {window.appInfo?.versions.electron ?? "—"}
setTab(value as TabId)} className="mt-5" > 按钮与表单 对话框 选择与通知

Button 变体

cva 管变体,cn() 管调用方覆盖。

Input 与合并

setName(event.target.value)} />
组件源码在 src/components/ui/,每个文件头记录 provenance;升级时逐文件 diff,不跑运行时组件库。

Dialog 与焦点

Base UI 负责焦点陷阱、Esc 与焦点归还;我们只写样式与内容。

打开对话框 发布变更 这是一个受控 Dialog;关闭后焦点应回到触发按钮。
取消
Electron 注意:无边框窗口的 -webkit-app-region: drag{" "} 会盖住弹层按钮,弹层区域要显式 no-drag;Esc 也要避免与退出全屏、全局快捷键冲突。

Select

Base UI 用 render 组合,不是 Radix 的{" "} asChild。