# Toggle button

A button that stays pressed or not, alone or in a group where one or several options are on.

- Family: action
- Import: `import { ToggleButton, ToggleGroup } from "@senvenli/react/action";`
- Page: https://design.senvenli.com/docs/components/toggle-button
- JSON: https://design.senvenli.com/docs/components/toggle-button.json

## Example: Filters that combine

A group where any number of buttons can be on.

```tsx
import { ToggleButton, ToggleGroup } from "@senvenli/react/action";

export default function Example() {
  return (
    <ToggleGroup label="Order status" defaultSelectedKeys={["open", "overdue"]}>
      <ToggleButton id="open">Open</ToggleButton>
      <ToggleButton id="paid">Paid</ToggleButton>
      <ToggleButton id="overdue">Overdue</ToggleButton>
      <ToggleButton id="refunded">Refunded</ToggleButton>
    </ToggleGroup>
  );
}
```

## Examples

### One choice

With `selectionMode="single"` the group behaves as a radio group.

```tsx
import { ToggleButton, ToggleGroup } from "@senvenli/react/action";

export default function Example() {
  return (
    <ToggleGroup label="Text alignment" selectionMode="single" defaultSelectedKeys={["left"]}>
      <ToggleButton id="left" icon="align-left">
        Left
      </ToggleButton>
      <ToggleButton id="center" icon="align-center">
        Center
      </ToggleButton>
      <ToggleButton id="right" icon="align-right">
        Right
      </ToggleButton>
    </ToggleGroup>
  );
}
```

### A single toggle, controlled

The label stays the same; only the pressed state changes.

```tsx
import { ToggleButton } from "@senvenli/react/action";
import { Text } from "@senvenli/react/display";
import { Stack } from "@senvenli/react/layout";
import { useState } from "react";

export default function Example() {
  const [isPinned, setPinned] = useState(false);
  return (
    <Stack direction="horizontal" space="space-150">
      <ToggleButton icon="pin" isSelected={isPinned} onChange={setPinned}>
        Pin project
      </ToggleButton>
      <Text tone="subtle">{isPinned ? "Pinned to the sidebar" : "Not pinned"}</Text>
    </Stack>
  );
}
```

### Compact and disabled

Compact for dense toolbars; a disabled toggle keeps its state.

```tsx
import { ToggleButton, ToggleGroup } from "@senvenli/react/action";

export default function Example() {
  return (
    <ToggleGroup label="Formatting" defaultSelectedKeys={["underline"]}>
      <ToggleButton id="bold" icon="bold" size="small">
        Bold
      </ToggleButton>
      <ToggleButton id="italic" icon="italic" size="small">
        Italic
      </ToggleButton>
      <ToggleButton id="underline" icon="underline" size="small" isDisabled>
        Underline
      </ToggleButton>
    </ToggleGroup>
  );
}
```

## Guidelines

### When to use it

- Turning a formatting or view option on and off.
- Picking options from a short group (ToggleGroup).

### Use something else when

- [switch](https://design.senvenli.com/docs/components/switch.md): The choice is a setting that applies immediately.
- [tabs](https://design.senvenli.com/docs/components/tabs.md): The choice switches between views of the same content; use SegmentedControl from Tabs.

## Accessibility

Keyboard:

- Space, Enter: Toggles the button.
- Arrow keys: Moves between buttons in a group.

Built in:

- The pressed state is announced; the label never changes with the state.

You provide:

- A `label` that names a ToggleGroup.

## API reference

### ToggleButton

| Prop | Type | Default | Required | Description |
| --- | --- | --- | --- | --- |
| children | `React.ReactNode` |  | yes | The label never changes with the state; `aria-pressed` carries it. |
| icon | `IconName` |  | no | Lucide icon name before the label. |
| size | `ToggleSize` |  | no |  |
| radius | `ComponentRadius` |  | no | Corner radius; the provider's default otherwise. |

Behavior props from React Aria:

| Prop | Type | Default | Required | Description |
| --- | --- | --- | --- | --- |
| autoFocus | `boolean` |  | no | Whether the element should receive focus on render. |
| id | `Key` |  | no | When used in a ToggleButtonGroup, an identifier for the item in `selectedKeys`. When used standalone, a DOM id. |
| onFocus | `(e: FocusEvent<Element, Element>) => void` |  | no | Handler that is called when the element receives focus. |
| onBlur | `(e: FocusEvent<Element, Element>) => void` |  | no | Handler that is called when the element loses focus. |
| onChange | `(isSelected: boolean) => void` |  | no | Handler that is called when the element's selection state changes. |
| onClick | `(e: MouseEvent<FocusableElement, MouseEvent>) => void` |  | no | **Not recommended – use `onPress` instead.** `onClick` is an alias for `onPress` provided for compatibility with other libraries. `onPress` provides additional event details for non-mouse interactions. |
| isDisabled | `boolean` |  | no | Whether the button is disabled. |
| onPress | `(e: PressEvent) => void` |  | no | Handler that is called when the press is released over the target. |
| onPressStart | `(e: PressEvent) => void` |  | no | Handler that is called when a press interaction starts. |
| onPressEnd | `(e: PressEvent) => void` |  | no | Handler that is called when a press interaction ends, either over the target or when the pointer leaves the target. |
| onPressChange | `(isPressed: boolean) => void` |  | no | Handler that is called when the press state changes. |
| onPressUp | `(e: PressEvent) => void` |  | no | Handler that is called when a press is released over the target, regardless of whether it started on the target or not. |
| onFocusChange | `(isFocused: boolean) => void` |  | no | Handler that is called when the element's focus status changes. |
| preventFocusOnPress | `boolean` |  | no | Whether to prevent focus from moving to the button when pressing it. Caution, this can make the button inaccessible and should only be used when alternative keyboard interaction is provided, such as ComboBox's MenuTrigger or a NumberField's increment/decrement control. |
| excludeFromTabOrder | `boolean` |  | no | Whether to exclude the element from the sequential tab order. If true, the element will not be focusable via the keyboard by tabbing. This should be avoided except in rare scenarios where an alternative means of accessing the element or its functionality via the keyboard is available. |
| onHoverStart | `(e: HoverEvent) => void` |  | no | Handler that is called when a hover interaction starts. |
| onHoverEnd | `(e: HoverEvent) => void` |  | no | Handler that is called when a hover interaction ends. |
| onHoverChange | `(isHovering: boolean) => void` |  | no | Handler that is called when the hover state changes. |
| isSelected | `boolean` |  | no | Whether the element should be selected (controlled). |
| defaultSelected | `boolean` |  | no | Whether the element should be selected (uncontrolled). |

Also takes 82 standard DOM and ARIA attributes (id, aria-label, event handlers…).

### ToggleGroup

| Prop | Type | Default | Required | Description |
| --- | --- | --- | --- | --- |
| label | `string` |  | yes | Names the group ("Text alignment"). |
| children | `React.ReactNode` |  | yes |  |

Behavior props from React Aria:

| Prop | Type | Default | Required | Description |
| --- | --- | --- | --- | --- |
| orientation | `Orientation` |  | no | The orientation of the the toggle button group. |

Also takes 81 standard DOM and ARIA attributes (id, aria-label, event handlers…).

### Tokens it reads

`--ds-background-brand-bold`, `--ds-background-brand-bold-hovered`, `--ds-background-brand-bold-pressed`, `--ds-background-danger`, `--ds-background-danger-bold`, `--ds-background-danger-bold-hovered`, `--ds-background-disabled`, `--ds-background-neutral`, `--ds-background-neutral-hovered`, `--ds-background-neutral-pressed`, `--ds-background-neutral-subtle-hovered`, `--ds-background-selected`, `--ds-background-selected-hovered`, `--ds-background-selected-pressed`, `--ds-border`, `--ds-border-bold`, `--ds-border-focused`, `--ds-border-selected`, `--ds-easing-standard`, `--ds-link`, `--ds-link-pressed`, `--ds-radius-control-medium`, `--ds-size-control`, `--ds-size-control-compact`, `--ds-size-control-large`, `--ds-size-icon`, `--ds-space-050`, `--ds-space-075`, `--ds-space-100`, `--ds-space-150`, `--ds-space-200`, `--ds-text`, `--ds-text-danger`, `--ds-text-disabled`, `--ds-text-on-brand`, `--ds-text-on-danger`, `--ds-text-subtle`

## Related

- [button](https://design.senvenli.com/docs/components/button.md)
