Switch
Turns a setting on or off, taking effect as soon as it changes.
Usage
import { CheckboxLabel } from 'nyctoui/checkbox'
import { Switch } from 'nyctoui/switch'
<CheckboxLabel>
<Switch defaultChecked />
Thumbs rating
</CheckboxLabel>Use a switch for a setting that applies at once, and a Checkbox for a choice saved with a form. Keep its label the same in both states: the label names the setting, and the switch shows whether it’s on.
The track fills with the brand ink when on. The knob stays light while off and turns to the page’s ink when on, so it stands out on the near-white track in dark mode too.
Good for
- Turning notifications on or off in app settings.
- Enabling two-factor sign-in on a security page.
- Turning a feature flag on for a team in an admin console.
- Turning dark mode or autoplay on in a media app.
Examples
Settings
In a Panel, each row’s title names its switch through aria-labelledby.
Sizes
md is 28 × 16, a step above 14px text. sm is 24 × 14, for dense tables. Both have a 24px-tall hit area.
Disabled
Keyboard
Space turns the focused switch on or off. Enter submits its form.
Best practices
- Use a switch for settings that apply at once, with no Save.
- Label it with the setting, like “Email me on mentions”, so on and off both make sense.
- Show the result straight away, and undo the switch if saving fails.
- Use a switch inside a form that only applies on submit. Use a checkbox.
- Use a switch for more than two states. Use a segmented control.
API reference
Switch
Wraps ui-sdk’s Switch and its thumb, and takes its props, including readOnly, required and controller.
| Prop | Type | Default |
|---|---|---|
checkedWhether it’s on. Pass it to control the switch. | boolean | — |
defaultCheckedWhether it starts on, when uncontrolled. | boolean | false |
onCheckedChangeRuns when it wants to change. details.cancel() keeps it as it is. | (checked: boolean, details) => void | — |
sizeTrack size. | 'sm' | 'md' | 'md' |
nameThe name it submits under while on. | string | — |
disabledCan’t be focused or changed, and isn’t submitted. | boolean | false |
sxStyleX styles merged after the built-in ones. | StyleXStyles | — |