|
| 1 | +# Alert |
| 2 | +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. |
| 3 | + |
| 4 | +## Usage |
| 5 | +An alert is used by wrapping html elements with an `rh-alert` element as slots in order to format the data in the desired manner. |
| 6 | + |
| 7 | +An alert consists of the following slots: |
| 8 | + |
| 9 | +`Header` |
| 10 | +- Header text for the alert, appears at the top of the alert. |
| 11 | + |
| 12 | +`Actions` |
| 13 | +- Buttons that can be used to perform an action from the alert, such as closing or accepting the alert. |
| 14 | + |
| 15 | +`Anonymous Slot` |
| 16 | +- This is the text that is inserted into the state to be displayed in the main content body. |
| 17 | + |
| 18 | +An alert consists of the following attributes: |
| 19 | + |
| 20 | +`dismissable` |
| 21 | +- The `dismissable` attribute adds a close button to the top right of the alert allowing the user to dismiss the alert. Clicking the close button dispatches a `close` event, then removes the alert from the page. |
| 22 | + |
| 23 | +An alert has the following events: |
| 24 | + |
| 25 | +`close` |
| 26 | +- This is fired when the user clicks the close button enabled by the `dismissable` attribute. Cancelling the event prevents the alert from closing. |
| 27 | + |
| 28 | +## Installation |
| 29 | + |
| 30 | +If using npm/bundlers: |
| 31 | +```bash |
| 32 | +npm install @rhds/elements |
| 33 | +``` |
| 34 | + |
| 35 | +Then once installed, import it to your application: |
| 36 | + |
| 37 | +```js |
| 38 | +import '@patternfly/elements/pf-alert/pf-alert.js'; |
| 39 | +``` |
| 40 | +## Usage |
| 41 | + |
| 42 | +### Basic Alert |
| 43 | + |
| 44 | +```html |
| 45 | +<pf-alert> |
| 46 | + <h3 slot="header">Default</h3> |
| 47 | + <p>Example Alert</p> |
| 48 | + <pf-button variant="secondary" slot="actions" data-action="dismiss">Dismiss</pf-button> |
| 49 | + <pf-button variant="link" slot="actions" data-action="confirm">Confirm</pf-button> |
| 50 | +</pf-alert> |
| 51 | +``` |
| 52 | + |
| 53 | +### Info Alert (also available `success`, `warning`, `danger`, and `error`) |
| 54 | + |
| 55 | +```html |
| 56 | +<pf-alert state="info"> |
| 57 | + <h3 slot="header">Info</h3> |
| 58 | + <p>Example Alert</p> |
| 59 | + <pf-button variant="secondary" slot="actions" data-action="dismiss">Dismiss</pf-button> |
| 60 | + <pf-button variant="link" slot="actions" data-action="confirm">Confirm</pf-button> |
| 61 | +</pf-alert> |
| 62 | +``` |
| 63 | + |
| 64 | +### Inline Alert |
| 65 | +```html |
| 66 | +<pf-alert variant="inline"> |
| 67 | + <h3 slot="header">Default</h3> |
| 68 | + <p>Example Alert</p> |
| 69 | + <pf-button variant="secondary" slot="actions" data-action="dismiss">Dismiss</pf-button> |
| 70 | + <pf-button variant="link" slot="actions" data-action="confirm">Confirm</pf-button> |
| 71 | +</pf-alert> |
| 72 | +``` |
| 73 | + |
| 74 | +### Toast Alert |
| 75 | + |
| 76 | +Alerts may be toasted using the `toast()` function. A toasted alert has a |
| 77 | +unique style, a drop shadow, and either disappears after eight seconds, or can persist until the user dismisses it. |
| 78 | + |
| 79 | +```js |
| 80 | +import { PfAlert } from '@patternfly/elements/pf-alert/pf-alert.js'; |
| 81 | + |
| 82 | +await PfAlert.toast({ |
| 83 | + heading: 'Toast alert', |
| 84 | + message: 'Example toasted alert', |
| 85 | + actions: [ |
| 86 | + { variant: 'secondary', action: 'confirm', text: 'Confirm' }, |
| 87 | + { action: 'dismiss', text: 'Dismiss' } |
| 88 | + ], |
| 89 | +}); |
| 90 | + |
| 91 | +``` |
| 92 | + |
| 93 | +You can respond to the various actions by listening for the `close` event |
| 94 | + |
| 95 | +```js |
| 96 | +document.addEventListener('close', function(event) { |
| 97 | + if (event.target instanceof PfAlert) { |
| 98 | + switch (event.reason) { |
| 99 | + case 'close': // handle alert close ("X" button) |
| 100 | + case 'dismiss': // handle alert dismiss (data-action="dismiss") |
| 101 | + case 'confirm': // handle alert confirm (data-action="confirm") |
| 102 | + } |
| 103 | + } |
| 104 | +}); |
| 105 | + |
| 106 | +``` |
| 107 | + |
| 108 | +### Adding an Event Listener to a Basic Alert |
| 109 | + |
| 110 | +If you would like to add custom logic when the alert is closed, you can do so with JavaScript. |
| 111 | +```js |
| 112 | +// Query for the alert element |
| 113 | +const alert = document.querySelector('pf-alert'); |
| 114 | +alert.addEventListener('close', () => { |
| 115 | + // Add code to be executed when the alert element is closed. |
| 116 | +}); |
| 117 | +``` |
| 118 | + |
| 119 | +### Preventing the default close behavior in a Basic Alert |
| 120 | +```js |
| 121 | +// Query for the alert element |
| 122 | +const alert = document.querySelector('pf-alert'); |
| 123 | +alert.addEventListener('close', async function (event) { |
| 124 | + // Prevent the alert from closing |
| 125 | + event.preventDefault(); |
| 126 | + // Perform some async task |
| 127 | + await alertClosed(); |
| 128 | + // Close the alert when finished |
| 129 | + this.remove(); |
| 130 | +}); |
| 131 | +``` |
0 commit comments