Skip to content
Thrum

Toggle group

A set of toggles that share one value: text alignment, a view, the series a chart shows. One item is pressed at a time, or several with multiple, and a single choice can be cleared by pressing it again. When exactly one option must always be selected, use the segmented control.

Install

npx shadcn@latest add https://thrumui.dev/r/toggle-group.json

Adds components/ui/toggle-group.tsx and installs @base-ui/react@1.9.0. Nothing else in your project changes.

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

Props

PropTypeDefaultDescription
ToggleGroup: valuereadonly string[]–The values of the pressed items, when the parent holds them. A list in both modes: empty, or one value, without multiple.
ToggleGroup: defaultValuereadonly string[]Default []The values pressed at first, when the group holds its own state.
ToggleGroup: onValueChange(value: string[]) => void–Called with the new list when an item is pressed or released.
ToggleGroup: multiplebooleanDefault falseLet several items be pressed at once. Without it, pressing one releases the other.
ToggleGroup: variant"default" | "outline"Default "default"default keeps the items 4 pixels apart. outline joins them into one framed shape.
ToggleGroup: size"sm" | "md" | "lg"Default "md"The height of every item: 28, 32 or 40 pixels.
ToggleGroup: orientation"horizontal" | "vertical"Default "horizontal"The direction the items run in, and which arrow keys move between them.
ToggleGroup: loopFocusbooleanDefault trueWhether the arrow keys wrap from the last item to the first.
ToggleGroup: disabledbooleanDefault falseDisables every item.
ToggleGroup: soundbooleanDefault truePlay a soft click when the selection changes, if the visitor has sound on.
ToggleGroup: classNamestring–Extra classes for the group, merged last.
ToggleGroupItem: valuestringRequiredWhat the group's value holds while this item is pressed. Unique within the group.
ToggleGroupItem: disabledbooleanDefault falseDimmed, skipped by the arrow keys, and cannot be changed.
ToggleGroupItem: ...propsComponentProps<"button">–Every other button attribute is passed through. An item with only an icon needs an aria-label.

Motion and sound

Motion

  • An item's background, border and text colour change over 150 ms when it is pressed or released.
  • A press scales the item to 97% and back over 150 ms.
  • Reduced motion: colours and scale change at once.

Sound

  • A soft click when an item is pressed or released. The items themselves are silent, so one press makes one sound.

Sound is off until the visitor turns it on. Pass sound={false} to keep this component silent. How sound works

Accessibility

  • The group has the group role and each item is a button with aria-pressed. Name the group with an aria-label.
  • Focus moves with the arrow keys and the value changes only on Enter or Space, so a reader can look along the items without changing anything.
  • Pressed is the primary fill with the primary text colour against an empty box. Two pressed neighbours in a joined group keep a line between them.
  • In a container narrower than its items, default items wrap onto more lines. Joined outline items shrink and clip their labels: keep them short, or set the group vertical.
KeysAction
TabMove into the group, onto one item, and out again. The group is one tab stop.
← / →Move to the previous or next item in a horizontal group. Past the end it wraps. In right-to-left text the keys swap.
↑ / ↓Move to the previous or next item in a vertical group.
Home / EndMove to the first or last item.
Enter / SpacePress or release the focused item. Moving does not change the value.

Source

components/ui/toggle-group.tsx
"use client";

import { createContext, useContext } from "react";
import { ToggleGroup as ToggleGroupPrimitive } from "@base-ui/react/toggle-group";

import { cn } from "@/lib/utils";
import { play } from "@/lib/thrum-sound";
import { Toggle, type ToggleProps, type ToggleSize, type ToggleVariant } from "@/components/ui/toggle";

type Look = { variant: ToggleVariant; size: ToggleSize };
const LookContext = createContext<Look>({ variant: "default", size: "md" });

export type ToggleGroupProps = ToggleGroupPrimitive.Props & {
  /** The look of every item. `default` keeps the items apart; `outline` joins them into one framed shape. */
  variant?: ToggleVariant;
  /** The size of every item: `sm` is 28 pixels tall, `md` 32, `lg` 40. */
  size?: ToggleSize;
  /** Play a soft click when the selection changes, if the visitor has sound on. */
  sound?: boolean;
};

/**
 * A set of toggles that share one value: one of them pressed at a time, or several with `multiple`.
 * The value is always a list of the pressed items' values. It is one tab stop, and the arrow keys move inside it.
 * Name the group with an `aria-label`.
 */
export function ToggleGroup({ variant = "default", size = "md", sound = true, className, onValueChange, ...props }: ToggleGroupProps) {
  return (
    <LookContext.Provider value={{ variant, size }}>
      <ToggleGroupPrimitive
        data-slot="toggle-group"
        onValueChange={(value, details) => {
          if (sound) play("select");
          onValueChange?.(value, details);
        }}
        className={cn(
          "inline-flex max-w-full data-[orientation=vertical]:flex-col",
          variant === "default"
            ? "flex-wrap gap-1"
            : // Joined: one frame, square inner corners, and each border laid over its neighbour's. The focused item comes to the front.
              // Items give way in a narrow container instead of pushing out of it.
              "[&>*]:min-w-0 [&>*]:shrink [&>*]:overflow-hidden [&>*]:rounded-none [&>*:focus-visible]:z-10 " +
                "data-[orientation=horizontal]:[&>*:first-child]:rounded-s-md data-[orientation=horizontal]:[&>*:last-child]:rounded-e-md data-[orientation=horizontal]:[&>*:not(:first-child)]:-ms-px " +
                "data-[orientation=vertical]:[&>*:first-child]:rounded-t-md data-[orientation=vertical]:[&>*:last-child]:rounded-b-md data-[orientation=vertical]:[&>*:not(:first-child)]:-mt-px " +
                // Two pressed neighbours keep a line between them, so they read as two items.
                "data-[orientation=horizontal]:[&>[data-pressed]+[data-pressed]]:border-s-primary-foreground/40 data-[orientation=vertical]:[&>[data-pressed]+[data-pressed]]:border-t-primary-foreground/40",
          className,
        )}
        {...props}
      />
    </LookContext.Provider>
  );
}

export type ToggleGroupItemProps = Omit<ToggleProps, "variant" | "size" | "sound" | "value"> & {
  /** What the group's value holds while this item is pressed. Unique within the group. */
  value: string;
};

/** One toggle of the group. It takes its look from the group. With only an icon inside, give it an `aria-label`. */
export function ToggleGroupItem({ className, ...props }: ToggleGroupItemProps) {
  const { variant, size } = useContext(LookContext);
  // Silent: the group plays one sound for the change.
  return <Toggle data-slot="toggle-group-item" variant={variant} size={size} sound={false} className={className} {...props} />;
}

Files and dependencies

Adds
components/ui/toggle-group.tsx
Packages
@base-ui/react@1.9.0
Brings along
Sound engineToggle

Changelog

  1. 1.0.0 · 2026-12-03First release.

Loading index…

Keyboard shortcuts

/Ctrl KSearch components, docs and actions
?Show this list
AAppearance studio
SSound on or off
MFull or reduced motion
TTheme: auto, light, dark
GSurface: solid or frosted
BBackdrop photograph (under glass)
CCorner radius: square to round
RReplay the demo, on a component page
DDraw new data, on a component page
←→HomeEndMove the cursor in a focused chart
EscClose a dialog, or leave a chart cursor