表单与选择@neumorphism-ui/switch

Switch

在原生 checkbox input 上应用 role=switch,保留表单和浏览器行为。支持 checked、defaultChecked 和 onCheckedChange。

@neumorphism-ui/switch
Registry JSON

预览

安装

安装: switchnpx shadcn@latest add @neumorphism-ui/switch

CLI 会依次处理 npm 依赖、base token、utils 和组件源码。

首次使用此 Registry?请先完成 namespace 配置 。

用法

用法: 安装npx shadcn@latest add @neumorphism-ui/switch @neumorphism-ui/label

Import

Switch import 代码
import { Switch } from "@/components/ui/switch"
import { Label } from "@/components/ui/label"

Example

Switch 用法代码
<div className="flex items-center justify-between gap-4">
  <Label htmlFor="system-theme">跟随系统主题</Label>
  <Switch id="system-theme" name="systemTheme" defaultChecked />
</div>

API 参考

组件属性类型默认值说明
Switchchecked / defaultCheckedboolean—受控的开启状态或非受控初始状态。
SwitchonCheckedChange(checked: boolean) => void—开启状态变化时调用。
SwitchonChangeReact.ChangeEventHandler<HTMLInputElement>—接收原生 checkbox change event。
Switchdisabledbooleanfalse禁用输入和指针操作。
Switchname / value / requirednative input props—用于表单提交的原生 checkbox 属性。

无障碍

  • 01

    由于在真实 checkbox 上使用 role=switch,会向辅助技术传达 Space 键行为和 checked 状态。

  • 02

    将 Label 的 htmlFor 与 Switch 的 id 关联,或提供 aria-label。

  • 03

    对于不会立即生效、还需另行保存的 boolean 选择,Checkbox 通常更自然。