主题切换

应用程序侧边栏的手动明/暗控制。下一个主题从确切的交互点平滑地揭示出来,用户的选择在本地保留,并且减少动作或不受支持的浏览器会回退到立即切换。

安装

使用 CLI 或手动添加可重用主题切换及其语义主题依赖项:

已支持深色模式,此 组件 会自动使用 BoardUI 的语义设计变量。 设置主题切换.

CLI 将组件源(及其内部依赖项)复制到您的项目中并安装任何所需的 npm 包。

npx boardui@latest add theme-toggle

使用共享 UI 包或自定义导入别名? 配置您的安装路径 在运行 CLI 之前。复制粘贴示例使用默认导入。

项目设置

光是默认值。应用保存的 boardui:theme 水化前的值,以防止第一次油漆闪光,并添加 suppressHydrationWarning 因为根类是在 React 水合之前在浏览器中设置的。

// app/layout.tsximport Script from "next/script";import "@/styles/globals.css"; const themeScript = '(function(){try{var dark=localStorage.getItem("boardui:theme")==="dark";document.documentElement.classList.toggle("dark",dark)}catch(e){document.documentElement.classList.remove("dark")}})();'; export default function RootLayout({ children }: { children: React.ReactNode }) { return ( <html lang="en" suppressHydrationWarning> <body> <Script id="boardui-theme" strategy="beforeInteractive"> {themeScript} </Script> {children} </body> </html> );}

该切换只能手动进行:它不会读取操作系统主题。它的简化动作和不受支持的浏览器后备会立即切换,而不会出现圆形显示。

折叠的侧边栏

套装 collapsed 用于 36px 仅图标控件,适合紧凑的侧边栏导轨,同时保持相同的可访问按下状态。

<ThemeToggle collapsed />