Debian Codegraph @

node/@patternfly/react-core/src/components/Alert/AlertGroup.tsx

raw
1 import { useEffect, useRef, useState } from 'react';
2 import * as ReactDOM from 'react-dom';
3 import { canUseDOM } from '../../helpers';
4 import { AlertGroupInline } from './AlertGroupInline';
5 import { useHasAnimations } from '../../helpers';
6
7 export interface AlertGroupProps extends Omit<React.HTMLProps<HTMLUListElement>, 'className'> {
8 /** Additional classes added to the AlertGroup */
9 className?: string;
10 /** Alerts to be rendered in the AlertGroup */
11 children?: React.ReactNode;
12 /** @beta Flag to indicate whether Alerts are animated upon rendering and being dismissed. This is intended
13 * to remain false for testing purposes only.
14 */
15 hasAnimations?: boolean;
16 /** Toast notifications are positioned at the top right corner of the viewport */
17 isToast?: boolean;
18 /** Turns the container into a live region so that changes to content within the AlertGroup, such as appending an Alert, are reliably announced to assistive technology. */
19 isLiveRegion?: boolean;
20 /** Determine where the alert is appended to */
21 appendTo?: HTMLElement | (() => HTMLElement);
22 /** Function to call if user clicks on overflow message */
23 onOverflowClick?: () => void;
24 /** Custom text to show for the overflow message */
25 overflowMessage?: string;
26 /** Adds an accessible label to the alert group. */
27 'aria-label'?: string;
28 }
29
30 export const AlertGroup: React.FunctionComponent<AlertGroupProps> = ({
31 className,
32 children,
33 hasAnimations: hasAnimationsProp,
34 isToast,
35 isLiveRegion,
36 onOverflowClick,
37 overflowMessage,
38 'aria-label': ariaLabel,
39
40 appendTo,
41 ...props
42 }: AlertGroupProps) => {
43 const containerRef = useRef<HTMLElement | null>(null);
44 const [isContainerReady, setIsContainerReady] = useState(false);
45 const hasAnimations = useHasAnimations(hasAnimationsProp);
46
47 const getTargetElement = () => {
48 if (typeof appendTo === 'function') {
49 return appendTo();
50 }
51 return appendTo || document.body;
52 };
53
54 useEffect(() => {
55 if (isToast && canUseDOM) {
56 const container = document.createElement('div');
57 const target = getTargetElement();
58 containerRef.current = container;
59 target.appendChild(container);
60 setIsContainerReady(true);
61
62 return () => {
63 if (containerRef.current) {
64 target.removeChild(containerRef.current);
65 containerRef.current = null;
66 }
67 setIsContainerReady(false);
68 };
69 }
70 }, [isToast, appendTo]);
71
72 const alertGroup = (
73 <AlertGroupInline
74 onOverflowClick={onOverflowClick}
75 className={className}
76 isToast={isToast}
77 isLiveRegion={isLiveRegion}
78 overflowMessage={overflowMessage}
79 aria-label={ariaLabel}
80 hasAnimations={hasAnimations}
81 {...props}
82 >
83 {children}
84 </AlertGroupInline>
85 );
86
87 if (!isToast) {
88 return alertGroup;
89 }
90
91 const container = containerRef.current;
92
93 if (!canUseDOM || !container || !isContainerReady) {
94 return null;
95 }
96
97 return ReactDOM.createPortal(alertGroup, container);
98 };
99 AlertGroup.displayName = 'AlertGroup';