Skip to content
All components
Primitives

Toggle group

A set of related toggle buttons with single or multiple selection, for switching views, alignment or filters in place.

Single or multiple selection across a set of toggles.

Installation

tdpxl is not an npm dependency of your app. The @tdpxl/cli command (not published to npm yet) copies the source into your project, with the registry items and npm packages it imports.

npx @tdpxl/cli add toggle-group

Then import it from @/components/ui/toggle-group. New project? Start with the installation guide.

Examples

Each example is one branch of the official demo, rendered in its own frame. The code is that branch as you would write it, with imports pointing at the default install paths.

Single selection

Compose a group allowing one selected value. Preserves the complete example's required parts, labels and providers. The example's local state remains owned by that composition.

"use client"

import { AlignCenterIcon, AlignLeftIcon, AlignRightIcon } from "lucide-react"

import { ToggleGroup, ToggleGroupItem } from "@/components/ui/toggle-group"

export function ToggleGroupSingleSelection() {
  return (
    <div className="flex flex-col items-center gap-4">
      <ToggleGroup type="single" aria-label="Text alignment" defaultValue="left">
        <ToggleGroupItem value="left" aria-label="Align left">
          <AlignLeftIcon className="size-4" />
        </ToggleGroupItem>
        <ToggleGroupItem value="center" aria-label="Align center">
          <AlignCenterIcon className="size-4" />
        </ToggleGroupItem>
        <ToggleGroupItem value="right" aria-label="Align right">
          <AlignRightIcon className="size-4" />
        </ToggleGroupItem>
      </ToggleGroup>
    </div>
  )
}

Multiple selection

Compose a group allowing multiple selected values. Preserves the complete example's required parts, labels and providers. The example's local state remains owned by that composition.

"use client"

import { ToggleGroup, ToggleGroupItem } from "@/components/ui/toggle-group"

export function ToggleGroupMultipleSelection() {
  return (
    <div className="flex flex-col items-center gap-4">
      <ToggleGroup type="multiple" aria-label="Status filters" defaultValue={["draft"]}>
        <ToggleGroupItem value="draft">Draft</ToggleGroupItem>
        <ToggleGroupItem value="review">In review</ToggleGroupItem>
        <ToggleGroupItem value="shipped">Shipped</ToggleGroupItem>
      </ToggleGroup>
    </div>
  )
}

Stroke

Switch between views with small stroke items that combine icon and text. variant and size set on the group apply to every item.

"use client"

import { CalendarIcon, KanbanIcon, ListIcon } from "lucide-react"

import { ToggleGroup, ToggleGroupItem } from "@/components/ui/toggle-group"

export function ToggleGroupStroke() {
  return (
    <div className="flex flex-col items-center gap-4">
      <ToggleGroup type="single" variant="stroke" size="sm" aria-label="Project view" defaultValue="list">
        <ToggleGroupItem value="list">
          <ListIcon />
          List
        </ToggleGroupItem>
        <ToggleGroupItem value="board">
          <KanbanIcon />
          Board
        </ToggleGroupItem>
        <ToggleGroupItem value="calendar">
          <CalendarIcon />
          Calendar
        </ToggleGroupItem>
      </ToggleGroup>
    </div>
  )
}

Always selected

Keep one option selected by ignoring the empty value. A single group can otherwise be cleared by pressing the selected item again.

"use client"

import * as React from "react"

import { ToggleGroup, ToggleGroupItem } from "@/components/ui/toggle-group"

function DensityPicker() {
  const [density, setDensity] = React.useState("comfortable")

  return (
    <div className="flex flex-col items-center gap-2">
      <ToggleGroup
        type="single"
        aria-label="Row density"
        value={density}
        onValueChange={(next) => {
          // Single toggle groups can be cleared; ignore the empty value to keep one option selected.
          if (next) setDensity(next)
        }}
      >
        <ToggleGroupItem value="compact">Compact</ToggleGroupItem>
        <ToggleGroupItem value="comfortable">Comfortable</ToggleGroupItem>
        <ToggleGroupItem value="spacious">Spacious</ToggleGroupItem>
      </ToggleGroup>
      <p className="text-paragraph-sm text-sub-600">Density: {density}</p>
    </div>
  )
}

export function ToggleGroupAlwaysSelected() {
  return (
    <div className="flex flex-col items-center gap-4">
      <DensityPicker />
    </div>
  )
}

Vertical

Stack options vertically with matching arrow-key navigation. orientation stacks the items and switches the arrow keys together.

"use client"

import { ToggleGroup, ToggleGroupItem } from "@/components/ui/toggle-group"

export function ToggleGroupVertical() {
  return (
    <div className="flex flex-col items-center gap-4">
      <ToggleGroup
        type="single"
        orientation="vertical"
        aria-label="Sort order"
        defaultValue="newest"
      >
        <ToggleGroupItem value="newest">Newest first</ToggleGroupItem>
        <ToggleGroupItem value="oldest">Oldest first</ToggleGroupItem>
        <ToggleGroupItem value="name">By name</ToggleGroupItem>
      </ToggleGroup>
    </div>
  )
}

API reference

Generated from the component's TypeScript signatures. Inherited DOM and primitive props are listed separately.

ToggleGroup

Coordinate single or multiple selection across related toggle actions.

ToggleGroup props
PropTypeDefaultDescription
valuestring | string[] | undefined—The controlled stateful value of the item that is pressed. The controlled stateful value of the items that are pressed.
defaultValuestring | string[] | undefined—The value of the item that is pressed when initially rendered. Use defaultValue if you do not need to control the state of a toggle group. The value of the items that are pressed when initially rendered. Use defaultValue if you do not need to control the state of a toggle group.
onValueChange((value: string) => void) | ((value: string[]) => void) | undefined—The callback that fires when the value of the toggle group changes. The callback that fires when the state of the toggle group changes.
disabledboolean | undefined—Whether the group is disabled from user interaction.
rovingFocusboolean | undefined—Whether the group should maintain roving focus of its buttons.
inputMode"search" | "text" | "none" | "tel" | "url" | "email" | "numeric" | "decimal" | undefined—Hints at the type of data that might be entered by the user while editing the element or its contents
aria-describedbystring | undefined—Identifies the element (or elements) that describes the object.
aria-invalidboolean | "false" | "true" | "grammar" | "spelling" | undefined—Indicates the entered value does not conform to the format expected by the application.
aria-labelstring | undefined—Defines a string value that labels the current element.
aria-labelledbystring | undefined—Identifies the element (or elements) that labels the current element.
childrenReact.ReactNode—Represents all of the things React can render.
size"sm" | "md" | "lg" | null | undefined—Visual size of this part. Choose a supported size while preserving readable content and usable interaction targets.
variant"stroke" | "ghost" | null | undefined—Visual treatment shared with Toggle. Selection mode and selected values are separate from appearance.
Inherited props (17)
ToggleGroup inherited props
PropTypeDefaultDescription
type(required)"single" | "multiple"—Selection mode: single allows one value; multiple allows an array of selected values.
loopboolean | undefined—Allow keyboard navigation to wrap between the first and last eligible items.
orientation("horizontal" | "vertical") | undefined—Horizontal or vertical layout/navigation axis as defined by this primitive.
dir("ltr" | "rtl") | undefined—Reading direction used by the primitive. Keep it consistent with the surrounding content and Direction provider.
classNamestring | undefined—Additional classes for this part. Preserve contrast, visible focus and responsive content when adapting styles.
defaultCheckedboolean | undefined—Initial checked state for a control supporting uncontrolled selection. Inherited DOM typing does not give a layout wrapper checkbox behavior.
autoFocusboolean | undefined—Request automatic focus when the element mounts or the primitive opens, according to its source behavior. Choose initial focus deliberately in context.
idstring | undefined—Stable DOM identifier used by labels, descriptions or other explicit associations.
roleReact.AriaRole | undefined—Semantic role appropriate to the rendered element. Preserve required relationships and keyboard behavior.
onFocusReact.FocusEventHandler<HTMLDivElement> | undefined—Receive the native focus event. Preserve the component's existing keyboard and focus behavior.
onBlurReact.FocusEventHandler<HTMLDivElement> | undefined—Receive the native blur event when focus leaves this control.
onChangeReact.ChangeEventHandler<HTMLDivElement, Element> | undefined—Receive the native change event; read the actual control value or checked state from its event target.
onKeyDownReact.KeyboardEventHandler<HTMLDivElement> | undefined—Receive keyboard events. Preserve built-in keyboard behavior unless deliberately implementing an accessible alternative.
onClickReact.MouseEventHandler<HTMLDivElement> | undefined—Receive activation events. The consumer owns the action performed and its result.
onSelectReact.ReactEventHandler<HTMLDivElement> | undefined—Handle the selection event defined by this part's source signature. DOM text-selection events differ from menu or command activation callbacks.
asChildboolean | undefined—Merge this part into one child instead of its default element. Preserve that child's semantics, ref support and accessibility attributes.
refReact.Ref<HTMLDivElement> | undefined—Allows getting a ref to the component instance. Once the component unmounts, React will set ref.current to null (or call the ref with null if you passed a callback ref).

ToggleGroupItem

One item within its parent composition. Keep parent context and the value, label or state required by the source API.

ToggleGroupItem props
PropTypeDefaultDescription
inputMode"search" | "text" | "none" | "tel" | "url" | "email" | "numeric" | "decimal" | undefined—Hints at the type of data that might be entered by the user while editing the element or its contents
aria-describedbystring | undefined—Identifies the element (or elements) that describes the object.
aria-invalidboolean | "false" | "true" | "grammar" | "spelling" | undefined—Indicates the entered value does not conform to the format expected by the application.
aria-labelstring | undefined—Defines a string value that labels the current element.
aria-labelledbystring | undefined—Identifies the element (or elements) that labels the current element.
childrenReact.ReactNode—Represents all of the things React can render.
value(required)string—A string value for the toggle group item. All items within a toggle group should use a unique value.
size"sm" | "md" | "lg" | null | undefined—Visual size of this part. Choose a supported size while preserving readable content and usable interaction targets.
variant"stroke" | "ghost" | null | undefined—Visual treatment shared with Toggle. Selection mode and selected values are separate from appearance.
Inherited props (18)
ToggleGroupItem inherited props
PropTypeDefaultDescription
formstring | undefined—ID of the form associated with this control, including when it is rendered outside that form element.
classNamestring | undefined—Additional classes for this part. Preserve contrast, visible focus and responsive content when adapting styles.
defaultCheckedboolean | undefined—Initial checked state for a control supporting uncontrolled selection. Inherited DOM typing does not give a layout wrapper checkbox behavior.
defaultValuestring | number | readonly string[] | undefined—Initial value for a control supporting uncontrolled state. Inherited DOM typing alone does not give a layout wrapper input behavior.
autoFocusboolean | undefined—Request automatic focus when the element mounts or the primitive opens, according to its source behavior. Choose initial focus deliberately in context.
idstring | undefined—Stable DOM identifier used by labels, descriptions or other explicit associations.
roleReact.AriaRole | undefined—Semantic role appropriate to the rendered element. Preserve required relationships and keyboard behavior.
onFocusReact.FocusEventHandler<HTMLButtonElement> | undefined—Receive the native focus event. Preserve the component's existing keyboard and focus behavior.
onBlurReact.FocusEventHandler<HTMLButtonElement> | undefined—Receive the native blur event when focus leaves this control.
onChangeReact.ChangeEventHandler<HTMLButtonElement, Element> | undefined—Receive the native change event; read the actual control value or checked state from its event target.
onKeyDownReact.KeyboardEventHandler<HTMLButtonElement> | undefined—Receive keyboard events. Preserve built-in keyboard behavior unless deliberately implementing an accessible alternative.
onClickReact.MouseEventHandler<HTMLButtonElement> | undefined—Receive activation events. The consumer owns the action performed and its result.
onSelectReact.ReactEventHandler<HTMLButtonElement> | undefined—Handle the selection event defined by this part's source signature. DOM text-selection events differ from menu or command activation callbacks.
asChildboolean | undefined—Merge this part into one child instead of its default element. Preserve that child's semantics, ref support and accessibility attributes.
disabledboolean | undefined—Make the supported control unavailable through its native or primitive disabled semantics. This does not disable arbitrary child links.
namestring | undefined—Name used by the control's form submission or primitive form integration.
type"button" | "submit" | "reset" | undefined—Selection mode: single allows one value; multiple allows an array of selected values.
refReact.Ref<HTMLButtonElement> | undefined—Allows getting a ref to the component instance. Once the component unmounts, React will set ref.current to null (or call the ref with null if you passed a callback ref).

Accessibility & usage

From registry/ui/toggle-group.meta.json (authored guidance; human review may still be pending). Behavior is checked by Storybook tests, not certified here.

When to use

  • Single selection that applies immediately: text alignment, list or board view, row density.
  • Multiple selection of quick filters or formatting options that combine.
  • A compact segmented control in a toolbar or above a list, with two to five options.

When not to use

  • A form field whose value is submitted later and must always have one answer; a radio group is clearer and works with labels and validation.
  • Switching between panels of different content; tabs manage the panel relationship.
  • Independent settings that are not related to each other.

Anatomy

ToggleGroup
Radix ToggleGroup root with type single or multiple, value or defaultValue, onValueChange and orientation. Its variant and size are shared with every item.
ToggleGroupItem
One option: a button with a unique value. Can override variant and size. Pressed items get a raised surface.

Keyboard & interaction

  • The group is one Tab stop; arrow keys move focus between items (Left/Right when horizontal, Up/Down when vertical), with Home and End to jump.
  • Enter or Space presses the focused item; in single mode it replaces the selection, in multiple mode it adds or removes the item.
  • Arrow keys follow the reading direction; in right-to-left contexts Left and Right are mirrored when a DirectionProvider or dir is set.
  • Disabled items are skipped by arrow navigation; disabled on the root disables every item.
  • Selection applies immediately and fires onValueChange; the consumer owns what happens next.

Responsive behavior

  • Items do not wrap; keep two to five short options. At narrow widths shorten labels, switch to icon-only items with names, or use a select.
  • orientation="vertical" stacks the items and switches the arrow keys to Up/Down, so the visual order always matches keyboard order.
  • Item sizes are 32, 36 or 40 CSS pixels tall and meet the 24x24 minimum (WCAG 2.5.8); on touch prefer lg or more spacing.
  • Check reflow at 320 CSS pixels: the group must not force horizontal page scrolling.

Content guidelines

  • Name the group for what it controls: 'Text alignment', 'Project view', 'Status filters'.
  • Use parallel, short labels of similar length: 'List', 'Board', 'Calendar'.
  • Icon-only items need aria-label with the option name ('Align left').
  • When the selection must always exist, start with a sensible default value instead of an empty selection.

Alternatives

  • Radio group — A mutually exclusive form choice with labels and validation.
  • Tabs — Switching between content panels.
  • Toggle — A single standalone pressed option.
  • Select — Many options, or options with long labels that would not fit side by side.

Resources