| 1 |
import { Children, useState, useEffect } from 'react'; |
| 2 |
import { css } from '@patternfly/react-styles'; |
| 3 |
import styles from '@patternfly/react-styles/css/components/Alert/alert-group'; |
| 4 |
import { AlertGroupProps } from './AlertGroup'; |
| 5 |
import { AlertProps } from '../Alert'; |
| 6 |
import { AlertGroupContext } from './AlertGroupContext'; |
| 7 |
import { useHasAnimations } from '../../helpers'; |
| 8 |
|
| 9 |
export const AlertGroupInline: React.FunctionComponent<AlertGroupProps> = ({ |
| 10 |
className, |
| 11 |
children, |
| 12 |
hasAnimations: hasAnimationsProp, |
| 13 |
isToast, |
| 14 |
isLiveRegion, |
| 15 |
onOverflowClick, |
| 16 |
overflowMessage, |
| 17 |
...props |
| 18 |
}: AlertGroupProps) => { |
| 19 |
const hasAnimations = useHasAnimations(hasAnimationsProp); |
| 20 |
const [handleTransitionEnd, setHandleTransitionEnd] = useState<() => void>(() => () => {}); |
| 21 |
|
| 22 |
const updateTransitionEnd = (onTransitionEnd: () => void) => { |
| 23 |
setHandleTransitionEnd(() => onTransitionEnd); |
| 24 |
}; |
| 25 |
|
| 26 |
// Clear transition callback on unmount to prevent memory leaks |
| 27 |
useEffect( |
| 28 |
() => () => { |
| 29 |
setHandleTransitionEnd(() => () => {}); |
| 30 |
}, |
| 31 |
[] |
| 32 |
); |
| 33 |
|
| 34 |
const onTransitionEnd = (event: React.TransitionEvent<HTMLLIElement>) => { |
| 35 |
if (!hasAnimations) { |
| 36 |
return; |
| 37 |
} |
| 38 |
|
| 39 |
const prefersReducedMotion = !window.matchMedia('(prefers-reduced-motion: no-preference)')?.matches; |
| 40 |
if ( |
| 41 |
// If a user has no motion preference, we want to target the grid template rows transition |
| 42 |
// so that the onClose is called after the "slide up" animation of other alerts finishes. |
| 43 |
// If they have motion preference, we don't need to check for a specific transition since only opacity should fire. |
| 44 |
(prefersReducedMotion || (!prefersReducedMotion && event.propertyName === 'grid-template-rows')) && |
| 45 |
(event.target as HTMLElement).className.includes(styles.modifiers.offstageRight) |
| 46 |
) { |
| 47 |
handleTransitionEnd(); |
| 48 |
// Clear the callback after execution to prevent memory retention |
| 49 |
setHandleTransitionEnd(() => () => {}); |
| 50 |
} |
| 51 |
}; |
| 52 |
|
| 53 |
return ( |
| 54 |
<AlertGroupContext.Provider value={{ hasAnimations, updateTransitionEnd }}> |
| 55 |
<ul |
| 56 |
role="list" |
| 57 |
aria-live={isLiveRegion ? 'polite' : null} |
| 58 |
aria-atomic={isLiveRegion ? false : null} |
| 59 |
className={css(styles.alertGroup, className, isToast ? styles.modifiers.toast : '')} |
| 60 |
{...props} |
| 61 |
> |
| 62 |
{Children.toArray(children).map((alert, index) => ( |
| 63 |
<li |
| 64 |
onTransitionEnd={onTransitionEnd} |
| 65 |
className={css(styles.alertGroupItem, hasAnimations && styles.modifiers.offstageTop)} |
| 66 |
key={ |
| 67 |
(alert as React.ReactElement<AlertProps>).props?.id || |
| 68 |
`alertGroupItem-${(alert as React.ReactElement<AlertProps>).key}` || |
| 69 |
index |
| 70 |
} |
| 71 |
> |
| 72 |
{alert} |
| 73 |
</li> |
| 74 |
))} |
| 75 |
{overflowMessage && ( |
| 76 |
<li> |
| 77 |
<button onClick={onOverflowClick} className={css(styles.alertGroupOverflowButton)}> |
| 78 |
{overflowMessage} |
| 79 |
</button> |
| 80 |
</li> |
| 81 |
)} |
| 82 |
</ul> |
| 83 |
</AlertGroupContext.Provider> |
| 84 |
); |
| 85 |
}; |
| 86 |
|
| 87 |
AlertGroupInline.displayName = 'AlertGroupInline'; |