Skip to main content
← All Demos

Switch

Switch toggles a setting between on and off states.

1. Basic Switch (Uncontrolled)

Click or press Space/Enter to toggle. Screen readers announce "on" or "off" via role="switch".

Keyboard: Tab to focus → Space or Enter to toggle
<Field id='basic-switch'>
  <Field.Label>Enable feature</Field.Label>
  <Switch />
</Field>

2. Controlled Switch

Parent manages the checked state. External controls can change the value.

const [checked, setChecked] = useState(false);

<Field id='controlled-switch'>
  <Field.Label>Dark mode</Field.Label>
  <Switch
    checked={checked}
    onChange={(nextChecked) => setChecked(nextChecked)}
  />
</Field>

3. Default Checked

Switch that starts in the "on" state.

<Field id='default-checked-switch'>
  <Field.Label>Notifications</Field.Label>
  <Switch defaultChecked />
</Field>

4. Disabled Switch

Disabled switches cannot be toggled. Screen readers announce the disabled state.

Expected: Tab should skip disabled switches.
<Field id='disabled-off' disabled>
  <Field.Label>Disabled (off)</Field.Label>
  <Switch />
</Field>

<Field id='disabled-on' disabled>
  <Field.Label>Disabled (on)</Field.Label>
  <Switch defaultChecked />
</Field>

5. Read-only Switch

Focusable but state cannot be changed. Uses aria-readonly.

<Field id='readonly-switch' readOnly>
  <Field.Label>Read-only (on)</Field.Label>
  <Switch defaultChecked />
</Field>

6. Required Switch

Switch marked as required for form validation. Uses aria-required.

<Field id='required-switch' required>
  <Field.Label>Agree to terms</Field.Label>
  <Switch />
</Field>
← All Demos