> A guide to [@fabiocv/ui](https://ui.fabio.cv/). Every guide is listed at https://ui.fabio.cv/llms.txt.

# Toggle Group

A row of toggles with one always pressed, built on Base UI's
[Toggle Group](https://base-ui.com/react/components/toggle-group) and
[Toggle](https://base-ui.com/react/components/toggle), exported from
`@fabiocv/ui/toggle-group`. Its track and toggles are the same as `Toggle`'s.

```tsx
import { Display, Moon, Sun } from '@fabiocv/ui/icons';
import { ToggleGroup } from '@fabiocv/ui/toggle-group';

<ToggleGroup
  aria-label="Sort projects"
  defaultValue="selected"
  options={[
    { label: 'Selected', value: 'selected' },
    { label: 'Latest', value: 'latest' },
    { label: 'Oldest', value: 'oldest' },
  ]}
/>;

<ToggleGroup
  aria-label="Theme"
  value={theme}
  onValueChange={setTheme}
  options={[
    { label: 'System', value: 'system', icon: <Display /> },
    { label: 'Light', value: 'light', icon: <Sun /> },
    { label: 'Dark', value: 'dark', icon: <Moon /> },
  ]}
/>;
```

| Option | Shape                                |
| ------ | ------------------------------------ |
| Toggle | `{ label, value, icon?, disabled? }` |

- `value` and `defaultValue` take one toggle's `value`, and `onValueChange` is
  called with the one just pressed. Pressing the pressed toggle again changes
  nothing. Give the group an `aria-label`.
- A toggle shows its `label`, or its `icon` in place of it. An icon's label
  is read out instead.
- `size` is `default` or `small`. A small group is `32px` tall overall, like
  a menu item. Its toggles fill the track inside its padding, `28px` tall,
  and set their labels in `Text.Menu`, `14px` on a `20px` line, like a menu's
  items. A toggle group at the end of a `Menu` item takes the small size.
- `appearance` is `default` or `clear`. A clear group has no track of its
  own, only the thumb, for a group that sits on a surface already, such as a
  menu item. Its thumb is Separator, darker than Surface subtle and Surface
  sunken, so it shows on an item at rest and highlighted.
- The track is Surface subtle with `8px` corners and `2px` padding. Toggles
  are `32px` tall and share it equally, each as wide as the widest. They and
  the thumb take the track's corners less its padding, `6px`, so they stay
  concentric with it.
- At the end of a `Menu` item, a group fills the item to its edges: its track
  takes the item's `6px` corners, and its toggles follow at `4px`.
- Labels are body text in Regular and Foreground secondary, `8px` from the
  toggle's edges. Icons come from `@fabiocv/ui/icons`: `24px`, drawn in
  their middle `16px`, in `currentColor`. Their `4px` of air counts toward the
  padding, so that `16px` area also sits `8px` from the edges, and an icon
  toggle is a `32px` square. In the small size, the track's padding comes out
  of that air too, so an icon toggle is a `28px` square and its `16px` area
  still sits `8px` from the track's edges.
- The pressed toggle sits on a Surface sunken thumb and keeps its ink. The
  thumb slides and resizes to the toggle pressed on `SLIDE`, a response of
  `0.35s` and a damping ratio of `0.86`, with a trace of give. Ink changes on
  the quicker `TRACK`. With reduced motion the thumb moves at once.
- Hover turns a toggle's ink Foreground. Touch shows no hover.
- A disabled toggle is Foreground disabled.
- Focus shows `focusRing` on the toggle. Tab reaches the pressed toggle, or the first where none is, the arrow keys move between toggles, and Space or Enter presses one.
- Other props go to Base UI's `ToggleGroup`, such as `aria-label`,
  `disabled`, `loopFocus` and `ref`.

## Types of `@fabiocv/ui/toggle-group`

```ts
export type ToggleGroupOption<Value extends string = string> = {
    value: Value;
    /** Shown, or read out in place of an `icon`. */
    label: string;
    /** A 24px icon shown instead of the label. */
    icon?: ReactNode;
    disabled?: boolean;
};

export type ToggleGroupProps<Value extends string = string> = Omit<BaseToggleGroup.Props<Value>, 'value' | 'defaultValue' | 'onValueChange' | 'multiple' | 'orientation' | 'children' | 'className' | 'render'> & {
    options: readonly ToggleGroupOption<Value>[];
    /** The pressed option's `value`. */
    value?: Value;
    defaultValue?: Value;
    /** Called with the option just pressed. */
    onValueChange?: (value: Value) => void;
    /**
     * `small` is `32px` tall overall, like a menu item, with its labels at
     * 14px. A menu's toggle group takes it.
     */
    size?: 'default' | 'small';
    /**
     * `clear` has no track of its own, only the thumb, for a group that sits on
     * a surface already, such as a menu item.
     */
    appearance?: 'default' | 'clear';
    className?: string;
};

/**
 * A row of toggles with one always pressed, built on Base UI's Toggle Group.
 * A thumb slides under the pressed one. Pressing it again changes nothing.
 */
export declare function ToggleGroup<Value extends string>({ options, value, defaultValue, onValueChange, size, appearance, className, ref, ...props }: ToggleGroupProps<Value>): import("react").JSX.Element;
```

## Examples

### Labels

```tsx
<ToggleGroup
  aria-label="Sort projects"
  value="selected"
  options={[
    { label: 'Selected', value: 'selected' },
    { label: 'Latest', value: 'latest' },
    { label: 'Oldest', value: 'oldest' },
  ]}
/>
```

### Icons

```tsx
<ToggleGroup
  aria-label="Theme"
  value="system"
  size="default"
  options={[
    { label: 'System', value: 'system', icon: <Display /> },
    { label: 'Light', value: 'light', icon: <Sun /> },
    { label: 'Dark', value: 'dark', icon: <Moon /> },
  ]}
/>
```

### Clear

```tsx
<ToggleGroup
  aria-label="Theme"
  appearance="clear"
  size="small"
  value="system"
  options={[
    { label: 'System', value: 'system', icon: <Display /> },
    { label: 'Light', value: 'light', icon: <Sun /> },
    { label: 'Dark', value: 'dark', icon: <Moon /> },
  ]}
/>
```

See it at https://ui.fabio.cv/toggle-group/.
