Switch
Toggles a single setting on or off, with optional icons in the thumb.
import { Switch } from '@windmorph/react';Props
Switch 39 props
Controlled on state (
checked, as an HTML checkbox).Initial on state when uncontrolled.
Turns the switch off: it can't be changed or focused.
The switch can be focused but not changed.
The switch must be on (
aria-required; withvalidationBehavior="native", the form can't submit until it is).Marks the switch invalid for assistive tech (
aria-invalid).Shows the default check / close icons in the thumb.
Web density,
0to-3: each step takes 4px off the round state layer around the thumb (40px at 0, 32px at -2 and -3, so it still shows around a pressed 28px thumb); the track stays 52 × 32px and the touch target 48px. Left out, the switch takes the density of the nearestThemeProviderorThemeScope. M3 asks for density to be the reader's choice rather than a default, so prefer setting it there, from a setting.Where the switch sits beside its label:
start(before it) orend(after it, the label growing to fill the row, as a settings row's). AsListItem'scontrolPlacement. Withendthe root carriesdata-control-placement="end", for styling.Icon in the thumb while checked (overrides the default check).
Icon in the thumb while unchecked (overrides the default close).
The root
<label>. UseinputReffor the switch input.Ref to the visually hidden switch
<input>.
Text
Defines a string value that labels the current element.
Identifies the element (or elements) that labels the current element.
Classes for the root
<label>, merged withclassNames.root; consumer classes win.Classes for each part, by slot.
classNames.rootis merged withclassName.The thumb, the circle that slides along the track.
The icon inside the thumb.
The round state layer around the thumb (hover, focus and press).
The root
<label>that holds the control and its label text.The 40px control that carries the state layer and holds the indicator.
The label text beside the control.
Inline styles for the root
<label>.
The value of the input element, used when submitting an HTML form. See MDN.
Whether to use native HTML form validation to prevent form submission when the value is missing or invalid, or mark the field as required or invalid via ARIA.
A function that returns an error message if a given value is invalid. Validation errors are displayed to the user when the form is submitted if
validationBehavior="native". For realtime validation, use theinvalidprop instead.Whether the element should receive focus on render.
Handler that is called when the element receives focus.
Handler that is called when the element loses focus.
Handler that is called when the element's focus status changes.
Handler that is called when a key is pressed.
Handler that is called when a key is released.
The name of the input element, used when submitting an HTML form. See MDN.
The
<form>element to associate the input with. The value of this attribute must be the id of a<form>in the same document. See MDN.The element's unique identifier. See MDN.
Identifies the element (or elements) that describes the object.
Identifies the element (or elements) that provide a detailed, extended description for the object.
Identifies the element that provides an error message for the object.
Handler that is called when the press is released over the target.
Handler that is called when a press interaction starts.
Handler that is called when a press interaction ends, either over the target or when the pointer leaves the target.
Handler that is called when the press state changes.
Handler that is called when a press is released over the target, regardless of whether it started on the target or not.
Also accepts the root element's HTML attributes (id, data-*, event handlers).
Tokens
M3's component tokens as CSS variables. Each is unset until you set it, on :root, a theme or any ancestor, and falls back to the default shown. The root carries data-variant (and data-size where it has sizes), which picks the variables that apply. See Component tokens in the Customisation guide.
| Variable | Default | Paints | Compose |
|---|---|---|---|
--md-comp-switch-selected-track-color | var(--md-sys-color-primary) | The track of a switch that is on, and its edge. | SwitchTokens.SelectedTrackColor |
--md-comp-switch-selected-handle-color | var(--md-sys-color-on-primary) | The handle of a switch that is on. | SwitchTokens.SelectedHandleColor |
--md-comp-switch-selected-icon-color | var(--md-sys-color-on-primary-container) | The icon in the handle of a switch that is on. | SwitchTokens.SelectedIconColor |
--md-comp-switch-unselected-track-color | var(--md-sys-color-surface-container-highest) | The track of a switch that is off. | SwitchTokens.UnselectedTrackColor |
--md-comp-switch-unselected-track-outline-color | var(--md-sys-color-outline) | The 2px edge of the track of a switch that is off. | SwitchTokens.UnselectedTrackOutlineColor |
--md-comp-switch-unselected-handle-color | var(--md-sys-color-outline) | The handle of a switch that is off. | SwitchTokens.UnselectedHandleColor |
--md-comp-switch-unselected-icon-color | var(--md-sys-color-surface-container-highest) | The icon in the handle of a switch that is off. | SwitchTokens.UnselectedIconColor |
Spec status
Built to the M3 Expressive switch spec. Each item of its variants and configurations links to a live example; where the M3 Expressive column of the spec differs from the original M3 one, it wins.
| Item | Status | Details |
|---|---|---|
| Without icons | Built | Configuration · Basic |
| Icon on selected switch | Built | Configuration · Icons |
| Icon on selected and unselected switch | Built | Configuration · Icons |
| Disabled | Built | Configuration · States |
| Turn a single setting on or off | Built | Use case · Basic |
| Density (web, chosen by the reader) | Built | Use case · Density |
Bundle size
Gzipped, as published: the component and the library code it imports. React, React Aria, Motion and the other dependencies aren't counted, and your bundler's minifier makes it smaller still. CI fails a change that grows it past its budget.
Switch: 14.7 kB
Differences from Compose
- A 52×32px track with a 2px outline. The thumb geometry is computed like Compose's
ThumbNode: 16px off, 24px on or with an icon, 28px pressed, centred 16 / 36px from the start. - The thumb can be dragged: it follows the pointer between its off and on positions, and releasing sets the switch from the half it's in (a tap still toggles).
- Pressing snaps the thumb; releasing animates its
width/height/ position on the fast spatial spring, and it mirrors in RTL. The geometry is passed as CSS variables so no state selectors compete. - Hover, focus and press recolour the thumb to
on-surface-variant(off) /primary-container(on); disabled colours are Compose's composites oversurface. iconsshows the default check / close marks;checkedIcon/uncheckedIconoverride them.