# Alert

> A callout for a message the user should notice; for an error on the page, a warning or a notice about the account.

Source: https://shadleaf.dev/1.2.0/components/alert/

A box with an icon, a title and a description that draws attention to a message in the page. It stays where it is until
the page changes.

**Example: Alert.**

```html
<sl:alert>
  <sl:icon name="circle-check"/>
  <sl:alert-title>Order placed</sl:alert-title>
  <sl:alert-description>We will email you when it ships.</sl:alert-description>
</sl:alert>
```

## When to use

- An error the server reports, such as data that could not be loaded.
- A notice, such as a trial that ends soon or a feature in beta.
- A warning above a form, such as a change that affects every user.

Use something else for:

- A short message about what just happened, that goes away by itself: a [toast](https://shadleaf.dev/1.2.0/components/toast.md).
- Errors that belong to a whole form: [form errors](https://shadleaf.dev/1.2.0/components/form-errors.md).
- A question the user must answer: an [alert dialog](https://shadleaf.dev/1.2.0/components/alert-dialog.md).

## Built from parts

`<sl:alert>` holds an optional [`<sl:icon>`](https://shadleaf.dev/1.2.0/components/icon.md), then `<sl:alert-title>` and `<sl:alert-description>`. The icon
takes the alert's colour and sits beside both. Use either part on its own.

**Example: Title only, no icon.**

```html
<sl:alert>
  <sl:alert-title>Your trial ends in 3 days.</sl:alert-title>
</sl:alert>
```

## Destructive

**Example: Destructive.**

```html
<sl:alert variant="destructive">
  <sl:icon name="circle-alert"/>
  <sl:alert-title>Payment failed</sl:alert-title>
  <sl:alert-description>
    <p>Your card was declined. Check the details and try again:</p>
    <ul>
      <li>the expiry date</li>
      <li>the billing address</li>
    </ul>
  </sl:alert-description>
</sl:alert>
```

## Status variants

`success`, `info` and `warning` are Shadleaf's own, for an order that shipped, a notice or a limit that is close. As the
destructive alert, they colour the text, the icon and the description: in `--success-text`, `--info-text` and
`--warning-text`, the same tokens as a [toast's rich colours](https://shadleaf.dev/1.2.0/components/toast.md#rich-colours), which keep 4.5:1 or more on the
card background in light and dark mode.

**Example: Status variants.** Shadleaf's own, in the status colours.

```html
<sl:alert variant="success">
  <sl:icon name="circle-check"/>
  <sl:alert-title>Order shipped</sl:alert-title>
  <sl:alert-description>It should arrive on Thursday.</sl:alert-description>
</sl:alert>
<sl:alert variant="info">
  <sl:icon name="info"/>
  <sl:alert-title>New invoices are monthly</sl:alert-title>
  <sl:alert-description>From January, you get one invoice per month instead of one per order.</sl:alert-description>
</sl:alert>
<sl:alert variant="warning">
  <sl:icon name="triangle-alert"/>
  <sl:alert-title>Storage almost full</sl:alert-title>
  <sl:alert-description>You have used 90% of your storage. Delete old files or upgrade your plan.</sl:alert-description>
</sl:alert>
```

## Announcing an alert

An alert is not announced by itself. Most alerts are part of the page when it loads, and a screen reader reads them in
order with everything else; announcing them too would interrupt the user for no reason.

When an alert appears later, for example in the response to an htmx request, add `role="alert"`. A screen reader then
reads it as soon as it is inserted. Like any other attribute, `role` passes through to the rendered element. An
alert that takes the focus when it appears (`tabindex="-1"` and `autofocus`, which htmx honours in swapped content) is
read out because of the focus, and needs no role; see [Forms with htmx](https://shadleaf.dev/1.2.0/guides/forms-htmx.md).

**Example: Announced.** role="alert" makes a screen reader read the alert as soon as it appears.

```html
<sl:alert variant="destructive" role="alert">
  <sl:icon name="circle-alert"/>
  <sl:alert-title>The order could not be saved</sl:alert-title>
  <sl:alert-description>Someone else changed it. Reload the page to see their changes.</sl:alert-description>
</sl:alert>
```

For a message that is not urgent, `role="status"` is announced when the user is idle instead of at once.

## Attributes

### sl:alert

Attributes of `<sl:alert>`: A callout for a message the user should notice: an optional `sl:icon`, then `sl:alert-title` and `sl:alert-description`. It is not announced by itself; add `role="alert"` when it appears after the page loaded, such as in an htmx response.

| Attribute | Values | Default | Description |
| --- | --- | --- | --- |
| `variant` | `default`, `destructive`, `success`, `info`, `warning` | `default` | Visual style of the alert. `success`, `info` and `warning` are Shadleaf's own, in the status colours. |

Every prop can also be set from an expression: `th:variant="${...}"`. Any other attribute passes through to the rendered element, and `class` is appended.

### sl:alert-title

Attributes of `<sl:alert-title>`: The title of an alert, inside `sl:alert`.

| Attribute | Values | Default | Description |
| --- | --- | --- | --- |
| `level` | `1`, `2`, `3`, `4`, `5`, `6` | — | Heading level. With it the title is a heading (`role="heading"` and `aria-level`) that screen reader users can jump to; without it, plain text. |

Every prop can also be set from an expression: `th:level="${...}"`. Any other attribute passes through to the rendered element, and `class` is appended.

### sl:alert-description

Attributes of `<sl:alert-description>`: The body of an alert, inside `sl:alert`: text, paragraphs or a list.

`sl:alert-description` declares no props; every attribute passes through.

## Styling hooks

The classes and attributes your own CSS can select to restyle the component, as explained in
[your own CSS rules](https://shadleaf.dev/1.2.0/guides/adjustability.md#5-your-own-css-rules).

### sl:alert

Styling hooks of `<sl:alert>`.

**Parts**

| Class | Element |
| --- | --- |
| `.alert` | `<div>` |

**Props**

| Prop | Selector | Values |
| --- | --- | --- |
| `variant` | `.alert[data-variant="…"]`, `.alert:not([data-variant])` | `destructive`, `success`, `info`, `warning`; `default` (default) |

### sl:alert-title

Styling hooks of `<sl:alert-title>`.

**Parts**

| Class | Element |
| --- | --- |
| `.alert-title` | `<div>` |

### sl:alert-description

Styling hooks of `<sl:alert-description>`.

**Parts**

| Class | Element |
| --- | --- |
| `.alert-description` | `<div>` |
