组件

Switch

An on/off switch drawn as one clean felt pill: the terracotta marker fills the track when it is on, and a paper coin for a thumb slides across. Radix Switch underneath.

安装

terminal
npx shadcn@latest add @wobko/switch

第一次使用?先把 registry 加进 components.json。 如何安装

示例

Default

Flip it: the marker fills the track and the paper coin slides across. The label toggles it too.

Sizes and states

Default and sm, off and on, invalid (red pencil outline that stays red when it is on) and disabled (dashed pencil, graphite fill). The thumb's side and the fill carry the state, so it never rests on color alone.

Settings list

The usual settings page: a title and a line of help on the left, the switch at the end. Hovering anywhere on a row lights its switch (the row is a `group/field`).

In a form

With a name (and optional value) a switch submits like a native checkbox: on gives "on" or its value, off sends nothing. Controlled here, so the summary follows it.

Demo

Official `switch-demo`: a switch and its label. Flip it and the marker fills the track while the paper coin slides across; the label toggles it too. Only the id is prefixed, so it stays unique on a page with other examples.

API 参考

Switch

Radix Switch.Root (a <button role="switch">, plus Radix's hidden checkbox inside a form) with the track drawn behind it and the thumb (data-slot="switch-thumb") inside. On, hover, focus and pressed follow the element's state in CSS, so a click never redraws anything.

属性类型默认值说明
size"default" | "sm""default"44 x 24 px or 36 x 20 px (calm density always uses the small one). The thumb and its travel follow the size through two custom properties.
checkedboolean-Controlled state.
defaultCheckedboolean-Initial state when uncontrolled.
onCheckedChange(checked: boolean) => void-Called on click, Space and Enter.
disabledbooleanfalseDashed pencil track on a graphite wash. Not focusable, no hover.
aria-invalidboolean | "true" | "false"-true draws the outline in red pencil, and keeps it red on top of the fill when the switch is on.
name / value / required / formstring | boolean-Forwarded to Radix's hidden input: on submits "on" (or `value`), off submits nothing.
seedstring | number-Pins the hand: the same seed always draws the same track and coin. Default: derived from useId().

无障碍

  • role="switch" with aria-checked true or false. Space and Enter toggle it, Tab focuses it (Radix). Label it with <Label htmlFor>, wrap it, or use aria-label.
  • State is not carried by color alone: the thumb sits at the start when off and at the end when on, and the track fills when it is on.
  • The hit area is 8 px larger than the track on every side (an ::after, so layout never moves). The track is 24 px tall at the default size, 20 px small with the enlarged hit area.
  • A Label beside it is a peer: peer-disabled dims it, and hovering the row (a `group/field`) or the label right after it lights the switch's hover overlay.
  • Keyboard focus draws a blue felt ring; the drawing is aria-hidden, pointer-events: none and never focusable. Windows high contrast replaces the ink by a real border, a Highlight fill when on and a solid thumb.
  • Reduced motion (the OS setting or data-motion="reduced") makes the thumb jump and the marker appear at once.
  • Right-to-left: the thumb starts at the right and travels left (rtl: twins).

源码

CLI 会复制进你项目的文件。

正在加载源码