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

# Toggle

A button that stays pressed, built on Base UI's
[Toggle](https://base-ui.com/react/components/toggle), exported from
`@fabiocv/ui/toggle`. It is a `ToggleGroup`'s toggle on a track of its
own.

```tsx
import { Grid, Sound } from '@fabiocv/ui/icons';
import { Toggle } from '@fabiocv/ui/toggle';

<Toggle
  label="Grid"
  icon={<Grid empty={!grid} />}
  pressed={grid}
  onPressedChange={setGrid}
/>;

<Toggle
  label="Sound"
  icon={<Sound muted={!sound} />}
  pressed={sound}
  onPressedChange={setSound}
/>;
```

- `pressed` and `defaultPressed` say whether it is pressed, and
  `onPressedChange` is called with the new state. Pressing it again lets it
  go.
- It shows its `label`, or its `icon` in place of it. An icon's label is read
  out instead.
- `size` is `default` or `small`, as for a `ToggleGroup`. A small toggle is
  `32px` tall overall, like a menu item, and sets its label in `Text.Menu`.
  With an icon it is a `28px` square on its track. A toggle at the end of a
  `Menu` item takes the small size.
- `appearance` is `default` or `clear`. The default sits on a Surface subtle
  track with `8px` corners and `2px` padding, and fills Surface sunken while
  pressed. A clear toggle has no track of its own, for one that sits on a
  surface already, such as a menu item, and fills Separator, so it shows on
  an item at rest and highlighted.
- The toggle takes the track's corners less its padding, `6px`, so they stay
  concentric. At the end of a `Menu` item the track's corners are `6px`, so the
  toggle takes `4px`.
- Its ink is Foreground secondary, and Foreground while hovered. Pressed, it
  keeps its ink and fills. The ink and the fill change on `TRACK`. Disabled,
  it is Foreground disabled.
- Focus shows `focusRing` on the toggle, and Space or Enter presses it.
- `className` goes to the track. Other props go to Base UI's `Toggle`, such
  as `disabled` and `ref`.

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

```ts
export type ToggleProps = Omit<BaseToggle.Props, 'value' | 'children' | 'className' | 'render'> & {
    /** Shown, or read out in place of an `icon`. */
    label: string;
    /** A 24px icon shown instead of the label. */
    icon?: ReactNode;
    /**
     * `small` is `32px` tall overall, like a menu item, with its label at 14px.
     * A menu's toggle takes it.
     */
    size?: 'default' | 'small';
    /**
     * `clear` has no track of its own, for a toggle that sits on a surface
     * already, such as a menu item.
     */
    appearance?: 'default' | 'clear';
    /** Given to the track. */
    className?: string;
};

/**
 * A button that stays pressed, built on Base UI's Toggle: a toggle group's
 * toggle on a track of its own. It fills while pressed, and pressing it again
 * lets it go.
 */
export declare function Toggle({ label, icon, size, appearance, className, ...props }: ToggleProps): import("react").JSX.Element;
```

## Examples

### Label

```tsx
<Toggle label="Grid" pressed={true} />
```

### Icon

```tsx
<Toggle label="Grid" icon={<Grid />} defaultPressed />
<Toggle label="Sound" icon={<Sound />} />
```

### Sizes

```tsx
<Toggle size="default" label="Default" defaultPressed />
<Toggle size="small" label="Small" defaultPressed />
```

### Clear

```tsx
<Toggle
  appearance="clear"
  size="small"
  label="Grid"
  icon={<Grid />}
  defaultPressed
/>
```

### Disabled

```tsx
<Toggle label="Grid" disabled={true} />
```

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