Skip to content

ToggleGroup

A row of toggle buttons that act on the same thing: several at once, like text styles, or one at a time with single, like an alignment.

import { ToggleGroup } from '@sypnia/ui/react'

On this page
  1. Several at once, and one at a time
  2. With words
  3. When to use it
  4. For everyone
  5. Props

Several at once, and one at a time

Code
import { useState } from 'react';
import { ToggleGroup } from '@sypnia/ui/react';

// Several at once (text styles) and one at a time (alignment). Arrow keys move inside each group.
export default function Editor() {
  const [styles, setStyles] = useState(['bold']);
  const [align, setAlign] = useState(['left']);
  return (
    <div className="sy-row">
      <ToggleGroup label="Text style" iconOnly value={styles} onChange={setStyles}
        items={[{ id: 'bold', label: 'Bold', icon: 'bold' }, { id: 'italic', label: 'Italic', icon: 'italic' }, { id: 'code', label: 'Code', icon: 'code' }]} />
      <ToggleGroup label="Alignment" single iconOnly value={align} onChange={setAlign}
        items={[{ id: 'left', label: 'Left', icon: 'align-left' }, { id: 'center', label: 'Center', icon: 'align-center' }, { id: 'right', label: 'Right', icon: 'align-right' }]} />
    </div>
  );
}

With words

On a phone, a group of five or more goes two by two.

Code
import { useState } from 'react';
import { ToggleGroup } from '@sypnia/ui/react';

// With words: which levels of log lines to show. On a phone a long group goes two by two.
export default function WithLabels() {
  const [levels, setLevels] = useState(['error', 'warn']);
  return (
    <ToggleGroup label="Levels" size="sm" value={levels} onChange={setLevels}
      items={[{ id: 'debug', label: 'Debug' }, { id: 'info', label: 'Info' }, { id: 'warn', label: 'Warning' }, { id: 'error', label: 'Error' }, { id: 'fatal', label: 'Fatal' }]} />
  );
}

When to use it

Use it when

  • Formatting tools in an editor.
  • Filters that combine, such as the levels of log lines to show.

Do not use it when

  • Choosing a view or a mode: use SegmentedControl.
  • Choices in a form: use a CheckboxGroup or a RadioGroup.

For everyone

  • It is one stop for the Tab key; the arrow keys move between its buttons.
  • With single it is a radio group: one is always chosen.
  • Icon-only buttons take their labels as names.

Props

ToggleGroupTypeDefault
itemsrequired ToggleItem[]
labelrequired string
onChangerequired (value: string[]) => void
valuerequired

The ids that are pressed.

string[]
iconOnly

Show only the icons; the labels become their names.

boolean
single

Only one can be pressed at a time, and one always is.

boolean
size "sm" | "md" | "lg"
  1. Several at once, and one at a time
  2. With words
  3. When to use it
  4. For everyone
  5. Props

    Search pages, sections and terms