oakoss/ui
Components

Toggle

A button that stays pressed until it's pressed again.

SourceReact Aria

Installation

npx shadcn@latest add @oakoss/toggle

Usage

import { Toggle } from '#/components/ui/inputs/toggle';

<Toggle>Bold</Toggle>;

Examples

Sizes

size takes Button's sizes, including the square icon sizes, which require an aria-label.

Accessibility

React Aria renders a native <button> with aria-pressed, so screen readers announce it as a toggle button, pressed or not. Keep its label the same in both states; the pressed state already says whether it's on.

This component adds:

  • A clear on state: on is Button's solid neutral fill, well apart from the faint hover layer, so hover can't pass for on. In Windows High Contrast, on takes the system's selection colors, and a disabled toggle, on or off, takes the system's disabled color, as a native disabled button does.
  • Target size: a 44×44 pointer target around every size. Set targetSize={false} where toggles sit closer than that, as in a toolbar.
  • Names: icon-only sizes require a name in the type.
KeyAction
SpaceToggles it
EnterToggles it

Follows the WAI-ARIA button pattern for toggle buttons.

API

Toggle

Extends React Aria's ToggleButton.

Props

Prop

Type

Also accepts all React Aria ToggleButton props.

Data attributes

AttributePresent when
data-slottoggle, unless a wrapper passes its own
data-variantAlways; ghost or outline
data-sizeAlways; the size
data-selectedIt's on
data-hoveredA pointer hovers it
data-pressedIt's being pressed
data-focus-visibleIt has keyboard focus
data-disabledisDisabled is set

CSS variables

Takes Button's --btn-* variables at the neutral intent; --btn-bg and --btn-fg set the on fill and its text.