Switch
An on/off toggle drawn as a role="switch" button inside a <label>.
- ref reaches the control; the root <label> receives className, style and data-* (slotProps.root.ref reaches it); the control receives id, aria-label/-labelledby/-describedby/-invalid/-required/-errormessage/ -details/-disabled, tabIndex, autoFocus and the click, focus and key handlers. A consumer onClick runs once per activation, before the toggle; preventDefault() in it cancels the toggle.
- Inside a Field it is named by the Field label (followed by its own label text) and described by the Field message and hint.
- With name (or required) it takes part in native forms like <input type="checkbox">, and a form reset restores defaultChecked.
- The thumb position mirrors when the switch itself is right-to-left (its nearest dir), so a switch in an LTR subtree of an RTL page keeps the LTR layout.
- label takes rich content (a link) and renders after the switch, or before or above it with labelPosition (the root carries data-label-position). Beside the label, the switch lines up with the first line of a label that wraps or has a second line. children are not rendered (a development warning says so).
- size (small, medium) sets the track and thumb dimensions; the root carries data-size. Switch does not read a surrounding Field's size or a WaveProvider's input defaults.
Examples
Default
export const Default: Story = {};Checked
export const Checked: Story = {
args: {
defaultChecked: true,
},
};With Label
export const WithLabel: Story = {
args: {
label: 'Dark mode',
},
};Disabled
export const Disabled: Story = {
args: {
label: 'Disabled switch',
disabled: true,
},
};Without Visible Label
export const WithoutVisibleLabel: Story = {
args: {
label: undefined,
'aria-label': 'Airplane mode',
},
};In Form
export const InForm: Story = {
args: {
name: 'notifications',
value: 'on',
},
render: (args) => (
<form className="flex flex-col items-start gap-3" onSubmit={(e) => e.preventDefault()}>
<Switch {...args} />
<Button type="submit" appearance="primary">
Save
</Button>
</form>
),
};Label Positions
export const LabelPositions: Story = {
render: (args) => (
<div className="flex w-80 flex-col gap-6">
<div className="flex flex-col gap-3">
<Switch {...args} label="Wi-Fi" labelPosition="before" className="flex justify-between" />
<Switch
{...args}
label="Bluetooth"
labelPosition="before"
className="flex justify-between"
defaultChecked
/>
<Switch
{...args}
label="Airplane mode"
labelPosition="before"
className="flex justify-between"
/>
</div>
<Switch {...args} label="Label above" labelPosition="above" />
<Switch {...args} label="Label after (default)" />
</div>
),
};Disabled Focusable
Managed by your organization.
export const DisabledFocusable: Story = {
args: {
label: 'Automatic updates',
disabledFocusable: true,
defaultChecked: true,
'aria-describedby': 'updates-reason',
},
render: (args) => (
<div className="flex flex-col items-start gap-1">
<Switch {...args} />
<p id="updates-reason" className="text-wave-caption-1 text-wave-muted-foreground">
Managed by your organization.
</p>
</div>
),
};Sizes
export const Sizes: Story = {
render: (args) => (
<div className="grid grid-cols-2 gap-3">
{(['small', 'medium'] as const).flatMap((size) =>
[false, true].map((on) => (
<Switch
{...args}
key={`${size}-${on}`}
size={size}
defaultChecked={on}
label={`${size} ${on ? 'on' : 'off'}`}
/>
)),
)}
</div>
),
};Props
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
checked | boolean | No | Controlled checked (on/off) state. | |
defaultChecked | boolean | No | false | Initial checked state for uncontrolled usage (also the value a form reset restores). |
onCheckedChange | (checked: boolean) => void | No | Called with the new checked state when the switch is toggled (not for no-op updates). | |
disabled | boolean | No | Whether the switch is disabled and non-interactive. | |
disabledFocusable | boolean | No | false | Marks the switch unavailable but keeps it focusable and in the tab order: for a disabled switch that needs a Tooltip, or one in a toolbar. Renders |
label | React.ReactNode | No | Label next to the control (any phrasing content, links included, but no other form controls). It names the control through | |
labelPosition | 'before' | 'after' | 'above' | No | 'after' | Where the label renders: after the control (default), before it, or above it. The DOM order follows the visual order. |
size | 'small' | 'medium' | No | 'medium' | Size of the track: the 0.7 40x20 track with a 14px thumb, or a small 32x16 track with a 10px thumb and a 24px-tall pointer target. Unlike the text controls and pickers, Switch does not read a surrounding Field's size or a WaveProvider's input defaults: this prop is the only source of its size. |
name | string | No | Form field name. With a name, the switch takes part in native form submission: it submits | |
value | string | No | 'on' | Value submitted while on. |
required | boolean | No | The form must not be submitted while the switch is off (native validation). | |
form | string | No | Id of the form the switch belongs to, when it is rendered outside that form. | |
onClick | React.MouseEventHandler<HTMLButtonElement> | No | Click handler of the switch control (the | |
onFocus | React.FocusEventHandler<HTMLButtonElement> | No | Focus handler of the switch control. | |
onBlur | React.FocusEventHandler<HTMLButtonElement> | No | Blur handler of the switch control. | |
onKeyDown | React.KeyboardEventHandler<HTMLButtonElement> | No | Keydown handler of the switch control. | |
onKeyUp | React.KeyboardEventHandler<HTMLButtonElement> | No | Keyup handler of the switch control. | |
ref | React.Ref<HTMLButtonElement> | No | Ref to the focusable | |
slotProps | { root?: SlotPart<'label'>; track?: SlotPart<'button'>; thumb?: SlotPart<'span'>; label?: SlotPart<'span'> } | No | Props for the elements the Switch draws, by part, merged into each part's own (Fluent UI's |
Any other prop is passed to the element it renders (HTMLLabelElement).
Parts
roottrackthumblabel
Stable class names
| Part | Class |
|---|---|
root | wave-switch |
track | wave-switch__track |
thumb | wave-switch__thumb |
label | wave-switch__label |