Alert

OverviewStyleGuidelinesCodeAccessibilityDemos

Installation

We recommend import maps when building pages with RHDS. Learn more about how to install on our getting started docs.

Red Hat CDN
<script type="importmap">
{
  "imports": {
    "@rhds/elements/": "https://www.redhatstatic.com/dx/v1-alpha/@rhds/elements@2.1.1/elements/",
  }
}
</script>
Copy to Clipboard Wrap lines
NPM
npm install @rhds/elements
Copy to Clipboard Wrap lines
JSPM
<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 Wrap 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 Wrap 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 Wrap 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.

Slots 3
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

Attributes 3
Attribute DOM Property Description Type Default
state state

Communicates the urgency of a message and is denoted by various styling configurations.

  • neutral - Indicates generic information or a message with no severity.
  • danger - Indicates a danger state, like an error that is blocking a user from completing a task.
  • warning - Indicates a warning state, like a non-blocking error that might need to be fixed.
  • caution - Indicates an action or notice which should immediately draw the attention
  • info - Indicates helpful information or a message with very little to no severity.
  • success - Indicates a success state, like if a process was completed without errors.
| 'danger' | 'warning' | 'caution' | 'neutral' | 'info' | 'success' | 'note' // deprecated | 'default' // deprecated | 'error'
'neutral'
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.

'alternate' | 'toast' | 'inline' | undefined
unknown
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.

boolean
false
Methods 0
None
Events 1
Event Name Description
close

when the dismissable alert closes

CSS Shadow Parts 0
None
CSS Custom Properties 0
None
Design Tokens 37
Token Copy
--rh-border-width-md
Full CSS Variable Permalink to this token
--rh-box-shadow-xl
Full CSS Variable Permalink to this token
--rh-color-icon-status-caution
Full CSS Variable Permalink to this token
--rh-color-icon-status-danger
Full CSS Variable Permalink to this token
--rh-color-icon-status-info
Full CSS Variable Permalink to this token
--rh-color-icon-status-neutral
Full CSS Variable Permalink to this token
--rh-color-icon-status-success
Full CSS Variable Permalink to this token
--rh-color-icon-status-warning
Full CSS Variable Permalink to this token
--rh-color-interactive-primary-default
Full CSS Variable Permalink to this token
--rh-color-interactive-primary-hover
Full CSS Variable Permalink to this token
--rh-color-status-caution
Full CSS Variable Permalink to this token
--rh-color-status-danger
Full CSS Variable Permalink to this token
--rh-color-status-info
Full CSS Variable Permalink to this token
--rh-color-status-neutral
Full CSS Variable Permalink to this token
--rh-color-status-success
Full CSS Variable Permalink to this token
--rh-color-status-warning
Full CSS Variable Permalink to this token
--rh-color-surface-lightest
Full CSS Variable Permalink to this token
--rh-color-surface-status-caution
Full CSS Variable Permalink to this token
--rh-color-surface-status-danger
Full CSS Variable Permalink to this token
--rh-color-surface-status-info
Full CSS Variable Permalink to this token
--rh-color-surface-status-neutral
Full CSS Variable Permalink to this token
--rh-color-surface-status-success
Full CSS Variable Permalink to this token
--rh-color-surface-status-warning
Full CSS Variable Permalink to this token
--rh-color-teal-10
Full CSS Variable Permalink to this token
--rh-color-teal-50
Full CSS Variable Permalink to this token
--rh-color-text-primary
Full CSS Variable Permalink to this token
--rh-color-text-secondary
Full CSS Variable Permalink to this token
--rh-font-family-body-text
Full CSS Variable Permalink to this token
--rh-font-size-body-text-sm
Full CSS Variable Permalink to this token
--rh-font-weight-body-text-medium
Full CSS Variable Permalink to this token
--rh-font-weight-body-text-regular
Full CSS Variable Permalink to this token
--rh-length-xl
Full CSS Variable Permalink to this token
--rh-line-height-body-text
Full CSS Variable Permalink to this token
--rh-size-icon-02
Full CSS Variable Permalink to this token
--rh-space-lg
Full CSS Variable Permalink to this token
--rh-space-xl
Full CSS Variable Permalink to this token
--rh-space-xs
Full CSS Variable Permalink to this token