Toggle group
A row of toggles where one or several can be on.
SourceStyles source@monoline/react 0.4.0Markdown for AI toolsstable
- Built on radix-ui/ToggleGroup
- 2 parts, named exports
- single type is role=radiogroup with radio items; multiple type is a group of toggle buttons.
- Arrow keys move focus between items; Space and Enter toggle.
- Give the group an aria-label.
- Styles in one file:
slider.css
Import
import '@monoline/css';
import {ToggleGroup} from '@monoline/react';
Not installed yet? Pick a route.
Copies the source and its styles into your project.
npx monoline add toggle-group
It needs radix-ui; install it if your project does not have it:
npm install radix-ui
Usage
import {ToggleGroup, ToggleGroupItem} from '@monoline/react';
<ToggleGroup type="single" defaultValue="30" aria-label="Range">
<ToggleGroupItem value="7">7 days</ToggleGroupItem>
<ToggleGroupItem value="30">30 days</ToggleGroupItem>
<ToggleGroupItem value="365">Year</ToggleGroupItem>
</ToggleGroup>
Anatomy
Import the parts you need and nest them like this.
import {ToggleGroup, ToggleGroupItem} from '@monoline/react';
<ToggleGroup>
<ToggleGroupItem />
</ToggleGroup>
Examples
Single
Data Attributes
State is exposed through attributes. Style it with these selectors, not with class toggles.
| Attribute | Stylesheet |
|---|---|
[data-disabled] | slider.css |
[data-state="on"] | slider.css |
CSS classes
| Class | Stylesheet |
|---|---|
.slider | slider.css |
.slider-range | slider.css |
.slider-thumb | slider.css |
.slider-track | slider.css |
.toggle | slider.css |
.toggle-group | slider.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
.slider {
border-color: var(--accent);
}
.slider[data-disabled] {
background: var(--wash);
}
Accessibility
- single type is role=radiogroup with radio items; multiple type is a group of toggle buttons.
- Arrow keys move focus between items; Space and Enter toggle.
- Give the group an aria-label.
Keyboard
| Keys | Action |
|---|---|
| Arrow keys | Move focus between items |
| Space / Enter | Toggle |