# ButtonGroup

Crate: `libero`
Import: `use libero::components::ButtonGroup;`
Feature: `button-group` (off by default, turns on `box`; [features.md](features.md))
Source: <https://github.com/tdymel/libero/tree/main/libero/src/components/buttons/button_group.rs>
Index: [index.md](index.md) lists every other page
Description: Buttons and action icons side by side as one control, sharing seams and defaults.

Buttons and action icons side by side as one control. Neighbours share one
seam and only the group's outer corners are round, on logical sides, so the
ends swap under right-to-left text.

## Usage

Pressing one button releases the other; `multiple: true` lets several stay
pressed.

```rust
use dioxus::prelude::*;
use libero::components::{ActionIcon, Button, ButtonGroup, Pictogram};
use pictogram_icons_lucide as lucide;
use std::collections::HashSet;

#[component]
fn Demo() -> Element {
    let mut selected = use_signal(|| HashSet::from(["bold".to_string()]));
    rsx! {
        ButtonGroup {
            "aria-label": "Text style",
            value: selected(),
            onchange: move |next| selected.set(next),
            Button { value: "bold", "Bold" }
            Button { value: "italic", "Italic" }
            ActionIcon {
                value: "underline",
                aria_label: "Underline",
                Pictogram { icon: lucide::underline::outlined }
            }
        }
    }
}
```

## Props

| Prop | Type | Default | Description |
|---|---|---|---|
| `orientation` | `Responsive<Orientation>` | `horizontal` | `"vertical"` stacks the buttons, each as wide as the widest. Or one per breakpoint, `orientation: responsive(Orientation::Vertical).md(Orientation::Horizontal)`: the layout switches with the window from the first paint. |
| `variant` | `Responsive<Variant>` | - | Default `variant` of the buttons inside; a button's own prop wins, as for `color`, `size`, `radius`, `disabled` and `focusable_when_disabled`. Between two buttons without a visible border of their own (every variant but `outlined`), the group draws a thin divider. Or one per breakpoint, `variant: responsive(Variant::Filled).md(Variant::Outlined)`. |
| `color` | `ThemeAwareValue` | - | Default `color` of the buttons inside. |
| `size` | `Responsive<Size>` | - | Default `size` of the buttons inside. Or one per breakpoint, `size: responsive(Size::Xs).md(Size::Md)`. |
| `radius` | `Responsive<ThemeAwareValue>` | - | The group's outer corners, or any CSS, e.g. `radius: "0"`. The corners between two buttons are always square. Or one per breakpoint, `radius: responsive(Size::Xs).md(Size::Xl)`. |
| `disabled` | `bool` | - | Disables every button inside that does not set `disabled` itself, and takes it out of the Tab order unless `focusable_when_disabled` says otherwise. |
| `focusable_when_disabled` | `bool` | - | Default `focusable_when_disabled` of the buttons inside: a disabled one stays a Tab stop. |
| `value` | `HashSet<String>` | - | The `value`s of the pressed buttons, strictly controlled. Set, every `Button` and `ActionIcon` with a `value` is a toggle with `aria-pressed`, unless it sets `selected` itself. Unset, a plain group. |
| `multiple` | `bool` | `false` | With `value`: any number of buttons pressed. Off, pressing one releases the other, and pressing the pressed one releases it. |
| `onchange` | `EventHandler<HashSet<String>>` | - | Asks for the next set of pressed `value`s. Without it the selection can never change. |
| `children` | `Element` | `required` | `Button`s, `ActionIcon`s, and components built on them, such as `ThemeSwitcher`. |

Like every component, `ButtonGroup` also takes the shared props `sx`,
`class`, `style`, `states`, `hidden_from`, `visible_from`, and any extra HTML attributes.

## Accessibility

### Libero handles

- The root is a `role="group"`, so a screen reader announces the buttons as
  one set.
- Each button stays its own Tab stop, and the focused one's ring is drawn above
  its neighbours.
- With `value`, a button with a `value` is a toggle button: `aria-pressed` says
  whether it is pressed, and Space and Enter toggle it.

### You must

- Name the group with `aria-label`, or `aria-labelledby` on a visible heading.

### Example

Zoom out, Reset and Zoom in in a `ButtonGroup` with `"aria-label": "Zoom"`: a
screen reader reads the three buttons as the "Zoom" group, and Tab stops on
each one.

### Limits

- It is not a `toolbar`: no arrow-key navigation between the buttons.
- For one choice out of several, use [`SegmentedControl`](segmented_control.md),
  which is a radio group.
- A child hidden with `display: none` still counts as first or last and squares its
  neighbour's outer corners; render a conditional control only when shown.

## CSS variables

None of its own; the buttons inside keep theirs.

## Data attributes

`horizontal` or `vertical` on the root's `data-state`.
