Switch
A control that lets users turn a setting on or off.
A switch lets users turn one setting on or off. Its thumb sits at one end of the track while the setting is off and moves to the other end when users turn it on, and the track changes its color with it. The switch has no label of its own, so the examples name it with a FieldLabel whose htmlFor refers to the id of the switch. A click on the label then changes the switch just as a click on the switch does.
Usage
Examples
Small
Dense settings lists and table rows leave little room beside each label. With size set to "sm", the switch takes less room, so many settings fit into a compact list without crowding the content around them.
Default Size
Most switches stand in forms, dialogs and settings pages, where they need no size of their own. When size is left out, the switch takes the value "default", which keeps every setting at the same scale across Biem products.
Checked by Default
Some settings suit most users from the start, such as a daily summary of sales by email. Through defaultChecked, the switch starts on, so users act only when the setting does not fit their work.
Controlled
Often the application needs to know a setting the moment it changes, for example to show the options that belong to it. With checked and onCheckedChange, the application holds the state of the switch, so the switch can also reflect a value from saved preferences or from another part of the interface.
Disabled
A setting can depend on another one, such as a rounding rule for prices that only applies while automatic pricing is on. Once disabled is set, the switch stays in view while users can no longer turn it on or off, and it exposes this state to assistive technology. Its value is left out when the form is submitted.
Invalid
Some settings cannot be saved together with others, and users need to see at once which switch stands in the way. When validation fails, aria-invalid marks the switch as invalid, on screen as well as for assistive technology.
API Reference
| Prop | size |
|---|---|
| Type | "sm" | "default" |
| Default | "default" |
| Description | Sets the width and the height of the switch and the size of its thumb. |
| Prop | checked |
|---|---|
| Type | boolean |
| Default | – |
| Description | Sets whether the switch is on and makes it controlled. Update it in onCheckedChange. |
| Prop | defaultChecked |
|---|---|
| Type | boolean |
| Default | false |
| Description | Turns the switch on at first render. The switch then tracks its state itself. |
| Prop | onCheckedChange |
|---|---|
| Type | (checked: boolean, eventDetails: SwitchRootChangeEventDetails) => void |
| Default | – |
| Description | Runs when users turn the switch on or off. It receives the state that follows. |
| Prop | disabled |
|---|---|
| Type | boolean |
| Default | false |
| Description | Prevents users from turning the switch on or off and exposes the disabled state to assistive technology. A disabled switch submits no value with the form. |
| Prop | readOnly |
|---|---|
| Type | boolean |
| Default | false |
| Description | Prevents users from turning the switch on or off. Unlike a disabled switch, it stays focusable and is submitted with the form. |
| Prop | required |
|---|---|
| Type | boolean |
| Default | false |
| Description | Requires users to turn the switch on before they can submit the form. |
| Prop | name |
|---|---|
| Type | string |
| Default | – |
| Description | Identifies the switch when the form is submitted. |
| Prop | value |
|---|---|
| Type | string |
| Default | – |
| Description | Sets the value that the switch submits with the form while it is on. Without it, the switch submits "on". |
| Prop | uncheckedValue |
|---|---|
| Type | string |
| Default | – |
| Description | Sets the value that the switch submits with the form while it is off. Without it, a switch that is off submits nothing. It has no effect without name. |
| Prop | form |
|---|---|
| Type | string |
| Default | – |
| Description | Sets the ID of the form that the switch belongs to. Set it when the switch is rendered outside of that form. |
| Prop | id |
|---|---|
| Type | string |
| Default | – |
| Description | Sets the ID of the hidden input, so that a label can refer to the switch with htmlFor. With nativeButton, the ID goes to the rendered button instead. |
| Prop | inputRef |
|---|---|
| Type | Ref<HTMLInputElement> |
| Default | – |
| Description | Receives the hidden input element that carries the value of the switch in a form. |
| Prop | render |
|---|---|
| Type | ReactElement | render function |
| Default | – |
| Description | Replaces the native element or composes the switch with another component. |
| Prop | nativeButton |
|---|---|
| Type | boolean |
| Default | false |
| Description | Declares whether the rendered element is a native button. Set it to true when render returns a button. |