# Alerts

Status messages, from notices in the page to live alerts and toasts.

_Diagram: An alert is a notice at the top of the content area under the heading, and a toast appears in the bottom corner at the end of the window._

## Live example

### Inline alerts

```html
<div class="stack">
  <div data-alert="info">
    <p><strong>Info:</strong> Maintenance is planned for Sunday at 02:00.</p>
  </div>
  <div data-alert="success">
    <p><strong>Success:</strong> Your changes were published.</p>
  </div>
  <div data-alert="warning">
    <p><strong>Warning:</strong> Your trial ends in 3 days.</p>
    <p><a href="#inline-title">Choose a plan</a> to keep your projects.</p>
  </div>
  <div data-alert="danger">
    <p><strong>Error:</strong> The file could not be uploaded. Try a file under 10 MB.</p>
  </div>
</div>
```

### Live regions

```html
<div class="cluster">
  <button type="button" data-announce="live-status">Save settings</button>
  <button type="button" data-announce="live-alert">Send without a subject</button>
</div>
<div id="live-status" role="status"></div>
<div id="live-alert" role="alert"></div>
```

### Toasts

```html
<div class="cluster">
  <button type="button" data-toast="success">Copy link</button>
  <button type="button" data-toast="info">Start export</button>
  <button type="button" data-toast="danger">Sync now</button>
</div>
```

## Attributes

| Attribute or value | Effect |
| --- | --- |
| `data-alert="info \| success \| warning \| danger"` | an inline alert with a tone |
| `<dui-toaster role="status">` | the toast region (fixed position) |
| `<button> as a toast's last child` | its dismiss button |
| `role="status" \| role="alert"` | native ARIA live regions |

### Toasts

A fixed `<dui-toaster>` region, whose web component (`web-components/dui-toaster.js`) adds a `show()` method and dismiss buttons. The region is `role="status"` and in the page from the start. Each toast has a Dismiss button. A toast is a status, not a dialog: it never takes focus. See `explainers/toast.md`.

### Motion

A new toast fades in and lifts by `--dui-motion-distance` over `--dui-motion-duration-normal`; reduced motion keeps the fade only. Inline alerts and live region text appear at once: they are essential feedback.

### Keyboard

| Key | Action |
| --- | --- |
| <kbd>Tab</kbd> | reaches each toast's Dismiss button; toasts never take focus |
| <kbd>Enter</kbd> / <kbd>Space</kbd> | on Dismiss, removes the toast and focuses the next one |

## Accessibility

- New messages are announced without moving focus ([WCAG 4.1.3](https://www.w3.org/WAI/WCAG22/Understanding/status-messages.html)): `role="status"` is polite, `role="alert"` interrupts. Static alerts need no role.
- Render the empty live region with the page and insert the text later ([ARIA22](https://www.w3.org/WAI/WCAG22/Techniques/aria/ARIA22), [ARIA19](https://www.w3.org/WAI/WCAG22/Techniques/aria/ARIA19)).
- Start the message with a word such as "Warning:", so tone is not color alone ([WCAG 1.4.1](https://www.w3.org/WAI/WCAG22/Understanding/use-of-color.html)).
- **Our choice:** toasts never dismiss themselves or take focus, so nobody loses a message ([WCAG 2.2.1](https://www.w3.org/WAI/WCAG22/Understanding/timing-adjustable.html)).

## Specifications

- [APG Alert pattern](https://www.w3.org/WAI/ARIA/apg/patterns/alert/)
- [WCAG 4.1.3 Status Messages](https://www.w3.org/WAI/WCAG22/Understanding/status-messages.html)
- [MDN `status` role](https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Reference/Roles/status_role)
- [MDN `alert` role](https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Reference/Roles/alert_role)

## See also

- [Molecules](https://domestic-ui.odonckers.dev/molecules/index.md)
- Source: [`css/components/molecules/alerts.css`](https://github.com/odonckers/domestic-ui/blob/main/css/components/molecules/alerts.css), [`examples/molecules/alerts.html`](https://github.com/odonckers/domestic-ui/blob/main/examples/molecules/alerts.html)

## How other systems do it

For comparison only; these are not the standard this system follows.

- [Apple Human Interface Guidelines: Feedback](https://developer.apple.com/design/human-interface-guidelines/feedback)
- [Material Design 3: Snackbar](https://m3.material.io/components/snackbar/overview)
- [Vercel Geist: Note](https://vercel.com/geist/note)
