# Button

Crate: `libero`
Import: `use libero::components::Button;`
Feature: `button` (on by default, turns on `loader`, `anchor`; [features.md](features.md))
Source: <https://github.com/tdymel/libero/tree/main/libero/src/components/buttons/button.rs>
Index: [index.md](index.md) lists every other page
Description: A clickable action, a toggle, or a router-aware link.

A clickable action, a toggle, or a link when `to` is set. It defaults to
`type="button"`, so it never submits a form by accident. A submit button sets
`r#type: "submit"`. For an icon-only button, use [ActionIcon](action_icon.md);
for one floating over the page, [FloatingActionButton](floating_action_button.md).

## Usage

```rust
use dioxus::prelude::*;
use libero::components::{Button, Loader, Pictogram};
use pictogram_icons_lucide as lucide;

#[component]
fn Demo() -> Element {
    rsx! {
        Button {
            trailing_icon: rsx! { Pictogram { icon: lucide::chevron_right::outlined, width: "1.25em", height: "1.25em" } },
            loading_position: "start",
            loader: rsx! { Loader { variant: "dots", size: "xs", color: "currentColor" } },
            "Save changes"
        }
    }
}
```

## Props

| Prop | Type | Default | Description |
|---|---|---|---|
| `color` | `ThemeAwareValue` | `primary` | Accent color. A theme color name or any CSS color. Under a gradient, its first stop. Unset, a `standard` or `outlined` button on a gradient `Paper`, a coloured or gradient `Header`, a filled, tonal or gradient `Alert` or a `Mark` takes that surface's text color. A secondary action beside a filled primary one, such as "Cancel" beside "Save", is `color: "neutral"` with `variant: "outlined"`; `"ink"` draws it in the page's text colour instead. |
| `variant` | `Responsive<Variant>` | `filled` | Visual style, from most to least emphasis: `filled`, `tonal`, `elevated`, `outlined`, `standard`. `gradient` fills it from `color` into the theme's second stop. Or one per breakpoint, `variant: responsive(Variant::Outlined).md(Variant::Filled)`. |
| `gradient` | `Gradient` | - | With `variant: "gradient"`: the second stop and the angle, as `("info", 90)` or `Gradient::default().to("info").deg(90)`. The first stop is `color`. A literal CSS stop's label contrast is yours to check. Ignored by the other variants. |
| `radius` | `Responsive<ThemeAwareValue>` | `md` | Corner radius, independent of `size`, or any CSS, e.g. `radius: "0"`. Or one per breakpoint, `radius: responsive(Size::Sm).md(Size::Xl)`. |
| `size` | `Responsive<Size>` | `md` | Height, padding and font size. Or one per breakpoint, `size: responsive(Size::Sm).md(Size::Lg)`. Breakpoints follow the window, not the parent. |
| `full_width` | `bool` | `false` | Stretches the button to fill its container. |
| `justify` | `ThemeAwareValue` | `center` | Where the icons and the label sit along the button, as `justify-content`: `start`, `end`, `space-between` or any CSS. It shows on a `full_width` button or one given a width. Unset, they are centred, and an overlong label is cut at its end. |
| `gap` | `ThemeAwareValue` | `xs` | Space between the icons and the label: a size word or any CSS, as `gap: "0"`. It faces the label under `dir="rtl"` too. |
| `selected` | `bool` | - | Makes it a toggle button with the selected look. Leave it unset for a plain action. |
| `disabled` | `bool` | `false` | Disables and dims the button: `aria-disabled` rather than `disabled`, so a focused button keeps its focus. It leaves the Tab order and ignores presses. |
| `focusable_when_disabled` | `bool` | `false` | With `disabled`: keeps the button in the Tab order. |
| `loading` | `bool` | `false` | Shows a `Loader` over the label and ignores clicks. The button stays focusable and keeps its width, and sets `aria-busy`; announce the progress in your own status region. Ignored on a link. |
| `loader` | `Element` | - | Drawn in place of the `Loader` while `loading`. Size it yourself, e.g. `Loader { size: "xs" }`. |
| `loading_position` | `LoadingPosition` | `center` | Where the loader sits: `center` over the hidden label, or `start` and `end` in the place of `icon` and `trailing_icon`, with the label still visible. The loader is half the button's height and follows a per-breakpoint `size`. |
| `ripple` | `bool` | `true` | The press ripple. `false` drops it, for a dense toolbar or a theme without one. Reduced motion drops it anyway. |
| `value` | `String` | - | Its key in the enclosing `ButtonGroup`'s `value`. With that set, the button is a toggle and a press asks the group to select it; its own `selected` wins. Also the `<button>`'s `value` attribute. |
| `onclick` | `EventHandler<MouseEvent>` | - | Click handler. Not called on a link. |
| `to` | `NavigationTarget` | - | Renders a link instead of a `<button>`. Takes a path, a URL or a typed route (`Route::Foo {}`), or an `Option` of one: `None` is a plain button. |
| `target` | `String` | - | The link's `target` attribute. `"_blank"` adds a hidden "(opens in a new tab)". |
| `new_tab_hint` | `bool` | `true` | With `target: "_blank"`, the hidden "(opens in a new tab)". `false` drops it and the icon. |
| `new_tab_icon` | `bool` | `false` | With `target: "_blank"`, draws a small external icon after the label. Unset, the theme's `anchor.new_tab_icon`, which is off. |
| `icon` | `Element` | - | Drawn before the label. It never shrinks. |
| `trailing_icon` | `Element` | - | Drawn after the label, as a chevron or an arrow. It never shrinks, and on a link it comes before the new-tab icon. |
| `parts` | `Parts<ButtonPart>` | - | Styles for the inner parts in the Style API tab, under `sx`. |
| `children` | `Element` | required | The label, on one line. A long one is cut at the edge. |

`Button` also takes the `<button>` HTML attributes (`type`, `form`, `name`,
`value`, ...) and, like every component, the shared props `sx`, `class`,
`style`, `states`, `hidden_from`, `visible_from`, and any extra HTML attributes.

## Style API

Style a part with the `parts` prop, or address it as `[data-slot='…']` in your
own CSS. The names are stable. [Styling](styling.md#style-api) explains how
parts work.

| Part | `data-slot` | Description |
|---|---|---|
| `ButtonPart::Icon` | `button-icon` | The `icon` wrapper. |
| `ButtonPart::TrailingIcon` | `button-trailing-icon` | The `trailing_icon` wrapper. |

## Accessibility

### Libero handles

- A label cut at the edge is still the full accessible name.
- A link with `target: "_blank"` reads a hidden "(opens in a new tab)". With
  `new_tab_icon`, it also draws a small external icon.
- `selected: Some(false)` announces a toggle that is off. An unset `selected`
  announces no state.
- `focusable_when_disabled` keeps a disabled button in the Tab order, with
  `aria-disabled`.
- `loading` sets `aria-busy` and keeps the button focusable, so focus stays on
  it while it works.

### You must

- Pass a label that may be cut as `title` too, so sighted users can read it on
  hover.
- Announce the progress and the result of a `loading` button in your own
  `role="status"` region. The button announces nothing itself.

```rust
use dioxus::prelude::*;
use libero::components::Button;

#[component]
fn Demo() -> Element {
    let label = "Download the quarterly report as a spreadsheet";
    rsx! {
        Button { title: label, "{label}" }
    }
}
```

### Example

A narrow toolbar button whose label may be cut, `Button { title: "Export as
PDF", "Export as PDF" }`: a screen reader still reads the full label, and the
title shows it on hover.

## Theme defaults

`ButtonDefaults` on the theme; per-size values live in its `sizes` scale.

| Field | Type | Description |
|---|---|---|
| `variant` | `Variant` | Default `variant` when the prop is omitted (`filled`). |
| `size` | `Size` | Default `size` when the prop is omitted. |
| `radius` | `Size` | Default `radius` when the prop is omitted. |
| `sizes` | `Sizes<ButtonSizeLevel>` | `font_size`, `height`, `padding_x` per size. |

## CSS variables

| Variable | Description |
|---|---|
| `--lsx-button-font-size-<size>` | `font-size` for that size step. |
| `--lsx-button-height-<size>` | `height` for that size step. |
| `--lsx-button-padding-x-<size>` | Horizontal padding for that size step. |
| `--lsx-button-justify` | `justify-content`, set by the `justify` prop. |
| `--lsx-button-gap` | Space between the icons and the label, set by the `gap` prop. |
| `--lsx-button-color` | Accent color of the current variant. |
| `--lsx-button-contrast` | Text color on top of that accent. |
| `--lsx-button-hover` | Accent color while hovered. |
| `--lsx-button-selected` | Background of a selected toggle button, and of any button while pressed. |
| `--lsx-button-container` | Container fill of `tonal`. |
| `--lsx-button-on-container` | Label color on that container, black or white, whichever reads on it. |

The corner radius comes from the shared radius scale, `--lsx-radius-<size>`,
and `elevated`'s shadow from the shared elevation scale, `--lsx-shadow-<size>`.

## Data attributes

State tokens on the root's `data-state`, space separated.

| Token | Condition |
|---|---|
| `filled` / `tonal` / `elevated` / `outlined` / `standard` | The `variant` in effect. |
| `size-<size>` | The `size` in effect. |
| `radius-<size>` | The `radius` in effect. |
| `checked` | `selected` is `Some(true)`. |
| `disabled` | `disabled` is set. |
| `loading` | `loading` is set, on a `<button>`. |
| `full-width` | `full_width` is set. |
| `ripple-a` / `ripple-b` | A click ripple is playing; the two alternate so the animation restarts. |
