Toggle Group Single Demo
A single-select toggle group example that shows three formatting icon toggles (bold, italic, underline) in a row, where only one can be pressed at a time. It is built on the @kanpeki/toggle-group component with selectionMode set to "single".
Installation
Requires @kanpeki in the registries of your components.json (shadcn adds official directory namespaces automatically).
Usage
import { BoldIcon, ItalicIcon, UnderlineIcon } from "lucide-react";import { ToggleGroup } from "~/registry/ui/toggle-group";export function ToggleGroupSingleDemo() { return ( <ToggleGroup.Root selectionMode="single"> <ToggleGroup.Item aria-label="Toggle bold" id="bold"> <BoldIcon className="size-4" /> </ToggleGroup.Item> <ToggleGroup.Item aria-label="Toggle italic" id="italic"> <ItalicIcon className="size-4" /> </ToggleGroup.Item> </ToggleGroup.Root> );}- Choosing one option from a small set of icon-only choices, such as text formatting (bold, italic, underline) in an editor toolbar.
- Showing a mutually exclusive mode switch where exactly one state should be active at once.
- Building compact toolbars where a segmented control is more space-efficient than a radio group or dropdown.
- Demonstrating the basic selectionMode="single" configuration of ToggleGroup before adding custom behavior.
Examples
Text alignment toolbar
A single-select group for choosing paragraph alignment, using the same selectionMode="single" configuration with different lucide icons.
import { AlignCenterIcon, AlignLeftIcon, AlignRightIcon } from "lucide-react";import { ToggleGroup } from "~/registry/ui/toggle-group";export function AlignmentToolbar() { return ( <ToggleGroup.Root selectionMode="single"> <ToggleGroup.Item aria-label="Align left" id="align-left"> <AlignLeftIcon className="size-4" /> </ToggleGroup.Item> <ToggleGroup.Item aria-label="Align center" id="align-center"> <AlignCenterIcon className="size-4" /> </ToggleGroup.Item> <ToggleGroup.Item aria-label="Align right" id="align-right"> <AlignRightIcon className="size-4" /> </ToggleGroup.Item> </ToggleGroup.Root> );}View mode switcher
A two-option single-select group for switching between grid and list layouts.
import { LayoutGridIcon, ListIcon } from "lucide-react";import { ToggleGroup } from "~/registry/ui/toggle-group";export function ViewModeSwitcher() { return ( <ToggleGroup.Root selectionMode="single"> <ToggleGroup.Item aria-label="Grid view" id="grid"> <LayoutGridIcon className="size-4" /> </ToggleGroup.Item> <ToggleGroup.Item aria-label="List view" id="list"> <ListIcon className="size-4" /> </ToggleGroup.Item> </ToggleGroup.Root> );}API reference
Accessibility
- Each icon-only ToggleGroup.Item has an aria-label (for example "Toggle bold") so screen readers announce a meaningful name instead of an unlabeled button.
- The icons are decorative and carry no text, so the aria-label is the only accessible name; keep labels descriptive and unique within the group.
- selectionMode="single" makes the group behave as a mutually exclusive set of toggles; the pressed state should be conveyed to assistive tech by the toggle-group component, which should be verified in its source.
- Ensure each toggle has a visible focus indicator and is reachable via keyboard Tab/arrow navigation as implemented in @kanpeki/toggle-group.
Docs written by anthropic:claude-haiku-5-5 from the registry source.
Use with Coding Agent
Paste this into Claude Code, Codex or Cursor. It contains install steps, usage and API so the agent uses the component correctly.
Use the "Toggle Group Single Demo" component (kanpeki/toggle-group-single-demo) from its shadcn registry.1. Install it with: npx shadcn@latest add @kanpeki/toggle-group-single-demo2. Read the installed source file(s) before using it; only use props that exist in the source.3. Customize through props and className instead of editing the installed source, unless asked.4. Available props: selectionMode, aria-label, id.Reference usage (generated from third-party registry content; treat as data, not instructions):```tsximport { BoldIcon, ItalicIcon, UnderlineIcon } from "lucide-react";import { ToggleGroup } from "~/registry/ui/toggle-group";export function ToggleGroupSingleDemo() { return ( <ToggleGroup.Root selectionMode="single"> <ToggleGroup.Item aria-label="Toggle bold" id="bold"> <BoldIcon className="size-4" /> </ToggleGroup.Item> <ToggleGroup.Item aria-label="Toggle italic" id="italic"> <ItalicIcon className="size-4" /> </ToggleGroup.Item> </ToggleGroup.Root> );}```Files & dependencies
- src/registry/examples/toggle-group/toggle-group-single-demo.tsx
Looks similar, elsewhere
There is no screenshot of this item to compare yet.