1 | ---
|
2 | title: Alert
|
3 | storybookPath: feedback-overlays-alert--info
|
4 | isExperimentalPackage: true
|
5 | ---
|
6 |
|
7 | Alert displays a short and concise message to draw a users' attention without
|
8 | interrupting their current task.
|
9 |
|
10 | ## Examples
|
11 |
|
12 | ### Tones
|
13 |
|
14 | The component offers several levels of tonal severity.
|
15 |
|
16 | ```jsx live
|
17 | <Stack gap="large">
|
18 | <Alert tone="caution" heading="This is a caution alert">
|
19 | Caution message
|
20 | </Alert>
|
21 | <Alert tone="positive" heading="This is a positive alert">
|
22 | Positive message
|
23 | </Alert>
|
24 | <Alert tone="info" heading="This is an info alert">
|
25 | Info message
|
26 | </Alert>
|
27 | <Alert tone="critical" heading="This is a critical alert">
|
28 | Critical message
|
29 | </Alert>
|
30 | </Stack>
|
31 | ```
|
32 |
|
33 | ### Closing
|
34 |
|
35 | You can also set an `onClose` prop callback function which will render a close
|
36 | icon button on the alert. The callback function will be called upon the button
|
37 | being pressed.
|
38 |
|
39 | ```jsx live
|
40 | const [isOpen, setIsOpen] = React.useState(true);
|
41 |
|
42 | React.useEffect(() => {
|
43 | const timeout = setTimeout(() => {
|
44 | if (!isOpen) {
|
45 | setIsOpen(true);
|
46 | }
|
47 | }, 2000);
|
48 |
|
49 | return () => clearTimeout(timeout);
|
50 | }, [isOpen]);
|
51 |
|
52 | if (!isOpen) {
|
53 | return null;
|
54 | }
|
55 |
|
56 | return (
|
57 | <Alert
|
58 | tone="caution"
|
59 | heading="This is a caution alert"
|
60 | onClose={() => setIsOpen(false)}
|
61 | >
|
62 | Click the button on the right to dismiss this notification
|
63 | </Alert>
|
64 | );
|
65 | ```
|
66 |
|
67 | ### Custom icons
|
68 |
|
69 | In rare cases, you may need to provide a custom icon. To do so, we expose an
|
70 | `icon` prop. You can pass it any icon from the [Icon package](/package/icon).
|
71 |
|
72 | ```jsx live
|
73 | <Alert heading="This an info alert" icon={LightBulbIcon}>
|
74 | Did you know that Alert components can have custom icons?
|
75 | </Alert>
|
76 | ```
|
77 |
|
78 | ## Props
|
79 |
|
80 | <PropsTable displayName="Alert" />
|
81 |
|
82 | [data-attribute-map]:
|
83 | https://github.com/brighte-labs/spark-web/blob/e7f6f4285b4cfd876312cc89fbdd094039aa239a/packages/utils/src/internal/buildDataAttributes.ts#L1
|