@biem/ui

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

switch-default.tsx
import { Field, FieldLabel } from "@biem/ui/components/ui/field";
import { Switch } from "@biem/ui/components/ui/switch";

export function SwitchDefault() {
	return (
		<div className="w-full max-w-sm">
			<Field orientation="horizontal">
				<Switch id="low-stock-alerts"/>
				<FieldLabel htmlFor="low-stock-alerts">Low stock alerts</FieldLabel>
			</Field>
		</div>
	);
}

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

Propsize
Type"sm" | "default"
Default"default"
DescriptionSets the width and the height of the switch and the size of its thumb.
Propchecked
Typeboolean
Default–
DescriptionSets whether the switch is on and makes it controlled. Update it in onCheckedChange.
PropdefaultChecked
Typeboolean
Defaultfalse
DescriptionTurns the switch on at first render. The switch then tracks its state itself.
ProponCheckedChange
Type(checked: boolean, eventDetails: SwitchRootChangeEventDetails) => void
Default–
DescriptionRuns when users turn the switch on or off. It receives the state that follows.
Propdisabled
Typeboolean
Defaultfalse
DescriptionPrevents 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.
PropreadOnly
Typeboolean
Defaultfalse
DescriptionPrevents users from turning the switch on or off. Unlike a disabled switch, it stays focusable and is submitted with the form.
Proprequired
Typeboolean
Defaultfalse
DescriptionRequires users to turn the switch on before they can submit the form.
Propname
Typestring
Default–
DescriptionIdentifies the switch when the form is submitted.
Propvalue
Typestring
Default–
DescriptionSets the value that the switch submits with the form while it is on. Without it, the switch submits "on".
PropuncheckedValue
Typestring
Default–
DescriptionSets 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.
Propform
Typestring
Default–
DescriptionSets the ID of the form that the switch belongs to. Set it when the switch is rendered outside of that form.
Propid
Typestring
Default–
DescriptionSets 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.
PropinputRef
TypeRef<HTMLInputElement>
Default–
DescriptionReceives the hidden input element that carries the value of the switch in a form.
Proprender
TypeReactElement | render function
Default–
DescriptionReplaces the native element or composes the switch with another component.
PropnativeButton
Typeboolean
Defaultfalse
DescriptionDeclares whether the rendered element is a native button. Set it to true when render returns a button.