Alert

Alerts are used to display important messages inline or as toast notifications.

This is a standard alert. You can customize its content and even the icon.

This is super informative

You can tell by how pretty the alert is.

Your changes have been saved

You can safely exit the app now.

Your session has ended

Please login again to continue.

An error occured while updating

Update your details and try again!.

Add the closable attribute to show a close button that will hide the alert.

You can close this alert any time!

Icons are optional. Simply omit the icon slot if you don’t want them.

Nothing fancy here, just a simple alert.
This is super informative

You can tell by how pretty the alert is.

Set the duration attribute to automatically hide an alert after a period of time. This is useful for alerts that don’t require acknowledgement.

Show Alert This alert will automatically hide itself after three seconds, unless you interact with it.

To display an alert as a toast notification, or “toast”, create the alert and call its toast() method. This will move the alert out of its position in the DOM and into the toast stack where it will be shown. Once dismissed, it will be removed from the DOM completely. To reuse a toast, store a reference to it and call toast() again later on.

You should always use the closable attribute so users can dismiss the notification. It’s also common to set a reasonable duration when the notification doesn’t require acknowledgement.

Info Success Warning Error This is super informative
You can tell by how pretty the alert is.
Your changes have been saved
You can safely exit the app now.
Your session has ended
Please login again to continue.
An error occured while updating
Update your details and try again!

For convenience, you can create a utility that emits toast notifications with a function call rather than composing them in your HTML. To do this, generate the alert with JavaScript, append it to the body, and call the toast() method as shown in the example below.

Create Toast

The toast stack is a fixed position singleton element created and managed internally by the alert component. It will be added and removed from the DOM as needed when toasts are shown. When more than one toast is visible, they will stack vertically in the toast stack.

By default, the toast stack is positioned at the bottom-right of the viewport. You can change its position by targeting .ods-toast-stack in your stylesheet. To make toasts appear at the top-left of the viewport, for example, use the following styles.

.ods-toast-stack {
left: 0;
right: auto;
}

import '@ods/components/web.alert';

Note: You only need to import a web component once in your code, as it registers itself globally when defined, allowing you to use it anywhere within your application without needing to re-import it each time you want to use the component.

Slots

NameDescription
(default)The alert's main content.
iconAn icon to show in the alert. Works best with `<ods-icon>`.

Properties

Please note, both DOM properties and HTML attributes are shown in the table below. Unless otherwise specified, the property and attribute names are identical.

Property/AttributeReflectsTypeDefault
open
booleanfalse
closable
booleanfalse
variant
'info' | 'success' | 'neutral' | 'warning' | 'error''info'
duration
Infinity

Events

React EventDescriptionEvent Detail
ods-showEmitted when the alert opens.--
ods-after-showEmitted after the alert opens and all animations are complete.--
ods-hideEmitted when the alert closes.--
ods-after-hideEmitted after the alert closes and all animations are complete.--

Methods

NameDescriptionArguments
show()Shows the alert.--
hide()Hides the alert--
toast()Displays the alert as a toast notification. This will move the alert out of its position in the DOM and, when dismissed, it will be removed from the DOM completely. By storing a reference to the alert, you can reuse it by calling this method again. The returned promise will resolve after the alert is hidden.--

Parts

NameDescription
baseThe component's base wrapper.
iconThe container that wraps the optional icon.
messageThe container that wraps the alert's main content.
close-buttonThe close button, an `<ods-icon-button>`.
close-button__baseThe close button's exported `base` part.

Dependencies

This component automatically imports the following dependencies.

  • ods-icon
  • ods-icon-button

Usage

This section includes guidelines for designers and developers about the usage of this component in different contexts.

Do

  • Trigger an alert when a user takes an action and needs a confirmation that the action succeeded or didn’t, and why.
  • Consider placement of the Alert - the default stack position is bottom right, but they can also be placed within the interface, like above a form or within a card.
  • Add an Alert Title if needed but it is not necessary to add one if the message itself is easily understood and scannable

Dont

  • Use Alerts to notify user of a new message or other low-priority background information, unless customization options allow for this.
  • Place an alert within the interface in a position that causes baseline shifts to other elements - i.e., place the alert at the top or bottom of a form, not in between inputs.
  • Add actions into the Alert other than the close action. If the alert requires an action from the user, consider a dialog instead.
  • Replace the standard status-based icons with custom alternatives
  • Use an alert as an alternative to inline error messaging on inputs

Alerts communicate important information to the user and should be as clear and concise as possible to convey the information with little ambiguity or confusion.

  • Get to the point, don’t pad the message with too much fluff

Do

  • Changes saved successfully!

Dont

  • Hey there Doctor Smith, just wanted to let you know that the changes you made were successful. Have a nice day!

  • Include helpful direction if needed

Do

  • Network error - Check your internet connection.

Dont

  • Network error.

Search