cnippet/ui
  1. ui
  2. actions
  3. toggle

actions

Toggle

A two-state button that can be on or off. Built with Base UI and Tailwind CSS. Copy-paste ready.

API Reference

Installation

Usage

Anatomy

Good to know

  • The state props are pressed, defaultPressed and onPressedChange.
  • For a set of related toggles, use ToggleGroup instead. It adds shared state and arrow-key navigation.
  • Icon-only toggles need an aria-label.
  • Style the pressed state with the data-pressed attribute.
  • toggleVariants({ variant, size }) returns the class names, for styling an element that isn't a Toggle.

API Reference

Toggle

Styled wrapper around Base UI's Toggle primitive.

Examples

Outline Variant

The outline variant renders toggles with a visible border, making the pressed/unpressed distinction clearer against light backgrounds.

Sizes

Renders the three size variants — sm, default, and lg — for layout comparison and scale matching.

With Label and Icon

Combines a text label and a leading icon inside a single toggle, a common pattern for formatting toolbars.

Toggle with icon

An icon-only toggle. Always add aria-label so screen readers can identify the action.

With Count Badge

A toggle that carries a live badge count — useful for inbox or filter toggles where the number of affected items matters.

Icon Swap

The icon changes between two states on press, giving immediate visual confirmation that the toggle is active.

Dynamic Label

The label text updates when the toggle is pressed, making the current state explicit instead of relying on styling alone.

Video Call Controls

A call overlay card with an animated status indicator. Mic, camera, and screen-share toggles swap icons and turn destructive red when disabled. A static Leave button ends the call.

Role Filter Chips

Pill-style toggle chips with per-category badge counts. The total result count updates reactively and a "Clear" link appears only when at least one filter is active.

Bookmark Toggle

Each article row has a bookmark toggle that fills the icon when pressed. A counter below tracks how many articles are saved across the list.

Rich-Text Formatting Toolbar

Strikethrough, inline code, blockquote, and link toggles each apply markdown syntax to a live preview string below the toolbar.

Social Post Interactions

Reply, repost, like, and share toggles on a post card. The repost and like counts increment live when pressed and the heart fills with a color transition.

Server Settings

A settings card with icon-swap toggles for Secure mode, Network access, and Debug visibility. Active toggles turn green; inactive ones show the off-state icon in muted color.

Task Priority Flags

A task list where each item has a row of flag toggles for Low / Medium / High / Urgent priority. A separate checkmark toggle marks tasks as done and applies a strikethrough.