Playground

Switch

An on/off setting that takes effect immediately.

  • Built on radix-ui/Switch
  • role=switch with aria-checked; Space and Enter toggle.
  • A visible label is clickable; without one, aria-label is required.
  • Styles in one file: checkbox.css

Import

import '@monoline/css';
import {Switch} from '@monoline/react';

Not installed yet? Pick a route.

Copies the source and its styles into your project.

npx monoline add switch

It needs radix-ui; install it if your project does not have it:

npm install radix-ui

Usage

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

<div style={{display: 'grid', gap: 'var(--s3)'}}>
  <Switch label="Launch at login" defaultChecked />
  <Switch label="Weekly digest" />
  <Switch label="Locked" disabled />
</div>

Examples

Basic

Props

SwitchProps

PropTypeDefaultDescription
labelReactNode–Visible label. Without it, give the switch an `aria-label`.

Data Attributes

State is exposed through attributes. Style it with these selectors, not with class toggles.

AttributeStylesheet
[aria-invalid="true"]checkbox.css
[data-orientation="horizontal"]checkbox.css
[data-state="checked"]checkbox.css
[data-state="indeterminate"]checkbox.css

CSS classes

ClassStylesheet
.checkboxcheckbox.css
.checkbox-markcheckbox.css
.radiocheckbox.css
.radio-dotcheckbox.css
.radio-groupcheckbox.css
.swcheckbox.css
.sw-thumbcheckbox.css

Customization

Style it with the kit's tokens in your own CSS, loaded after @monoline/css. Prefer the attributes above over extra classes. See Theming for every token.

app.css
.checkbox {
  border-color: var(--accent);
}
.checkbox[aria-invalid="true"] {
  background: var(--wash);
}

Try the props

Change a prop and the component above updates; the code below is what you would write. To edit the code itself, use the Playground.

example.tsx
<Switch label="Airplane mode" />

Accessibility

  • role=switch with aria-checked; Space and Enter toggle.
  • A visible label is clickable; without one, aria-label is required.

Keyboard

KeysAction
Space / EnterToggle