Debian Codegraph @

node/@patternfly/react-core/src/components/Alert/examples/Alert.md

raw
1 ---
2 id: Alert
3 section: components
4 cssPrefix: ['pf-v6-c-alert', 'pf-v6-c-alert-group']
5 propComponents: ['Alert', 'AlertGroup', 'AlertActionCloseButton', 'AlertActionLink']
6 ouia: true
7 ---
8
9 import './alert.css';
10 import { Fragment, useEffect, useState } from 'react';
11 import RhUiUsersFillIcon from '@patternfly/react-icons/dist/esm/icons/rh-ui-users-fill-icon';
12 import RhUiContainerFillIcon from '@patternfly/react-icons/dist/esm/icons/rh-ui-container-fill-icon';
13 import DatabaseIcon from '@patternfly/react-icons/dist/esm/icons/database-icon';
14 import RhUiServerStackFillIcon from '@patternfly/react-icons/dist/esm/icons/rh-ui-server-stack-fill-icon';
15 import LaptopIcon from '@patternfly/react-icons/dist/esm/icons/laptop-icon';
16 import buttonStyles from '@patternfly/react-styles/css/components/Button/button';
17
18 Micro animations have been added for `<Alert>` components within an `<AlertGroup>`. By default, you must opt into animations, since they can require updates to tests. To enable or disable animations as needed, use the `hasAnimations` property. With animations enabled, we recommend you ensure that dynamically-added alerts are prepended to a list of alerts, rather than appended to the end of it.
19
20 Micro animations are turned on for all examples and demos where alerts are dynamically added.
21
22 ## Alert examples
23
24 ### Alert variants
25
26 PatternFly supports several alert variants for different scenarios. Each variant has an associated status icon, background, and alert title coded to communicate the severity of an alert. Use the `variant` property to apply the following styling options. If no `variant` is specified, then the variant will be set to "custom".
27
28 | Variant | Description |
29 | ------- | -------------------------------------------------------------------------------------------------------------------------------------------------------- |
30 | Custom | Use for generic messages that should have a custom color set by the associated CSS variable. Should be used when the message has no associated severity. |
31 | Info | Use for general informational messages |
32 | Success | Use to indicate that a task or process has been completed successfully |
33 | Warning | Use to indicate that a non-critical error has occurred |
34 | Danger | Use to indicate that a critical or blocking error has occurred |
35
36 ```ts file = "AlertVariants.tsx"
37
38 ```
39
40 ### Alert variations
41
42 PatternFly supports several properties and variations that can be used to add extra content to an alert.
43
44 - As demonstrated in the 1st variation below, use the `actionLinks` property to add one or more `<AlertActionLink>` components that place links beneath the alert message. You must pass in `href` and `component="a"` properties to have an `<AlertActionLink>` act as a proper link, rather than as a button.
45
46 - As demonstrated in the 2nd variation below, use a native HTML `<a>` element to add links within an alert message.
47
48 - As demonstrated in the 3rd variation below, use the `actionClose` property to add an `<AlertActionCloseButton>` component, which can be used to manage and customize alert dismissals.
49
50 - As demonstrated in the 4th and 5th variations below, use the `component` property to set the element for an alert title.
51 - If there is not a description passed via `children` prop, then the `component` prop should be set to a non-heading element such as a `span` or `div`.
52 - If there is a description passed via `children` prop, then the `component` prop should be a heading element. Headings should be ordered by their level and heading levels should not be skipped. For example, a heading of an `h2` level should not be followed directly by an `h4`.
53
54 ```ts file = "AlertVariations.tsx"
55
56 ```
57
58 ### Alert timeout
59
60 Use the `timeout` property to automatically dismiss an alert after a period of time. If set to `true`, the `timeout` will be 8000 milliseconds. Provide a specific value to dismiss the alert after a different number of milliseconds.
61
62 ```ts file = "AlertTimeout.tsx"
63
64 ```
65
66 ### Expandable alerts
67
68 An alert can contain additional, hidden information that is made visible when users click a caret icon. This information can be expanded and collapsed each time the icon is clicked.
69
70 It is not recommended to use an expandable alert with a `timeout` in a [toast alert group](/components/alert#toast-alert-group) because the alert could timeout before users have time to interact with and view the entire alert.
71
72 See the [toast alert considerations](/components/alert/accessibility#toast-alerts) section of the alert accessibility documentation to understand the accessibility risks associated with using toast alerts.
73
74 ```ts file = "AlertExpandable.tsx"
75
76 ```
77
78 ### Truncated alerts
79
80 Use the `truncateTitle` property to shorten a long `title`. Set `truncateTitle` equal to a number (passed in as `{n}`) to reduce the number of lines of text in the alert's `title`. Users may hover over or tab to a truncated `title` to see the full message in a tooltip.
81
82 ```ts file = "AlertTruncated.tsx"
83
84 ```
85
86 ### Custom icons
87
88 Use the `customIcon` property to replace a default alert icon with a custom icon.
89
90 ```ts file = "AlertCustomIcons.tsx"
91
92 ```
93
94 ### Inline alerts variants
95
96 Use inline alerts to display an alert inline with content. All alert variants may use the `isInline` property to position alerts in content-heavy areas, such as within forms, wizards, or drawers.
97
98 ```ts file = "AlertInlineVariants.tsx"
99
100 ```
101
102 ### Inline alert variations
103
104 All general alert variations can use the `isInline` property to apply inline styling.
105
106 ```ts file = "AlertInlineVariations.tsx"
107
108 ```
109
110 ### Plain inline alert variants
111
112 Use the `isPlain` property to make any inline alert plain. Plain styling removes the colored background but keeps colored text and icons.
113
114 ```ts file = "AlertPlainInlineVariants.tsx"
115
116 ```
117
118 ### Plain inline alert variations
119
120 It is not recommended to use a plain inline alert with `actionClose` nor `actionLinks` because these alerts are non-dismissible and should persist until the error or action related to the alert is resolved.
121
122 ```ts file = "AlertPlainInlineVariations.tsx"
123
124 ```
125
126 ### Static live region alerts
127
128 Live region alerts allow you to expose dynamic content changes in a way that can be announced by assistive technologies.
129
130 By default, `isLiveRegion`alerts are static.
131
132 ```ts file = "AlertStaticLiveRegion.tsx"
133
134 ```
135
136 ### Dynamic live region alerts
137
138 Alerts that are asynchronously appended into dynamic [alert groups](/components/alert/#alert-group-examples) via the `isLiveRegion` property will be announced to assistive technology the moment the change happens, following the strategy used for `aria-atomic`, which defaults to false. This means only changes of type "addition" will be announced.
139
140 ```ts file="AlertDynamicLiveRegion.tsx"
141
142 ```
143
144 ### Asynchronous live region alerts
145
146 This example shows how an alert could be triggered by an asynchronous event in the application. Note that you can customize how the alert will be announced to assistive technology. See the [alert accessibility](/components/alert/accessibility/) for more information.
147
148 ```ts file="AlertAsyncLiveRegion.tsx"
149
150 ```
151
152 ## Alert group examples
153
154 An alert group stacks and positions 2 or more alerts in a live region, either in a layer over the main content of a page or inline with the page content. Alert groups should always rank alerts by age, stacking new alerts on top of old ones as they surface.
155
156 ### Alert group variants
157
158 Alert groups can be one of the following variants:
159
160 | Variant | Description |
161 | ------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
162 | Static inline | Static inline alert groups contain alerts that appear when the page loads, and are seen within the normal page content flow. These groups should not contain alerts that will dynamically appear or update. |
163 | Toast | Toast alert groups contain alerts that typically appear in response to an asynchronous event or user action. These groups are positioned on top of other content at the top right of the page. |
164 | Dynamic | Dynamic alert groups contain alerts that typically appear in response to a user action, and are seen within the normal page content flow. |
165
166 Dynamic alerts that are generated after the page initially loads must be appended to either a toast or dynamic `AlertGroup`, both of which must use the `isLiveRegion` property. New alerts appended to a toast or dynamic group will be announced by assistive technologies the moment the change happens. For information about customizing this announcement, read the [aria-atomic and aria-relevant](/components/alert/accessibility#aria-atomic-and-aria-relevant) section of the alert accessibility documentation.
167
168 ### Static inline alert group
169
170 All alert group variants may combine multiple [alert variants](/components/alert) For example, the following static inline alert group includes one "success" alert and one "info" alert.
171
172 ```ts file="AlertGroupStatic.tsx"
173
174 ```
175
176 ### Toast alert group
177
178 Toast alert groups are created by passing in the `isToast` and `isLiveRegion` properties.
179
180 Click the buttons in the example below to add alerts to a toast group.
181
182 ```ts file="AlertGroupToast.tsx"
183
184 ```
185
186 ### Toast alert group with overflow capture
187
188 Users will see an overflow message once a predefined number of alerts are displayed. They will not see any alerts beyond the display limit, which must be explicitly set.
189
190 In the following example, an overflow message will appear when more than 4 alerts would be shown. When a 5th alert would appear, an overflow message is shown instead.
191
192 When an overflow message appears in an `AlertGroup` using the `isLiveRegion` property, the "view 1 more alert" text label will be announced, but the alert message will not.
193
194 Users navigating via keyboard or another assistive technology will need a way to navigate to and reveal hidden alerts before they disappear. Alternatively, there should be a place where notifications or alerts are collected to be viewed or read later.
195
196 ```ts file="AlertGroupToastOverflowCapture.tsx"
197
198 ```
199
200 ### Asynchronous alert groups
201
202 The following example shows how alerts can be triggered by an asynchronous event in the application. You can customize how an alert will be announced to assistive technology by adjusting the value of the `aria-live` property. Click the "start async" alert button below and then click the buttons in the above toast examples to demonstrate how asynchronous events add alerts to a group. Click the "stop async alerts" button to halt this behavior.
203
204 See the [alert accessibility tab](/components/alert/accessibility) for more information on customizing this behavior.
205
206 ```ts file="AlertGroupAsync.tsx"
207
208 ```
209
210 ### Dynamic alert groups
211
212 Click the buttons in the example below to add dynamic alerts to a group.
213
214 ```ts file="AlertGroupSingularDynamic.tsx"
215
216 ```
217
218 ### Dynamic alert group with overflow message
219
220 In the following example, there can be a maximum of 4 alerts shown at once.
221
222 ```ts file="AlertGroupSingularDynamicOverflow.tsx"
223
224 ```
225
226 ### Multiple dynamic alert groups
227
228 You may add multiple alerts to an alert group at once. Click the "add alert collection" button in the example below to add a batch of 3 toast alerts to a group.
229
230 ```ts file="AlertGroupMultipleDynamic.tsx"
231
232 ```