Playground

Toggle group

A row of toggles where one or several can be on.

  • 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.

AttributeStylesheet
[data-disabled]slider.css
[data-state="on"]slider.css

CSS classes

ClassStylesheet
.sliderslider.css
.slider-rangeslider.css
.slider-thumbslider.css
.slider-trackslider.css
.toggleslider.css
.toggle-groupslider.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

KeysAction
Arrow keysMove focus between items
Space / EnterToggle