Toggle Group

A row of toggles sharing one value, pressed by a person or an agent.

Source
$npx @dinachi/cli@latest add toggle-group
multiple
orientation
variant
size
disabled

Usage

import { ToggleGroup, ToggleGroupItem } from "@/components/ui/toggle-group"
import { AlignCenter, AlignLeft, AlignRight } from "lucide-react"

export function Alignment() {
  return (
    <ToggleGroup aria-label="Alignment" defaultValue={["left"]}>
      <ToggleGroupItem value="left" aria-label="Align left">
        <AlignLeft className="h-4 w-4" />
      </ToggleGroupItem>
      <ToggleGroupItem value="center" aria-label="Align center">
        <AlignCenter className="h-4 w-4" />
      </ToggleGroupItem>
      <ToggleGroupItem value="right" aria-label="Align right">
        <AlignRight className="h-4 w-4" />
      </ToggleGroupItem>
    </ToggleGroup>
  )
}

Examples

One item pressed at a time.
import { ToggleGroup, ToggleGroupItem } from '@/components/ui/toggle-group';
import { AlignLeft, AlignCenter, AlignRight } from 'lucide-react';

export function Example() {
  return (
    <ToggleGroup defaultValue={["center"]}>
      <ToggleGroupItem value="left" aria-label="Align left">
        <AlignLeft className="h-4 w-4" />
      </ToggleGroupItem>
      <ToggleGroupItem value="center" aria-label="Align center">
        <AlignCenter className="h-4 w-4" />
      </ToggleGroupItem>
      <ToggleGroupItem value="right" aria-label="Align right">
        <AlignRight className="h-4 w-4" />
      </ToggleGroupItem>
    </ToggleGroup>
  );
}

Notes

Defaults

Items default to variant="outline" and size="sm"; a standalone Toggle defaults to default for both.

Single mode

Without multiple, pressing an item replaces the value; pressing the pressed item clears it. Read value[0].

API

valuestring[]
Controlled pressed values. Always an array.
defaultValuestring[] = []
Starting values when uncontrolled.
onValueChange
agent
(value, details) => void
Runs for people and agents. details.cancel() refuses the change.
multiple
agent
boolean = false
Allows several pressed items. Agents also get setValue.
disabled
agent
boolean = false
Blocks people and agents.
orientation"horizontal" | "vertical" = "horizontal"
Which arrow keys move focus. Vertical also stacks the items.
loopFocusboolean = true
Wraps focus from the last item to the first.

Agent

Inside an AgentProvider, it appears in agent.snapshot() as toggle-group.

Reads

label

The group's aria-label.

value

The pressed item's label, or a list of labels when multiple.

items

Every item's label.

multiple

Present when several items can be pressed.

disabled

True when the whole group is blocked.

Actions

select("Align center")

Picks by label, then by value. Replaces in single mode, adds in multiple. A miss lists the items.

setValue(["Toggle bold", "Toggle italic"])

Replaces the whole value. Only when multiple.

An agent's change runs through onValueChange. isAgentChange(details) is true for it, and details.cancel() refuses it; the agent is told the app refused. A disabled item and a disabled group refuse on their own.

await agent.act({
  steps: [{ ref: "toggle-group", action: "select", args: ["Align right"] }],
})

Accessibility

  • The group has role group; each item is a button with aria-pressed.
  • Name the group with aria-label. Without one the agent lists it as toggle-group.
  • Icon-only items each need an aria-label. The agent reads these as item names.
  • Disabled items stay in the group but cannot be pressed.