Button group

Slant / Controls

Related buttons spaced apart or attached into one bar, sharing a size and variant.

Demo

Usage

@tuttn/slant
import { Button, ButtonGroup, Input } from '@tuttn/slant'

// Spaced: a set of related actions, named for assistive technology.
<ButtonGroup label="Draft actions">
  <Button>Discard</Button>
  <Button variant="primary">Publish</Button>
</ButtonGroup>

// Attached: one bar with shared borders. Size and variant reach every button,
// unless a button sets its own.
<ButtonGroup attached variant="primary" label="Merge">
  <Button>Merge</Button>
  <ButtonGroup.Separator />
  <Button icon aria-label="More merge options"><ChevronDownIcon /></Button>
</ButtonGroup>

// A text cell and an input join the bar too.
<ButtonGroup attached size="sm" label="Pages">
  <Button icon aria-label="Previous page"><ChevronLeftIcon /></Button>
  <ButtonGroup.Text>2 of 5</ButtonGroup.Text>
  <Button icon aria-label="Next page"><ChevronRightIcon /></Button>
</ButtonGroup>
<ButtonGroup attached label="Share link">
  <Input readOnly defaultValue="tuttn.io/t/24" aria-label="Link" />
  <Button>Copy</Button>
</ButtonGroup>

// A column.
<ButtonGroup attached orientation="vertical" size="sm" label="Sort">…</ButtonGroup>

Props

PropTypeDefaultDescription
labelstring—The group's accessible name, for a set of related actions such as "Text alignment".
attachedbooleanfalseJoins the buttons into one bar with shared borders, instead of spacing them apart.
orientation'horizontal' | 'vertical''horizontal'A row of buttons, or a column.
size'sm' | 'md' | 'lg'—The size every button inside takes, unless it sets its own.
variant'primary' | 'secondary' | 'ghost'—The variant every button inside takes, unless it sets its own.
ButtonGroup.SeparatorHTMLAttributes<HTMLDivElement>—A hairline between buttons (role="separator", aria-orientation across the group). In an attached bar it sits on the shared border, which matters between solid primary buttons.
ButtonGroup.Text size'sm' | 'md' | 'lg'the group's sizeA static, non-focusable cell drawn in a button's box, for a count ("1 of 5") or a unit; it takes the attached corners like a button.
…restHTMLAttributes—className, style, other attributes and the ref go on each part's own element: the role="group" div, the separator div and the text span.

The group is a role="group" element named by label; give one whenever the buttons belong to a single task ("Text alignment", "Pages"), so a screen reader announces it on the way in. Every button keeps its own tab stop: for a set of options with one tab stop and arrow keys, use a ToggleGroup instead, which is built on the same context.

Inside a group, a Button's size and variant default to the group's, and in an attached group it renders data-attached with the group's orientation, which squares its inner corners and overlaps the borders it shares. An Input does the same, so a field can sit between buttons. A hovered or focused member rises above its neighbours so its whole border and focus ring show.