Skip to content

Switch

Toggles a single setting on or off, with optional icons in the thumb.

import { Switch } from '@windmorph/react';

Props

By name or description, across the 39 props below

Switch 39 props

  • checkedtype boolean

    Controlled on state (checked, as an HTML checkbox).

  • defaultCheckedtype boolean

    Initial on state when uncontrolled.

  • disabledtype booleandefault false

    Turns the switch off: it can't be changed or focused.

  • readOnlytype booleandefault false

    The switch can be focused but not changed.

  • requiredtype booleandefault false

    The switch must be on (aria-required; with validationBehavior="native", the form can't submit until it is).

  • invalidtype booleandefault false

    Marks the switch invalid for assistive tech (aria-invalid).

  • iconstype booleandefault false

    Shows the default check / close icons in the thumb.

  • densitytype 0 | -1 | -2 | -3default 0

    Web density, 0 to -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 nearest ThemeProvider or ThemeScope. M3 asks for density to be the reader's choice rather than a default, so prefer setting it there, from a setting.

  • controlPlacementtype "start" | "end"default "start"

    Where the switch sits beside its label: start (before it) or end (after it, the label growing to fill the row, as a settings row's). As ListItem's controlPlacement. With end the root carries data-control-placement="end", for styling.

  • checkedIcontype ReactNode

    Icon in the thumb while checked (overrides the default check).

  • uncheckedIcontype ReactNode

    Icon in the thumb while unchecked (overrides the default close).

  • reftype Ref<HTMLLabelElement>

    The root <label>. Use inputRef for the switch input.

  • inputReftype Ref<HTMLInputElement>

    Ref to the visually hidden switch <input>.

Events

  • onCheckedChangetype ((checked: boolean) => void)

    Called when the user switches it on or off.

Text

  • aria-labeltype string

    Defines a string value that labels the current element.

  • aria-labelledbytype string

    Identifies the element (or elements) that labels the current element.

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.

Switch tokens (md-comp-switch)
VariableDefaultPaintsCompose
--md-comp-switch-selected-track-colorvar(--md-sys-color-primary)The track of a switch that is on, and its edge.SwitchTokens.SelectedTrackColor
--md-comp-switch-selected-handle-colorvar(--md-sys-color-on-primary)The handle of a switch that is on.SwitchTokens.SelectedHandleColor
--md-comp-switch-selected-icon-colorvar(--md-sys-color-on-primary-container)The icon in the handle of a switch that is on.SwitchTokens.SelectedIconColor
--md-comp-switch-unselected-track-colorvar(--md-sys-color-surface-container-highest)The track of a switch that is off.SwitchTokens.UnselectedTrackColor
--md-comp-switch-unselected-track-outline-colorvar(--md-sys-color-outline)The 2px edge of the track of a switch that is off.SwitchTokens.UnselectedTrackOutlineColor
--md-comp-switch-unselected-handle-colorvar(--md-sys-color-outline)The handle of a switch that is off.SwitchTokens.UnselectedHandleColor
--md-comp-switch-unselected-icon-colorvar(--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.

  • 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 over surface.
  • icons shows the default check / close marks; checkedIcon / uncheckedIcon override them.