Switch
A control that turns a setting on or off. It renders a native button with the switch role, and a hidden checkbox beside it so that forms submit its value.
- Size
- 1.7 kB
Example
import { Switch } from "menhir/switch";
export default function SwitchExample() {
return (
<label class="inline-flex items-center gap-2">
<Switch.Root
name="wifi"
defaultChecked
class="h-6 w-11 cursor-pointer rounded-full bg-stone-300 p-0.5 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-stone-500 data-checked:bg-stone-800 dark:bg-stone-700 dark:data-checked:bg-stone-300"
>
<Switch.Thumb class="block size-5 rounded-full bg-white shadow-sm transition-transform data-checked:translate-x-5 motion-reduce:transition-none dark:data-checked:bg-stone-900" />
</Switch.Root>
Wi-Fi
</label>
);
}
Anatomy
<Switch.Root>
<Switch.Thumb />
</Switch.Root>API reference
Root
The switch: a <button role="switch">, followed by a visually hidden <input type="checkbox"> for forms. Label it with a <label> around it or one whose for matches its id, or put it in a Field (In a Field, below). Its render function must render a <button>.
| Prop | Type | Default | Description |
|---|---|---|---|
checked | boolean | — | Whether the switch is on, when controlled. Pair it with onCheckedChange. |
defaultChecked | boolean | false | Whether the switch starts on, when uncontrolled. A form reset goes back to it. |
onCheckedChange | (checked: boolean) => void | — | Called with the new value when the user turns the switch on or off, or the form is reset. A controlled switch changes only if checked does. |
disabled | boolean | false | Whether the switch ignores the user. It cannot be focused, and the form does not submit it. |
readOnly | boolean | false | Whether the switch can be focused but not turned on or off. |
required | boolean | false | Whether the form can be submitted only while the switch is on. |
name | string | — | The name the form submits the switch under. |
value | string | "on" | What the form submits while the switch is on. |
form | string | — | The id of the form the switch belongs to, when it is not inside it. |
render | (props) => JSX.Element | — | Renders the switch with your own button component. Read once. |
Thumb
The part that slides between off and on: a <span>. Place it inside Root.
| Prop | Type | Default | Description |
|---|---|---|---|
render | (props) => JSX.Element | — | Renders the thumb with your own component. |
Keyboard
| Keys | Action |
|---|---|
| Space | Turns the switch on or off. |
| Enter | Turns the switch on or off. |
| Tab | Moves focus to or away from the switch. |
In a Field
A Switch joins a Field as Field.Control's render: the Field gives the button its id, label, description and error, and the hidden input its name. A required Switch that is off shows its error once the user has left it, turned it off again or tried to submit the form, and when the browser blocks the submission, focus moves to the switch. The Field's data-invalid and data-touched are on the button; style the Thumb through it, as in [data-invalid] .thumb. For a controlled Switch, render it with a function and put checked and onCheckedChange on it, as the Checkbox page shows. A Switch outside a Field costs nothing more.
import { Field } from "menhir/field";
import { Switch } from "menhir/switch";
export default function SwitchInField() {
return (
<Field.Root name="terms">
<Field.Label>Accept the terms</Field.Label>
<Field.Control render={Switch.Root} required>
<Switch.Thumb />
</Field.Control>
<Field.Description>You can read them on the next page.</Field.Description>
<Field.Error match="valueMissing">Turn this on to go on.</Field.Error>
</Field.Root>
);
}