开关
基于 React Aria 构建的可访问切换开关 - 原生切换语义、键盘支持和焦点可见环。两种形状(丸状和矩形)三种尺寸。开启状态使用主渐变,带有嵌入环和拇指上浮雕的小蓝色标签,与 Figma 1:1 匹配。
丸
大
中
小
矩形
大
中
小
CLI
手动安装
CLI 将组件源(及其内部依赖项)复制到您的项目中并安装任何所需的 npm 包。
npx boardui@latest add switch使用共享 UI 包或自定义导入别名? 配置您的安装路径 在运行 CLI 之前。复制粘贴示例使用默认导入。
形状
两种形状通过 shape 属性。 pill (默认)是全圆角的; rectangle 用途 radius/md 在赛道上和 radius/xs 在拇指上,两者均按尺寸缩放。拇指的内部标签遵循相同的形状。
<Switch shape="pill" defaultSelected /> {/* fully rounded (default) */}尺寸
三种尺寸通过 size 属性 - lg (56×32), md (42×24,默认)和 sm (28×16)。拇指、衬垫、行程距离和开启状态环都一起缩放。
<Switch size="lg" defaultSelected /> {/* 56 × 32 */}状态和标签
使用 isSelected + onChange 对于受控开关,或 defaultSelected 对于不受控制的。 isDisabled 将控件调暗至 50%。通行证 children 对于标签 - React Aria 将其与输入相关联,因此单击文本可以切换开关。
{/* Controlled */}<Switch isSelected={enabled} onChange={setEnabled} />
{/* Disabled */}<Switch isDisabled defaultSelected />
{/* With label (react-aria associates it automatically) */}<Switch>Auto-sync to cloud</Switch>卡
可选设置卡 - 可选的 24×24 主图标、左侧的标题和简短说明以及右侧的开关。整个卡片切换并显示悬停背景,切换对应于 CheckboxCard。通过任何 Remix 图标传递 icon 到 SwitchCard.
import { RiNotification4Line, RiMoonLine } from "@remixicon/react";import { SwitchCard } from "@/components/base/switch/switch-card";
export function Example() { return ( <div className="flex max-w-md flex-col gap-4"> <SwitchCard icon={RiNotification4Line} title="Push notifications" description="Get notified when something needs your attention." defaultSelected /> {/* icon is optional - pass the component reference, not <RiMoonLine /> */} <SwitchCard icon={RiMoonLine} title="Dark mode" /> </div> );}