Switch
An on/off setting that takes effect immediately.
SourceStyles source@monoline/react 0.5.0Markdown for AI toolsstable
- 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
| Prop | Type | Default | Description |
|---|---|---|---|
label | ReactNode | – | 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.
| Attribute | Stylesheet |
|---|---|
[aria-invalid="true"] | checkbox.css |
[data-orientation="horizontal"] | checkbox.css |
[data-state="checked"] | checkbox.css |
[data-state="indeterminate"] | checkbox.css |
CSS classes
| Class | Stylesheet |
|---|---|
.checkbox | checkbox.css |
.checkbox-mark | checkbox.css |
.radio | checkbox.css |
.radio-dot | checkbox.css |
.radio-group | checkbox.css |
.sw | checkbox.css |
.sw-thumb | checkbox.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
| Keys | Action |
|---|---|
| Space / Enter | Toggle |