forked from patternfly/patternfly-react
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAlert.tsx
More file actions
259 lines (245 loc) · 8.41 KB
/
Alert.tsx
File metadata and controls
259 lines (245 loc) · 8.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
import { Fragment, useEffect, useRef, useState } from 'react';
import { css } from '@patternfly/react-styles';
import styles from '@patternfly/react-styles/css/components/Alert/alert';
import { AlertIcon } from './AlertIcon';
import { capitalize, useOUIAProps, OUIAProps } from '../../helpers';
import { AlertContext } from './AlertContext';
import maxLines from '@patternfly/react-tokens/dist/esm/c_alert__title_max_lines';
import { Tooltip, TooltipPosition } from '../Tooltip';
import { AlertToggleExpandButton } from './AlertToggleExpandButton';
export enum AlertVariant {
success = 'success',
danger = 'danger',
warning = 'warning',
info = 'info',
custom = 'custom'
}
/** The main alert component. */
export interface AlertProps extends Omit<React.HTMLProps<HTMLDivElement>, 'action' | 'title'>, OUIAProps {
/** Close button; use the alert action close button component. */
actionClose?: React.ReactNode;
/** Action links; use a single alert action link component or multiple wrapped in an array
* or React fragment.
*/
actionLinks?: React.ReactNode;
/** Content rendered inside the alert. */
children?: React.ReactNode;
/** Additional classes to add to the alert. */
className?: string;
/** Set a custom icon to the alert. If not set the icon is set according to the variant. */
customIcon?: React.ReactNode;
/** Uniquely identifies the alert. */
id?: string;
/** Flag indicating that the alert is expandable. */
isExpandable?: boolean;
/** Flag to indicate if the alert is inline. */
isInline?: boolean;
/** Flag to indicate if the alert is in a live region. */
isLiveRegion?: boolean;
/** Flag to indicate if the alert is plain. */
isPlain?: boolean;
/** Function to be executed on alert timeout. Relevant when the timeout prop is set. */
onTimeout?: () => void;
/** If set to true, the timeout is 8000 milliseconds. If a number is provided, alert will
* be dismissed after that amount of time in milliseconds.
*/
timeout?: number | boolean;
/** If the user hovers over the alert and `timeout` expires, this is how long to wait
* before finally dismissing the alert.
*/
timeoutAnimation?: number;
/** Title of the alert. */
title: React.ReactNode;
/** Sets the element to use as the alert title. Default is h4. */
component?: keyof React.JSX.IntrinsicElements;
/** Adds accessible text to the alert toggle. */
toggleAriaLabel?: string;
/** Position of the tooltip which is displayed if text is truncated. */
tooltipPosition?:
| TooltipPosition
| 'auto'
| 'top'
| 'bottom'
| 'left'
| 'right'
| 'top-start'
| 'top-end'
| 'bottom-start'
| 'bottom-end'
| 'left-start'
| 'left-end'
| 'right-start'
| 'right-end';
/** Truncate title to number of lines. */
truncateTitle?: number;
/** Adds alert variant styles. */
variant?: 'success' | 'danger' | 'warning' | 'info' | 'custom';
/** Variant label text for screen readers. */
variantLabel?: string;
/** Value to overwrite the randomly generated data-ouia-component-id.*/
ouiaId?: number | string;
/** Set the value of data-ouia-safe. Only set to true when the component is in a static state, i.e. no animations are occurring. At all other times, this value must be false. */
ouiaSafe?: boolean;
}
export const Alert: React.FunctionComponent<AlertProps> = ({
variant = AlertVariant.custom,
isInline = false,
isPlain = false,
isLiveRegion = false,
variantLabel = `${capitalize(variant)} alert:`,
actionClose,
actionLinks,
title,
component = 'h4',
children = '',
className = '',
ouiaId,
ouiaSafe = true,
timeout = false,
timeoutAnimation = 3000,
onTimeout = () => {},
truncateTitle = 0,
tooltipPosition,
customIcon,
isExpandable = false,
toggleAriaLabel = `${capitalize(variant)} alert details`,
onMouseEnter = () => {},
onMouseLeave = () => {},
id,
...props
}: AlertProps) => {
const ouiaProps = useOUIAProps(Alert.displayName, ouiaId, ouiaSafe, variant);
const getHeadingContent = (
<Fragment>
<span className="pf-v6-screen-reader">{variantLabel}</span>
{title}
</Fragment>
);
const titleRef = useRef(null);
const TitleComponent = component as any;
const divRef = useRef<HTMLDivElement>(undefined);
const [isTooltipVisible, setIsTooltipVisible] = useState(false);
useEffect(() => {
if (!titleRef.current || !truncateTitle) {
return;
}
titleRef.current.style.setProperty(maxLines.name, truncateTitle.toString());
const showTooltip = titleRef.current && titleRef.current.offsetHeight < titleRef.current.scrollHeight;
if (isTooltipVisible !== showTooltip) {
setIsTooltipVisible(showTooltip);
}
}, [titleRef, truncateTitle, isTooltipVisible]);
const [timedOut, setTimedOut] = useState(false);
const [timedOutAnimation, setTimedOutAnimation] = useState(true);
const [isMouseOver, setIsMouseOver] = useState<boolean | undefined>();
const [containsFocus, setContainsFocus] = useState<boolean | undefined>();
const dismissed = timedOut && timedOutAnimation && !isMouseOver && !containsFocus;
useEffect(() => {
const calculatedTimeout = timeout === true ? 8000 : Number(timeout);
if (calculatedTimeout > 0) {
const timer = setTimeout(() => setTimedOut(true), calculatedTimeout);
return () => clearTimeout(timer);
}
}, [timeout]);
useEffect(() => {
const onDocumentFocus = () => {
if (divRef.current) {
if (divRef.current.contains(document.activeElement)) {
setContainsFocus(true);
setTimedOutAnimation(false);
} else if (containsFocus) {
setContainsFocus(false);
}
}
};
document.addEventListener('focus', onDocumentFocus, true);
return () => document.removeEventListener('focus', onDocumentFocus, true);
}, [containsFocus]);
useEffect(() => {
if (containsFocus === false || isMouseOver === false) {
const timer = setTimeout(() => setTimedOutAnimation(true), timeoutAnimation);
return () => clearTimeout(timer);
}
}, [containsFocus, isMouseOver, timeoutAnimation]);
useEffect(() => {
dismissed && onTimeout();
}, [dismissed, onTimeout]);
const [isExpanded, setIsExpanded] = useState(false);
const onToggleExpand = () => {
setIsExpanded(!isExpanded);
};
const myOnMouseEnter = (ev: React.MouseEvent<HTMLDivElement>) => {
setIsMouseOver(true);
setTimedOutAnimation(false);
onMouseEnter(ev);
};
const myOnMouseLeave = (ev: React.MouseEvent<HTMLDivElement>) => {
setIsMouseOver(false);
onMouseLeave(ev);
};
if (dismissed) {
return null;
}
const Title = (
<TitleComponent
{...(isTooltipVisible && { tabIndex: 0 })}
ref={titleRef}
className={css(styles.alertTitle, truncateTitle && styles.modifiers.truncate)}
>
{getHeadingContent}
</TitleComponent>
);
return (
<div
ref={divRef}
className={css(
styles.alert,
isInline && styles.modifiers.inline,
isPlain && styles.modifiers.plain,
isExpandable && styles.modifiers.expandable,
isExpanded && styles.modifiers.expanded,
styles.modifiers[variant],
className
)}
{...ouiaProps}
{...(isLiveRegion && {
'aria-live': 'polite',
'aria-atomic': 'false'
})}
onMouseEnter={myOnMouseEnter}
onMouseLeave={myOnMouseLeave}
id={id}
{...props}
>
{isExpandable && (
<AlertContext.Provider value={{ title, variantLabel }}>
<div className={css(styles.alertToggle)}>
<AlertToggleExpandButton
isExpanded={isExpanded}
onToggleExpand={onToggleExpand}
aria-label={toggleAriaLabel}
/>
</div>
</AlertContext.Provider>
)}
<AlertIcon variant={variant} customIcon={customIcon} />
{isTooltipVisible ? (
<Tooltip content={getHeadingContent} position={tooltipPosition}>
{Title}
</Tooltip>
) : (
Title
)}
{actionClose && (
<AlertContext.Provider value={{ title, variantLabel }}>
<div className={css(styles.alertAction)}>{actionClose}</div>
</AlertContext.Provider>
)}
{children && (!isExpandable || (isExpandable && isExpanded)) && (
<div className={css(styles.alertDescription)}>{children}</div>
)}
{actionLinks && <div className={css(styles.alertActionGroup)}>{actionLinks}</div>}
</div>
);
};
Alert.displayName = 'Alert';