Notices
A region in a corner of the screen, above the page, where messages that arrive on their own stack up: also called toasts or snackbars.
Example
View Code
<div class="notices" popover="manual" aria-live="polite" aria-label="Notifications">
<div class="alert" data-variant="success">
<div><strong>Saved.</strong> Your changes are live.</div>
<button type="button" data-close aria-label="Dismiss">×</button>
</div>
<div class="alert">
<div>Link copied.</div>
<button type="button" data-close aria-label="Dismiss">×</button>
</div>
</div>
When to use it
Feedback on something that just happened: "Saved", "Link copied", "Upload failed". The page stays usable while a notice shows. For a message that belongs in the flow of the page, next to the thing it is about, use an alert on its own.
How it works
One region per page, written into the page by you, usually at the end of body. It carries popover="manual", so it sits in the top layer above everything else, and neither Escape nor a click outside closes it. Each message in it is an ordinary alert.
View Code
<div class="notices" popover="manual" aria-live="polite" aria-label="Notifications">
<div class="alert" data-variant="success">
<div><strong>Saved.</strong> Your changes are live.</div>
<button type="button" data-close aria-label="Dismiss">×</button>
</div>
</div>
The region is fixed at the bottom inline end of the screen, a medium space from the edges: the bottom right in a left-to-right page, the bottom left in a right-to-left one. data-placement="top" moves it to the top inline end. Those are its only two values; the inline end is the only side.
<div class="notices" popover="manual" aria-live="polite" aria-label="Notifications" data-placement="top"></div>
Its width is the xs width, or the screen less a margin each side when that is narrower. Messages stack in a column in source order, newest last, with a small gap between them. At the bottom the newest sits nearest the edge; at the top the stack grows downward and the newest is at its foot. The region itself takes no clicks, so the page under its empty corner still works; only the messages do. A stack taller than the screen scrolls.
A message fades in over the fast duration when it arrives, which reduced motion collapses. Nothing slides. The close button is the alert's own data-close, which alert.js wires up, so a page with notices that can be closed loads alert.js.
notify.js opens the region on load. A notice already in the region when the page loads, such as a "Saved" after a redirect, shows in the top layer once the module runs. The module dispatches yeti:notify on it as soon as it runs, so a listener added by a later script misses it. A screen reader does not announce it either, since it is there before the region opens.
Loading the modules
A page with notices loads two modules. notify.js opens the region, raises notices and closes timed ones. alert.js closes a notice when its close button is clicked. yeti.js holds both.
Both are optional. Without notify.js a region that holds notices when the page loads still shows them, in the same corner, but not in the top layer, so something on the page placed above it can cover it. An empty region shows nothing. Nothing raises a notice from a button, data-timeout is ignored, and no yeti:notify is dispatched. With alert.js loaded, the close button still works.
<script type="module" src="…/js/alert.js"></script>
<script type="module" src="…/js/notify.js"></script>
Raising a notice from a button
Give a button data-action="notify" and a data-target that names a template. A click puts a copy of what the template holds at the end of the region. Each click adds one, so three quick clicks raise three notices. A data-target that names no template does nothing, and so does a click on a page with no region.
View Code
<button class="button" type="button" data-action="notify" data-target="saved">Save</button>
<template id="saved">
<div class="alert" data-variant="success" data-timeout="6">
<div><strong>Saved.</strong> Your changes are live.</div>
<button type="button" data-close aria-label="Dismiss">×</button>
</div>
</template>
<div class="notices" popover="manual" aria-live="polite" aria-label="Notifications"></div>
The notify action belongs to notify.js. button.js ignores actions it does not know, so a page can load both.
Raising a notice from script
Append an .alert to the region. The module sees it arrive and treats it as one from a button: it reports it and starts its timer.
const region = document.querySelector('.notices');
region.append(document.getElementById('saved').content.cloneNode(true));
A stack taller than the screen scrolls. When a notice arrives the region scrolls to its end, so the newest notice is in view.
Closing on its own
data-timeout on the alert closes it after that many seconds. data-timeout="6" is six seconds. Fractions such as 7.5 work too. Give a notice at least five seconds, and more when it has more to read. When the time is up the notice fades over the fast duration and is removed, as a click on its close button does. Without data-timeout, or with a value that is not a positive number, a notice stays until it is closed.
The count pauses, and picks up where it left off:
- while the pointer is over the notice,
- while focus is inside it,
- while the page is hidden, such as in a background tab.
A notice that holds a control ignores data-timeout. A control is a link, a field, anything with a tabindex, or any button other than the close button. It must not vanish while someone reaches for it. Give such a notice a close button.
Accessibility
The region is the live region. Give it aria-live="polite", so each message added to it is announced when the reader pauses, and a name, such as aria-label="Notifications". It must be open before the first message arrives, or the live region is not there to hear it. Most notices need no role of their own. A message that must interrupt whatever the reader is doing, such as a failed payment or a lost connection, carries role="alert". Say what happened in the words; the hue is decoration.
Limits
- While a modal dialog is open, everything outside it is inert, so a notice cannot be focused or clicked. Feedback for something inside a dialog belongs in the dialog.
- A dropdown or popover opened after the region sits above it in the top layer, so it can cover a notice in the same corner.
- Without
notify.jsonly the notices written into the page show, outside the top layer, and none closes on its own.
Attributes
| Attribute | Type | Values | Default | Description |
|---|---|---|---|---|
data-placement |
enum | bottom, top |
bottom |
Which corner the region sits in: the bottom or the top, at the inline end, so the right in a left-to-right page. The stack runs newest last in both. |
Markers
Attributes that descendants carry, not the root.
| Attribute | Type | Values | On | Description |
|---|---|---|---|---|
data-timeout |
string | > .alert |
Seconds before notify.js closes this notice. The count pauses while the pointer is over it, focus is in it or the page is hidden, and a notice holding a control ignores it. |
Children
> .alert: any number. A message. The region is empty until one arrives.
Tokens
| Token | Description |
|---|---|
--yeti-space-md |
The margin between the region and the edges of the screen. |
--yeti-space-sm |
The gap between messages. |
--yeti-width-xs |
The widest the region grows; it is always narrower than the screen by a margin each side. |
--yeti-duration-fast |
How long a message takes to fade in. |
--yeti-ease |
The curve of that fade. |
Accessibility
- Required attributes:
aria-live,aria-labeloraria-labelledby - The region is the live region: aria-live="polite", so each message added to it is announced when the reader pauses, and a name such as aria-label="Notifications". It must be open before the first message arrives, which notify.js does on load. A message that must interrupt carries role="alert". Escape does not close the region, and nothing in it takes focus on its own. While a modal dialog is open, a notice cannot be focused or clicked.
Browser support
- Used without guards: popover, @starting-style, dvh unit, :has()
- Behind
@supports: nothing
JavaScript
Optional enhancement: components/notices/notify.js. The component works without it.
Each event bubbles, crosses a shadow boundary, and cannot be cancelled.
| Event | Module | Detail | Description |
|---|---|---|---|
yeti:notify |
notify.js |
none | Dispatched on a notice once it is in the region. |
yeti:close |
notify.js |
none | Dispatched on a timed notice after its fade and before it is removed, while it is still in the page, as alert.js does for a close by hand. |
Available since 7.0.0.