# Alert

Crate: `libero`
Import: `use libero::components::Alert;`
Feature: `alert` (on by default, turns on `action-icon`; [features.md](features.md))
Source: <https://github.com/tdymel/libero/tree/main/libero/src/components/feedback/alert.rs>
Index: [index.md](index.md) lists every other page
Description: A tinted surface for something the reader has to know, with a title, an optional icon and close button, and a role that follows its color.

A tinted surface for something the reader has to know, such as an error, a
warning or a note. It takes no focus and does not close on Escape, because it
is not an overlay. `Form`'s error summary is an `Alert` with `color: "error"`
(see [Form](form.md)).

## Usage

```rust
use dioxus::prelude::*;
use libero::components::{Alert, Button};

#[component]
fn Demo() -> Element {
    rsx! {
        Alert {
            title: "Card expiring",
            actions: rsx! {
                Button { variant: "standard", color: "currentColor", size: "xs", "Update card" }
            },
            "Your card ends 09/26. Update it before the next invoice."
        }
    }
}
```

## Props

| Prop | Type | Default | Description |
|---|---|---|---|
| `title` | `String` | - | The title, and the alert's accessible name. Text only. |
| `icon` | `Option<Element>` | - | A leading icon of your own, hidden from screen readers. Unset, a `color` set to `info`, `success`, `warning` or `error` draws its severity's glyph, which an `IconProvider` can swap (`IconSlot::Info`, `Success`, `Warning`, `Error`); another colour draws none. |
| `with_icon` | `Option<bool>` | `true` | `false` drops the severity glyph of an alert without `icon`, `true` draws it for the theme's default colour too. Unset, `theme.alert.severity_icons` (on by default) for a `color` the call site sets. |
| `color` | `ThemeAwareValue` | `info` | The tint. A theme color name or any CSS color. `error` and `warning` make the role `alert`, the rest `status`. A `gradient` surface keeps the theme's gradient: there only the role follows this. |
| `variant` | `Responsive<Variant>` | `tonal` | Visual style, shared with `Button`: `filled`, `tonal`, `elevated`, `outlined`, `standard`, `gradient`. `gradient` paints the theme's gradient whatever the `color`. Or one per breakpoint, `variant: responsive(Variant::Outlined).md(Variant::Filled)`. |
| `radius` | `Responsive<ThemeAwareValue>` | `md` | Corner radius, a size step from `xs` to `xxl`, or any CSS, e.g. `radius: "0"`. Or one per breakpoint, `radius: responsive(Size::Sm).md(Size::Xl)`. |
| `actions` | `Option<Element>` | - | Buttons or links beside the message, before the close button. Give each a label that says what it does: `Button { variant: "standard", color: "currentColor", size: "xs" }` reads on every variant. |
| `onclose` | `EventHandler<()>` | - | Shows the close button and fires when it is pressed. Unmount the alert to close it. |
| `close_label` | `String` | `common.close` | The close button's accessible name. Unset, the localization's `common.close`, "Close" in English. |
| `parts` | `Parts<AlertPart>` | - | Styles for the inner parts in the Style API tab, under `sx`: `Parts::new().part(AlertPart::Title, sx().font_weight("700"))`. |
| `children` | `Element` | required | The message, read as the alert's description. |

Like every component, `Alert` also takes the shared props `sx`, `class`,
`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. [Style API in Styling](styling.md#style-api)
explains how parts work.

| Part | `data-slot` | Description |
|---|---|---|
| `AlertPart::Icon` | `icon` | The icon wrapper. |
| `AlertPart::Body` | `body` | The column holding the title and the message. |
| `AlertPart::Title` | `title` | The title. |
| `AlertPart::Message` | `message` | The message. |
| `AlertPart::Actions` | `actions` | The row holding `actions`. |
| `AlertPart::Close` | `close` | The close button. |

## Accessibility

### Libero handles

- An `error` or `warning` color renders `role="alert"`, which interrupts a
  screen reader. Every other color renders the polite `role="status"`. Your
  own `role` replaces either.
- The icon is hidden from screen readers.
- `actions` come after the message and before the close button in `Tab` order.
- Under a finger each action `Button` is at least 44px tall and wide, an `xs`
  one included.

### You must

- Name each action by what it does, such as "Update card": the alert takes no
  focus, so a reader finds the button by `Tab`.
- Say the severity in the title or the message too, as the icon is not read.
- Prefer `tonal` or `filled` for an error: `outlined` has no tint.
- Move the focus somewhere sensible in `onclose`: closing removes the focused
  close button.
- Mount the Alert after the page loads to have it announced: one present at
  first paint is read only when the reader reaches it.

### Example

A failed save, `Alert { color: "error", title: "Error: not saved" }` mounted
after the click: a screen reader interrupts to read it, and the title says the
severity in words.

## Theme defaults

`AlertDefaults` on the theme.

| Field | Type | Description |
|---|---|---|
| `variant` | `Variant` | `Tonal`. |
| `color` | `&'static str` | `info`. A palette colour name, read to default the `color` prop. |
| `radius` | `Size` | `Md`, the same step as `paper.radius`. |
| `padding` | `Size` | `Md`, on the spacing scale. |
| `gap` | `Size` | `Md`, from icon to text to close button. |
| `body_gap` | `Size` | `Xs`, from title to message. |
| `icon_size` | `&'static str` | `20px`, the icon slot's width and height. |
| `severity_icons` | `bool` | `true`. An alert whose `color` prop is `info`, `success`, `warning` or `error`, and that has no `icon`, draws its severity's glyph. |

## CSS variables

| Variable | Description |
|---|---|
| `--lsx-alert-radius` | The theme's corner radius. |
| `--lsx-alert-radius-override` | Set by the `radius` prop; wins over the theme's. |
| `--lsx-alert-padding` | From `AlertDefaults::padding`. |
| `--lsx-alert-gap` | From `AlertDefaults::gap`. |
| `--lsx-alert-body-gap` | From `AlertDefaults::body_gap`. |
| `--lsx-alert-icon-size` | From `AlertDefaults::icon_size`. |
| `--lsx-alert-color` | Resolved `color`. |
| `--lsx-alert-contrast` | Text color on that color, for `filled`. For a literal CSS color, black or white, whichever reads on it. |
| `--lsx-alert-container` | The tint of `tonal`. |
| `--lsx-alert-on-container` | Text color on that tint. |

## Data attributes

| Attribute | On |
|---|---|
| `data-state="filled"` / `tonal` / `elevated` / `outlined` / `standard` / `gradient` | The root, for the `variant` in effect. |
| `data-slot` | Each part, see [Style API](#style-api). |
