Skip to content

Switch

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

NameTypeRequiredDefaultDescription
checkedbooleanNo

Controlled checked (on/off) state.

defaultCheckedbooleanNofalse

Initial checked state for uncontrolled usage (also the value a form reset restores).

onCheckedChange(checked: boolean) => voidNo

Called with the new checked state when the switch is toggled (not for no-op updates).

disabledbooleanNo

Whether the switch is disabled and non-interactive.

disabledFocusablebooleanNofalse

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 aria-disabled="true", data-disabled and data-disabled-focusable on the switch instead of the native disabled attribute. The switch cannot be toggled and is not submitted with its form; your onClick is not called, and a click on the switch (or Space and Enter) does not reach ancestor click handlers, as for a natively disabled control. Wins over disabled when both are set. In a roving container (a Toolbar) it stays in the arrow-key order.

labelReact.ReactNodeNo

Label next to the control (any phrasing content, links included, but no other form controls). It names the control through aria-labelledby, after a consumer or Field aria-labelledby; a consumer aria-label names it instead. Clicking its text toggles the control; clicking a link inside it follows the link. children are not rendered: pass the label here.

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.

namestringNo

Form field name. With a name, the switch takes part in native form submission: it submits name=value while on.

valuestringNo'on'

Value submitted while on.

requiredbooleanNo

The form must not be submitted while the switch is off (native validation).

formstringNo

Id of the form the switch belongs to, when it is rendered outside that form.

onClickReact.MouseEventHandler<HTMLButtonElement>No

Click handler of the switch control (the role="switch" button).

onFocusReact.FocusEventHandler<HTMLButtonElement>No

Focus handler of the switch control.

onBlurReact.FocusEventHandler<HTMLButtonElement>No

Blur handler of the switch control.

onKeyDownReact.KeyboardEventHandler<HTMLButtonElement>No

Keydown handler of the switch control.

onKeyUpReact.KeyboardEventHandler<HTMLButtonElement>No

Keyup handler of the switch control.

refReact.Ref<HTMLButtonElement>No

Ref to the focusable role="switch" button (Fluent UI's ref, on its primary element). The root <label> is slotProps.root.ref.

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 root, indicator and label slot props): your classes after the part's (yours win a conflict), your styles over its styles, your handlers before its handlers (preventDefault() in yours skips the Switch's handling of the event, its own on… prop included), your ref beside its ref, and your ids added to its aria-labelledby and aria-describedby. Every attribute the Switch sets on a part is locked and keeps the Switch's value (set it through the Switch's own props); you add the attributes it does not set. A part takes no children.

Any other prop is passed to the element it renders (HTMLLabelElement).

Parts

  • root
  • track
  • thumb
  • label

Stable class names

PartClass
rootwave-switch
trackwave-switch__track
thumbwave-switch__thumb
labelwave-switch__label