Menhir

Toggle Group

View as Markdown

A set of toggle buttons that share one value, such as the text alignment buttons of an editor. By default one item is pressed at a time; with multiple, each item presses on its own. The group is one tab stop: arrow keys move focus between its items, and Enter or Space presses one. It renders role="group"; name it with aria-label or aria-labelledby. A group that is a whole toolbar, of three controls or more, passes role="toolbar", and then also gets aria-orientation when vertical.

Size
1.9 kB

Example

Styling
ToggleGroupExample.tsx
import { ToggleGroup } from "menhir/toggle-group";

const item =
  "cursor-pointer rounded-md border border-stone-300 px-3 py-1 text-sm focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-stone-500 data-pressed:border-transparent data-pressed:bg-stone-800 data-pressed:text-white dark:border-stone-700 dark:data-pressed:bg-stone-300 dark:data-pressed:text-stone-900";

export default function ToggleGroupExample() {
  return (
    <ToggleGroup.Root
      aria-label="Text alignment"
      defaultValue={["left"]}
      class="inline-flex gap-1"
    >
      <ToggleGroup.Item value="left" class={item}>
        Left
      </ToggleGroup.Item>
      <ToggleGroup.Item value="center" class={item}>
        Center
      </ToggleGroup.Item>
      <ToggleGroup.Item value="right" class={item}>
        Right
      </ToggleGroup.Item>
    </ToggleGroup.Root>
  );
}

Anatomy

<ToggleGroup.Root>
  <ToggleGroup.Item />
</ToggleGroup.Root>

API reference

Root

<div role="group">, with data-horizontal or data-vertical, and data-disabled.

PropTypeDefaultDescription
valuestring[]—The values of the pressed items, when controlled. Pair it with onValueChange. Nothing pressed is [].
defaultValuestring[][]The values of the items pressed at first, when uncontrolled.
onValueChange(value: string[]) => void—Called with the new values when the user presses or releases an item. A controlled group changes only if value does.
multiplebooleanfalseWhether several items can be pressed at once. Otherwise pressing an item releases the one pressed before.
disabledbooleanfalseWhether every item ignores the user. They cannot be focused either.
orientation"horizontal" | "vertical""horizontal"Whether the items sit side by side, moved between with Left and Right, or stacked, moved between with Up and Down.
render(props) => JSX.Element—Renders the group with your own component. Read once.

Item

<button type="button" aria-pressed>, with data-pressed or data-unpressed, and data-disabled. Name it with its content, or with aria-label when it shows only an icon. Its render function must render a <button>.

PropTypeDefaultDescription
valuestring—Identifies the item in the group's value. Required.
disabledbooleanfalseWhether the item ignores the user. Arrow keys skip it.
render(props) => JSX.Element—Renders the item with your own button component. Read once.

Keyboard

KeysAction
TabMoves focus into the group, to the item that last had focus or else the first, or out of it.
ArrowRightMoves focus to the next item, or the first from the last. Horizontal groups; the previous item in right-to-left text.
ArrowLeftMoves focus to the previous item, or the last from the first. Horizontal groups; the next item in right-to-left text.
ArrowDownMoves focus to the next item, or the first from the last. Vertical groups.
ArrowUpMoves focus to the previous item, or the last from the first. Vertical groups.
HomeMoves focus to the first item.
EndMoves focus to the last item.
SpacePresses or releases the focused item.
EnterPresses or releases the focused item.