Switch
The Material 3 switch on a native checkbox. It turns something on at once, such as a card on the overview, and works before any script has loaded.
- Native HTML
- 1.7 KB gzipped · script and styles
@orbex/sonar-components/switch
Examples
Shows 1.96K instead of 1,960 where space is short.
Code
import { Switch } from '@orbex/sonar-components/switch'
export default function Examples() {
return (
<>
<Switch label="Daily shipments" defaultChecked />
<Switch label="Compact figures" description="Shows 1.96K instead of 1,960 where space is short." />
<Switch label="Recurring orders" disabled />
</>
)
}
Sizes, variants and states
Usage
- For settings that apply immediately. For a choice that is submitted later, use Checkbox.
- Name what is turned on, not the action: “Daily shipments”, not “Show daily shipments”.
Do and don’t
Accessibility
Keyboard
| Key | What it does |
|---|---|
| Tab | Moves focus to the switch. |
| Space | Turns it on or off. |
Roles and attributes
| Element or attribute | What it gives |
|---|---|
<input type="checkbox" role="switch"> |
Announced as a switch, on or off; still a checkbox in forms and without JavaScript. |
<label> |
Wraps the input, so the label names it and clicking the label toggles it. |
aria-describedby |
Ties the help text to the input without adding it to the name. |
Notes
- A native checkbox with
role="switch": announced as a switch, on or off, toggled with Space. - The help text sits outside the label, so it describes the switch without lengthening its name.
- On touch screens the row grows to 48 px.
Use it
Each component is its own entry point with its own stylesheet, so an app loads only what it uses. The tokens and the cascade layers are set once for the app: see Installation.
import { Switch } from '@orbex/sonar-components/switch'
import '@orbex/sonar-components/switch.css'
API
| Prop | Type | Default |
|---|---|---|
label
Required
What the switch turns on. |
ReactNode
|
— |
description
One line on the effect, read after the label. |
ReactNode
|
— |
Also takes every attribute of the native <input>
, except type | role, and a ref to it.