videorc/ui

Toggle

A two-state button on Base UI at the 28 px desktop height. Pressed wears the chrome plate, the same material as a selected segment and an active tab.

Installation

$ pnpm dlx shadcn@latest add @videorc/toggle

Usage

import { Toggle } from "@/components/ui/toggle";
<Toggle aria-label="Keep on top">
  <PinIcon />
</Toggle>

Examples

Outline and compact

outline draws a hairline around the toggle, for one that sits beside fields. xs (24 px) is for dense rows only. Control it with pressed and onPressedChange when the app owns the state.

Theming

Token or attributeMeaning
chrome-plateThe pressed fill, keyed off data-pressed
bg-mutedHover. The plate means pressed, never hovered
text-muted-foreground, text-foregroundResting and pressed text
h-controlThe 28 px desktop control height
data-pressed, data-disabledBase UI's state attributes, for styling from outside

Keyboard

KeyAction
Space, EnterPresses or releases the toggle
TabMoves focus to the next control

Accessibility

Base UI renders a <button> with aria-pressed, so the state is announced on its own. An icon-only toggle needs an aria-label that stays the same in both states ("Keep on top", never "Unpin"). A toggle is for a mode the user turns on and off; choosing one value among a few is Segmented control, and a setting that applies at once is a Switch.

API reference

Every Base UI Toggle prop, plus:

Prop

Type

toggleVariants({ variant, size }) returns the classes, for an element styled as a toggle.