Overview
An alert shows a short status message. It has a status icon, a title, a description and a close button. data-status sets the icon and the colors. An alert comes in three forms:
- A standard alert sits in a page section.
- An inline alert is one line next to a form or an action, where space is limited.
- A toast floats over the page after an action, such as save, delete or submit, and can close itself.
Write the HTML for an alert that is in the page when it loads. Call NDS.Alert.create() for an alert that appears later, such as after a failed request. A toast is made only with create(): its timer and placement need the script.
Pick another component when:
- the user must make a decision before they continue: Modal. An alert never blocks the page.
- the page or view cannot show its content: the user must sign in, has no permission, the item is not found, or the load failed with nothing else to show: Status Section. Inside a tab panel, use an alert: the other tabs still work
Markup
A new version is ready. Update now to get the latest fixes.
<div class="nds-alert nds-card" data-status="info" role="alert">
<span class="nds-feedback nds-alert-icon nds-outline">
<span class="nds-feedback-icon">
<i class="nds-icon" aria-hidden="true"></i>
</span>
</span>
<div class="nds-alert-content">
<div class="nds-alert-text">
<span class="nds-alert-title">Update available</span>
<p class="nds-alert-description">A new version is ready. Update now to get the latest fixes.</p>
</div>
</div>
<button class="nds-btn nds-subtle nds-icon-only nds-alert-close" aria-label="Close alert" data-i18n-aria-label="alert.close_alert">
<i class="nds-icon nds-hgi-cancel-01" aria-hidden="true"></i>
</button>
</div>
NDS.Alert.create({
status: 'info',
title: 'Update available',
description: 'A new version is ready. Update now to get the latest fixes.',
target: '#messages'
});
Variants
#alert-js is the same alert as one NDS.Alert.create() call (data-js on the base canon). A row whose On element is create() sets an option of that call, or of the toast’s. create({ display: 'toast' }) means only a call with that option, and create():not({ display: 'inline' }) any call without it.
| Group | Option | Markup | On element | Use |
|---|---|---|---|---|
| Structure | Standard (default) | — | — | A title and a description, with the stripe on the start edge. On a phone, it stacks and the stripe moves to the top |
| Structure | Inline | canon #alert-inline |
— | One line, with a thin stripe at the bottom and a solid icon. Always tinted. Actions go to the end of the line |
| Structure | Inline | display: 'inline' |
create() |
The same, in JavaScript |
| Structure | Toast (hint: Floats over the page and can close itself) | canon #alert-toast |
— | Floats at a corner of the screen, so it needs no target. duration closes it after that many ms; 0 keeps it until the user closes it. A toast is made only with JavaScript: its timer and placement need the script |
| Status | Info (default) | [data-status="info"] |
.nds-alert |
Neutral news or an update |
| Status | Info (default) | status: 'info' |
create() |
The same, in JavaScript |
| Status | Success | [data-status="success"] |
.nds-alert |
A confirmation |
| Status | Success | status: 'success' |
create() |
The same, in JavaScript |
| Status | Warning | [data-status="warning"] |
.nds-alert |
A risk the user should know about |
| Status | Warning | status: 'warning' |
create() |
The same, in JavaScript |
| Status | Error | [data-status="error"] |
.nds-alert |
A failure |
| Status | Error | status: 'error' |
create() |
The same, in JavaScript |
| Status | Critical (hint: Error colors with its own icon) | [data-status="critical"] |
.nds-alert |
A system-level emergency. Same colors as Error, with its own icon |
| Status | Critical (hint: Error colors with its own icon) | status: 'critical' |
create() |
The same, in JavaScript |
| Status | Neutral | [data-status="neutral"] |
.nds-alert |
A general notice |
| Status | Neutral | status: 'neutral' |
create() |
The same, in JavaScript |
| Actions | None (default) | — | — | No actions. Actions go last in .nds-alert-content, after .nds-alert-text |
| Actions | Buttons | canon #alert-actions |
.nds-alert-content |
Buttons that let the user respond, such as retry, undo or update. Use nds-sm buttons |
| Actions | Buttons | canon #alert-js-actions |
create() |
The same, in JavaScript. onClick gets the alert element. dismiss: true closes the alert after the click |
| Actions | Link | canon #alert-link |
.nds-alert-content |
A link to a detail page. Links inside an alert take neutral colors |
| Actions | Link | canon #alert-js-link |
create() |
The same, in JavaScript. class replaces the button classes |
| Actions | Copy (hint: Copies a code or a log) | canon #alert-copy |
.nds-alert-content |
Copies data-copy, or the text of the element that data-copy-target selects. See Copy |
| Actions | Copy (hint: Copies a code or a log) | canon #alert-js-copy |
create() |
The same, in JavaScript. create() adds the copy icon |
| No close | No close | remove |
.nds-alert-close |
Leave out the close button, for an alert the user cannot close |
| No close | No close | closable: false |
create():not({ display: 'toast' }) |
The same, in JavaScript. A toast with a duration keeps its close button, which shows the countdown |
| Shadow | Shadow | .nds-shadow |
.nds-alert |
An elevation shadow |
| Shadow | Shadow | shadow: true |
create():not({ display: 'toast' }) |
The same, in JavaScript. A toast has a shadow by default |
| No title | No title | remove |
.nds-alert-title |
Leave out the title for a one-line message |
| No title | No title | title: '' |
create() |
The same, in JavaScript |
| Tinted | Tinted (hint: Tints the background with the status color) | .nds-color |
.nds-alert:not(.nds-inline) |
Tints the background with the status color. Use it in dense layouts, where the stripe alone does not stand out. No effect on inline, which is always tinted |
| Tinted | Tinted (hint: Tints the background with the status color) | color: true |
create():not({ display: 'inline' }) |
The same, in JavaScript |
| Position | Top (default) (hint: At the top, on the end side) | — | create({ display: 'toast' }) |
At the top, on the end side, below the sticky header. top-end is the same |
| Position | Top start | position: 'top-start' |
create({ display: 'toast' }) |
Top, on the start side. -start and -end follow the text direction |
| Position | Top left (hint: Stays on the left in every direction) | position: 'top-left' |
create({ display: 'toast' }) |
Top left in every direction |
| Position | Top right (hint: Stays on the right in every direction) | position: 'top-right' |
create({ display: 'toast' }) |
Top right in every direction |
| Position | Bottom (hint: At the bottom, on the end side) | position: 'bottom' |
create({ display: 'toast' }) |
At the bottom, on the end side. bottom-end is the same |
| Position | Bottom start | position: 'bottom-start' |
create({ display: 'toast' }) |
Bottom, on the start side |
| Position | Bottom left (hint: Stays on the left in every direction) | position: 'bottom-left' |
create({ display: 'toast' }) |
Bottom left in every direction |
| Position | Bottom right (hint: Stays on the right in every direction) | position: 'bottom-right' |
create({ display: 'toast' }) |
Bottom right in every direction |
Behavior
Toast
A toast is an alert that floats at a corner of the screen and fades in, so it needs no target. Only NDS.Alert.create() makes one, with display: 'toast', because its placement and timer need the script. Top toasts sit below the sticky header, with the newest first. Bottom toasts stack with the newest last. On a phone, a toast takes the full width. A toast shows above an open Modal or Panel, and the user can hover it or close it there. To dock toasts in a place of your own, add a .nds-alert-placeholder with data-position.
Auto Close
A toast with a duration closes itself after that many milliseconds, and a ring on its close button counts down. The timer pauses while the pointer is over the toast or the keyboard focus is in it, and resumes when they leave. A click stops the timer for good, and the ring goes away: the toast then stays until the user closes it. On a touch screen, a tap does the same.
Built-in Features
-
Status Icon and Color
data-statussets the icon, the stripe color and the tint. The icon element stays empty in the markup. -
Close Button
The close button removes the alert. It works on alerts in the page at load. For alerts you add later, call
NDS.Alert.init(), or create them withNDS.Alert.create(). -
Safe Content
create()escapes the title and the description. An action with an unsafehref, such asjavascript:, becomes a plain button.
Best Practices
- Pick the status that matches the message: success for a confirmation, info for news, warning for a risk, error for a failure, critical for a system-level emergency, and neutral for a general notice. Do not use a status for its color only.
- Add
nds-colorin dense layouts, where the stripe alone does not stand out. An inline alert is always tinted, so it needs nonds-color. - Keep the description to one or two sentences. For more, add a link to a detail page.
- Add actions when the user must respond, such as retry, undo or update. Do not add an action that only closes the alert; the close button does that.
- Add a copy action for text the user will paste somewhere else, such as a code, a reference number or an error log.
- Do not use an alert in place of a page the user cannot use, such as a sign-in gate or an access-denied page. Show a Status Section instead: there, the message is the page.
- Do not use a toast for a critical error or a message the user must act on. A toast can close before the user reads it.
- Give a toast a
durationof 3000 to 5000 ms. Under 2000 ms is too short to read. - Prefer the logical toast positions (
-start,-end), which follow the text direction. Use-leftand-rightonly when the toast must stay at one physical edge.
API
Data Attributes
| Attribute | Element | Effect |
|---|---|---|
data-status |
.nds-alert |
The status: info, success, warning, error, critical or neutral. Sets the icon and the colors |
data-position |
.nds-alert-placeholder |
Where toasts dock. Place your own placeholder with this attribute, and toasts for that position go into it instead of a new one |
data-alert-close |
A button inside .nds-alert |
Closes the alert on click, as data-modal-close does on a modal. .nds-alert-close still works, and it also places the built close button and makes it MD. A size class on the button still wins |
data-state |
.nds-toast |
Set by the script: toast-show, toast-hide and paused. Do not set it yourself |
CSS Custom Properties
| Property | Default | Controls |
|---|---|---|
--alert-stripe |
var(--border-neutral-primary) |
Stripe color. Each status sets its own |
--alert-icon-bg |
var(--background-neutral-light) |
Background of the icon circle. Each status sets its own |
--progress-duration |
4000ms |
Length of the countdown ring on .nds-alert-close. create() sets it from duration |
An alert is a card, so the card color properties (--card-bg, --card-border-color and --card-title-color) also apply. See Cards.
Tokens
It reads these shared tokens. A change to one also changes other components: see Tokens.
Source: the var() reads in _sass/components/_alert.scss.
| Token | Preview | Value |
|---|---|---|
--background-defaultRead by background-color | var(--colors-base-white)Dark mode var(--colors-neutral-800) | |
--background-neutral-lightRead by background-color | var(--colors-neutral-100)Dark mode var(--colors-alpha-white-10) | |
--background-successRead by --alert-stripe | var(--colors-green-600) | |
--background-success-faint | var(--colors-green-25)Dark mode var(--colors-green-alpha-10) | |
--background-infoRead by --alert-stripe | var(--colors-blue-600) | |
--background-info-faint | var(--colors-blue-25)Dark mode var(--colors-blue-alpha-10) | |
--background-warningRead by --alert-stripe | var(--colors-yellow-600) | |
--background-warning-faint | var(--colors-yellow-25)Dark mode var(--colors-yellow-alpha-10) | |
--background-errorRead by --alert-stripe | var(--colors-red-600) | |
--background-error-faint | var(--colors-red-25)Dark mode var(--colors-red-alpha-10) | |
--text-primary-paragraphRead by color | var(--colors-neutral-700)Dark mode var(--colors-neutral-300) | |
--text-success | var(--colors-green-700)Dark mode var(--colors-green-400) | |
--text-info | var(--colors-blue-700)Dark mode var(--colors-blue-400) | |
--text-warning | var(--colors-yellow-700)Dark mode var(--colors-yellow-400) | |
--text-error | var(--colors-red-700)Dark mode var(--colors-red-400) | |
--border-neutral-lightRead by --alert-stripe | var(--colors-neutral-300)Dark mode var(--colors-neutral-400) | |
--border-neutral-primaryRead by --alert-stripe | var(--colors-neutral-300)Dark mode var(--colors-neutral-600) | |
--featuredicons-background-error-lightRead by --alert-icon-bg | var(--background-error-light)Dark mode var(--colors-red-alpha-10) | |
--featuredicons-background-info-lightRead by --alert-icon-bg | var(--background-info-light)Dark mode var(--colors-blue-alpha-10) | |
--featuredicons-background-warning-lightRead by --alert-icon-bg | var(--background-warning-light)Dark mode var(--colors-yellow-alpha-10) | |
--featuredicons-background-success-lightRead by --alert-icon-bg | var(--background-success-light)Dark mode var(--colors-green-alpha-10) | |
--featuredicons-background-neutral-lightRead by --alert-icon-bg | var(--background-neutral-light)Dark mode var(--colors-alpha-white-10) | |
--link-neutralRead by --link-color | var(--colors-neutral-700)Dark mode var(--colors-neutral-200) | |
--link-neutral-hoveredRead by --link-hover | var(--colors-neutral-500)Dark mode var(--colors-neutral-400) | |
--link-neutral-pressedRead by --link-pressed | var(--colors-neutral-400)Dark mode var(--colors-neutral-500) | |
--link-neutral-focusedRead by --link-focused | var(--colors-neutral-700)Dark mode var(--colors-neutral-200) | |
--link-neutral-visitedRead by --link-visited | var(--colors-neutral-600)Dark mode var(--colors-neutral-300) |
JavaScript
| Method | Effect |
|---|---|
NDS.Alert.create(options) |
Builds an alert, inserts it and returns the element. Before the alert’s bundle loads, the call loads it and returns a promise of the element: await the call when you use what it returns |
NDS.Alert.dismiss(elOrSelector) |
Removes one alert. A toast fades out first |
NDS.Alert.dismissAll(container) |
Removes every alert inside a container, given as an element or a selector |
NDS.Alert.init() |
Wires the close button on alerts added to the page after load |
| Option | Default | Effect |
|---|---|---|
status |
'info' |
The status, as in data-status |
title |
'' |
Title text. Leave it empty for no title |
description |
'' |
Description text |
display |
'default' |
'default', 'inline' or 'toast' |
target |
null |
Element or selector to insert into. A toast needs none. The alert has no outer margin: in a container with no gap, add nds-block to the element create() returns |
prepend |
false |
Insert at the start of target, not at the end |
closable |
true |
Show the close button. A toast with a duration always has one |
shadow |
true for a toast, else false |
Add .nds-shadow |
color |
false |
Add .nds-color |
id |
null |
Id of the alert element. The new alert replaces an alert in the page with the same id, so a repeated toast does not stack |
position |
'top' |
Toast position: top or bottom, with an optional -start, -end, -left or -right. -start and -end follow the text direction, so -start is the left in English and the right in Arabic. top and bottom alone sit at the end side |
duration |
0 |
Toast auto-close time in ms. 0 never closes |
actions |
[] |
Action objects (next table) |
| Action key | Effect |
|---|---|
label |
Button text |
variant, size |
Button classes, nds-{variant} nds-{size}. Default subtle and sm |
class |
Replaces the button classes. Use 'nds-link' for a text link |
href, target |
Makes the action a link. target adds rel="noopener noreferrer" |
onClick(alertEl) |
Runs on click |
dismiss |
true closes the alert after the click |
copy, copyTarget |
Copies this text, or the text of the element this selector finds. Adds the copy icon, a checkmark flash and a screen-reader announcement |
Alerts fire no events. Use onClick on an action. The full API is in the banner of _js/nds-alert.js.