Alert
On this page
Installation
We recommend import maps when building pages with RHDS. Learn more about how to install on our getting started docs.
<script type="importmap">
{
"imports": {
"@rhds/elements/": "https://www.redhatstatic.com/dx/v1-alpha/@rhds/elements@2.1.1/elements/",
}
}
</script>
Copy to Clipboard
Copied!
Wrap lines
Overflow lines
npm install @rhds/elements
Copy to Clipboard
Copied!
Wrap lines
Overflow lines
<script type="importmap">
{
"imports": {
"@rhds/elements/rh-alert/rh-alert.js": "https://ga.jspm.io/npm:@rhds/elements@2.1.1/rh-alert/rh-alert.js"
},
"scopes": {
"https://ga.jspm.io/": {
"@floating-ui/core": "https://ga.jspm.io/npm:@floating-ui/core@1.6.9/dist/floating-ui.core.mjs",
"@floating-ui/dom": "https://ga.jspm.io/npm:@floating-ui/dom@1.6.13/dist/floating-ui.dom.mjs",
"@floating-ui/utils": "https://ga.jspm.io/npm:@floating-ui/utils@0.2.9/dist/floating-ui.utils.mjs",
"@floating-ui/utils/dom": "https://ga.jspm.io/npm:@floating-ui/utils@0.2.9/dist/floating-ui.utils.dom.mjs",
"@lit/context": "https://ga.jspm.io/npm:@lit/context@1.1.4/development/index.js",
"@lit/reactive-element": "https://ga.jspm.io/npm:@lit/reactive-element@2.0.4/development/reactive-element.js",
"@lit/reactive-element/decorators/": "https://ga.jspm.io/npm:@lit/reactive-element@2.0.4/development/decorators/",
"@patternfly/pfe-core": "https://ga.jspm.io/npm:@patternfly/pfe-core@4.0.4/core.js",
"@patternfly/pfe-core/": "https://ga.jspm.io/npm:@patternfly/pfe-core@4.0.4/",
"@patternfly/pfe-core/ssr-shims.js": "https://ga.jspm.io/npm:@patternfly/pfe-core@4.0.4/core.js",
"@rhds/elements/lib/": "https://ga.jspm.io/npm:@rhds/elements@2.1.1/lib/",
"@rhds/elements/": "https://ga.jspm.io/npm:@rhds/elements@2.1.1/elements/",
"@rhds/icons": "https://ga.jspm.io/npm:@rhds/icons@1.1.2/icons.js",
"@rhds/icons/ui/": "https://ga.jspm.io/npm:@rhds/icons@1.1.2/ui/",
"@rhds/tokens/css/": "https://ga.jspm.io/npm:@rhds/tokens@2.2.0/css/",
"@rhds/tokens/media.js": "https://ga.jspm.io/npm:@rhds/tokens@2.2.0/js/media.js",
"lit": "https://ga.jspm.io/npm:lit@3.2.1/index.js",
"lit-element/lit-element.js": "https://ga.jspm.io/npm:lit-element@4.1.1/development/lit-element.js",
"lit-html": "https://ga.jspm.io/npm:lit-html@3.2.1/development/lit-html.js",
"lit-html/": "https://ga.jspm.io/npm:lit-html@3.2.1/development/",
"lit/": "https://ga.jspm.io/npm:lit@3.2.1/",
"prism-esm": "https://ga.jspm.io/npm:prism-esm@1.29.0-fix.6/prism.js",
"prism-esm/components/": "https://ga.jspm.io/npm:prism-esm@1.29.0-fix.6/components/",
"tslib": "https://ga.jspm.io/npm:tslib@2.8.1/tslib.es6.mjs"
}
}
}
</script>
Copy to Clipboard
Copied!
Wrap lines
Overflow lines
Add it to your page with this import statement
<script type="module">
import '@rhds/elements/rh-alert/rh-alert.js';
</script>
Copy to Clipboard
Copied!
Wrap lines
Overflow lines
Usage
<rh-alert>
<h3 slot="header">Default</h3>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam eleifend elit sed est
egestas, a sollicitudin mauris tincidunt.</p>
<rh-button slot="actions" variant="secondary" data-action="confirm">Confirm</rh-button>
<rh-button slot="actions" variant="link" data-action="dismiss">Cancel</rh-button>
</rh-alert>
<script type="module">
import '@rhds/elements/rh-alert/rh-alert.js';
import '@rhds/elements/rh-button/rh-button.js';
</script>
Copy to Clipboard
Copied!
Wrap lines
Overflow lines
Toasting Alerts
The toast
variant of alert is intended specifically and only to be used when
toasting alerts. To toast an alert, use the static toast()
method on the
RhAlert
constructor. Avoid writing your own
<rh-alert variant="toast">
in HTML. The argument to toast()
is an object
with at least a message
property, and several other options.
import { RhAlert } from '@rhds/elements/rh-alert/rh-alert.js';
RhAlert.toast({ state: 'success', message: 'Saved!' });
The full list of options for the toast
method is:
Option | Type | Description |
---|---|---|
message (required) |
lit-html renderable value | Alert body content. Can be any value which lit-html can render. Simple strings are preferable |
heading |
string |
Alert heading content. Must be a simple string. |
state |
See alert's state attribute |
Alert state attribute |
persistent |
boolean |
Whether the alert should remain on screen until the user explicitly dismisses it |
actions |
Array of one or two actions objects | One or more optional body actions. See actions options info below. |
Toast action options
Actions objects have two keys, text
for the action button text, and action
e.g. dismiss
or confirm
, which is applied to the close
event as the
action
property.
Option | type | Description |
---|---|---|
text |
string | Button label text, e.g. "Confirm" |
action |
string | close event event.action property e.g. 'confirm' |
Toasted alert content
When toasting, you must provide body content for the alert. This content can
either be a string, a DOM node, a lit-html TemplateResult
, or an array of the
same.
Toasting a string
await RhAlert.toast({
message: 'Your request was submitted',
});
Toasting a DOM Node
const message = document.createElement('strong');
message.textContent = 'Your request was submitted';
await RhAlert.toast({ message });
Toasting a lit-html template:
import { html } from 'lit';
await RhAlert.toast({
message: html`Your request was <em>submitted</em>`,
});
Toasting an array of renderables
import { html } from 'lit';
await RhAlert.toast({
message: [
'Your request was',
html`<em>successfully</em>`,
new Text('submitted'),
],
});
You may, for example, use the <template>
element to define alert content
<template id="success-alert-content-template">
<p>Your subscription has been successfully renewed.</p>
<p>Contact <a href="#">Customer support</a> for more information.</p>
</template>
const template = document.getElementById('success-alert-content-template')
const { childNodes } = template.content.cloneNode(true);
const message = Array.from(childNodes);
await RhAlert.toast({ message });
Alert headings
You can provide an optional string for the alert's heading, which will
become the text content of the alert's slotted heading (h3
).
import { RhAlert } from '@rhds/elements/rh-alert/rh-alert.js';
await RhAlert.toast({
state: 'success',
heading: 'Subscription renewed',
message: 'Your subscription was successfully renewed',
});
Persistent toasted alerts
Toasted alerts can be timed or persistent. Timed alerts remain on screen for
eight seconds before disappearing. Persistent alerts remain on screen until the
user explicitly dismisses them. To make your toasted alert persistent, use the
persistent: true
option.
import { html } from 'lit';
await RhAlert.toast({
state: 'warning',
persistent: true,
message: html`Your subscription will expire in <time>two days</time>`,
});
Toasted alert actions
Like inline alerts, toasted alerts may include one or two actions. When there are two actions, the first will be a secondary variant button, and the second a link variant button. If there is only one action, it will be a link button.
Responding to actions
Actions have a button text and an action string, which identifies which button
was pressed. You can listen for the close
event, and if it is an
AlertCloseEvent
object, it will have an action
property.
import { AlertCloseEvent } from '@rhds/elements/rh-alert/rh-alert.js';
document.addEventListener('close', function(event) {
if (event instanceof AlertCloseEvent) {
switch (event.action) {
case 'renew':
// renew subscription
break;
}
}
});
await RhAlert({
state: 'warning',
message: 'Your subscription is about to expire',
actions: [
{ text: 'Renew', action: 'renew' },
{ text: 'Dismiss', action: 'dismiss' },
]
});
By default, clicking on an action button will remove the alert. If you would like to keep the alert on screen, prevent the default action:
document.addEventListener('close', function(event) {
if (event instanceof AlertCloseEvent) {
switch (event.action) {
case 'prevent':
// prevent alert from closing
event.preventDefault();
// ... do something with event
break;
}
}
});
rh-alert
An alert is a banner used to notify a user about a change in status or communicate other information. It can be generated with or without a user triggering an action first.
Slot Name | Description |
---|---|
|
Provide a description for the alert message |
header |
Provide a header for the alert message. |
actions |
Provide actions that the user can take for the alert |
Attribute | DOM Property | Description | Type | Default |
---|---|---|---|---|
state |
state |
Communicates the urgency of a message and is denoted by various styling configurations.
|
|
|
variant |
variant |
The alternate Inline alert style includes a border instead of a line which can be used to express more urgency or better grab the attention of a user. A Toast alert is used to present a global message about an event, update, or confirmation, like the result of a user action that cannot be presented within a specific layout or component. |
|
|
dismissable |
dismissable |
Alert variants have different rules regarding their ability to be dismissed by a user. Default, Info, and Success Inline alerts can be dismissed by a user selecting the close button. Warning and Danger Inline alerts can be dismissed by a user resolving the issues caused by the alert. All Toast alerts can be dismissed by a user selecting the close button or waiting for them to time out. |
|
|
Event Name | Description |
---|---|
close |
when the dismissable alert closes |
Token | Copy |
---|---|
--rh-border-width-md
|
|
--rh-box-shadow-xl
|
|
--rh-color-icon-status-caution
|
|
--rh-color-icon-status-danger
|
|
--rh-color-icon-status-info
|
|
--rh-color-icon-status-neutral
|
|
--rh-color-icon-status-success
|
|
--rh-color-icon-status-warning
|
|
--rh-color-interactive-primary-default
|
|
--rh-color-interactive-primary-hover
|
|
--rh-color-status-caution
|
|
--rh-color-status-danger
|
|
--rh-color-status-info
|
|
--rh-color-status-neutral
|
|
--rh-color-status-success
|
|
--rh-color-status-warning
|
|
--rh-color-surface-lightest
|
|
--rh-color-surface-status-caution
|
|
--rh-color-surface-status-danger
|
|
--rh-color-surface-status-info
|
|
--rh-color-surface-status-neutral
|
|
--rh-color-surface-status-success
|
|
--rh-color-surface-status-warning
|
|
--rh-color-teal-10
|
|
--rh-color-teal-50
|
|
--rh-color-text-primary
|
|
--rh-color-text-secondary
|
|
--rh-font-family-body-text
|
|
--rh-font-size-body-text-sm
|
|
--rh-font-weight-body-text-medium
|
|
--rh-font-weight-body-text-regular
|
|
--rh-length-xl
|
|
--rh-line-height-body-text
|
|
--rh-size-icon-02
|
|
--rh-space-lg
|
|
--rh-space-xl
|
|
--rh-space-xs
|
|
Other libraries
To learn more about our other libraries, visit this page.
Feedback
To give feedback about anything on this page, contact us.