Playground

Toggle

A button that stays pressed or not.

  • Built on radix-ui/Toggle
  • A button with aria-pressed; Space and Enter toggle it.
  • Icon-only toggles need an aria-label.
  • Styles in one file: slider.css

Import

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

Not installed yet? Pick a route.

Copies the source and its styles into your project.

npx monoline add toggle

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

npm install radix-ui

Usage

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

<div style={{display: 'flex', gap: 'var(--s3)'}}>
  <Toggle>Bold</Toggle>
  <Toggle defaultPressed>Italic</Toggle>
  <Toggle disabled>Locked</Toggle>
</div>

Examples

Basic

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

  • A button with aria-pressed; Space and Enter toggle it.
  • Icon-only toggles need an aria-label.

Keyboard

KeysAction
Space / EnterToggle it