Switch
Turns a setting on or off at once, with no Save. A track with a thumb that slides to the end when on: a light edge on the soft gray fill when off, the accent when on. label puts its name after it, and description a second line under the name.
Preview
Usage
When to use
- Turn a setting on or off where the change applies at once, such as email notifications.
- A row of settings on a settings page, each with its own switch.
When not to use
- A choice that waits for Save, or accepting terms. Use Checkbox.
- Picking one of several options. Use RadioGroup.
Platform support
What each platform has, from the spec. A selected platform is highlighted.
| Name | Web | iOS | Android | Note |
|---|---|---|---|---|
| size | Yes | Yes | Yes | |
| label | Yes | Yes | Yes | |
| description | Yes | Yes | Yes | |
| requiredText | Yes | Yes | Yes | |
| checked | Yes | Yes | Yes | |
| defaultChecked | Yes | Yes | Yes | |
| onCheckedChange | Yes | Yes | Yes | |
| disabled | Yes | Yes | Yes | |
| required | Yes | Yes | Yes | |
| name | Yes | Deferred | Deferred | Not generated for iOS and Android. |
| value | Yes | Deferred | Deferred | Not generated for iOS and Android. |
Variants
size
Track width from size-element-*, so it follows touch; the label and description are 14px at every size. lg is the default, for settings pages and props panels; md in dense rows and toolbars, such as a table; xl next to xl Inputs and Buttons.
Examples
Default
With a description
Disabled
In a table row
md, the smallest size, for dense rows and toolbars. The name is in another column, so the switch takes aria-label.
Next to large controls
lg matches lg Inputs and Buttons; the label and description step up with it (14px). The Variants section shows every size side by side.
Do and don't
Do
- Use it for a setting that takes effect at once, and apply the change in onCheckedChange.
- Name the setting in the label, and say what on does in the description.
- Use md in dense rows and toolbars, such as a table, where the name sits in another column (aria-label). A props panel keeps the default lg, so the switch stays easy to hit on a phone.
- Match the size of the controls around it: lg with lg Inputs on a settings page, md in a dense row, xl with xl Inputs and Buttons.
Don't
- Don't use it in a form that waits for Save; use Checkbox.
- Don't use it to pick one of several options; use RadioGroup.
- Don't change the label when it toggles (On, Off); the thumb shows the state.
Props
| Name | Type | Values | Default | Description |
|---|---|---|---|---|
| size | enum | md | lg | xl | lg | Track width from size-element-*, so it follows touch; the label and description are 14px at every size. lg is the default, for settings pages and props panels; md in dense rows and toolbars, such as a table; xl next to xl Inputs and Buttons. |
| label | node | - | - | The visible name, after the switch. A press on it toggles the switch. Name the setting, not the action: Email notifications. |
| description | node | - | - | A second line under the label: what turning it on does. Needs label. |
| requiredText | node | - | Required | The Required note shown with required and a label, for a page in another language. |
| checked | boolean | - | - | Controlled state. Pair it with onCheckedChange. |
| defaultChecked | boolean | - | false | On at first render, when you don't control it. |
| onCheckedChange | function | - | - | Called with true or false when the user toggles it. Apply the change here; there is no Save. |
| disabled | boolean | - | false | Can't be focused or toggled; the label dims too. |
| required | boolean | - | false | Must be on to submit the form. |
| name | string | - | - | Form name. The form submits value under this name while the switch is on. Not generated for iOS and Android. |
| value | string | - | on | Value submitted while the switch is on. Not generated for iOS and Android. |
Anatomy
| Part | Description |
|---|---|
| root | The track: a recessed rail (shadow-inset), radius-full, a border-strong edge (border-control on hover, like Checkbox) on the soft gray fill; on, the accent fills it to the edge. As wide as size-element-* and as tall as half of it plus the two borders, so it follows touch. Mouse: md 32 by 18px, lg 36 by 20px, xl 44 by 24px; touch: md 36 by 20px, lg 44 by 24px, xl 48 by 26px. |
| thumb | The thumb, a raised knob (background-raised and shadow-low), offset from the inside start edge (offset-inline-start is that distance): a circle half of spacing-xs (2px) from the inner edge, the inside height minus spacing-xs: 12px (md), 14px (lg) or 18px (xl) on mouse. It takes the track's icon color: secondary when off, on-accent when on. |
| hitArea | Transparent ::before centered on the track, never shorter than size-target-min (24px on mouse, 44px on touch). |
| row | With label: the row that holds the switch, then the Label and the description, gap-sm. Shared with Checkbox and RadioGroup. |
| label | With label: a Label after the switch, level with it on its first line. 14px at md, lg and xl. |
| description | With description: secondary text under the label, gap-xs, the same size as the label. |
Accessibility
Role: switch
| Keys | Action |
|---|---|
| Space, Enter | Turns the switch on or off. |
| Tab | Moves focus to the next control; each switch is its own tab stop. |
- Name every switch with the label prop (a press on it toggles the switch), or aria-label when the name is shown elsewhere, such as a row in a table.
- description is linked with aria-describedby and read after the name, outside the label.
- The change applies at once. A choice that waits for Save is a Checkbox.
- On and off differ by the thumb's position as well as the fill, so the state never relies on color alone.
- The thumb slides only when the user allows motion; under prefers-reduced-motion it jumps.
- On mouse the track is 18px (md), 20px (lg) or 24px (xl) tall, but its hit area is never smaller than size-target-min (24px on mouse, 44px on touch). On touch md, lg and xl grow with size-element-md, -lg and -xl.
- In a right-to-left page the thumb starts at the right and slides left when on.
Install
npx werewolf-ui add switchRun npx werewolf-ui init once in your app before adding components. Init sets up tokens, their CSS import and agent rules. Add installs this component and its dependencies while preserving your theme. See Installation for requirements and the alternative registry setup.