Debian Codegraph @

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

raw
1 import { useRef, useContext } from 'react';
2 import { Button, ButtonVariant, ButtonProps } from '../Button';
3 import RhMicronsCloseIcon from '@patternfly/react-icons/dist/esm/icons/rh-microns-close-icon';
4 import { AlertContext } from './AlertContext';
5 import { AlertGroupContext } from './AlertGroupContext';
6 import alertGroupStyles from '@patternfly/react-styles/css/components/Alert/alert-group';
7
8 /** Renders a close button for a dismissable alert when this sub-component is passed into
9 * the alert's actionClose property.
10 */
11
12 export interface AlertActionCloseButtonProps extends ButtonProps {
13 /** Accessible label for the close button */
14 'aria-label'?: string;
15 /** Additional classes added to the alert action close button. */
16 className?: string;
17 /** A callback for when the close button is clicked. */
18 onClose?: () => void;
19 /** Variant Label for the close button. */
20 variantLabel?: string;
21 }
22
23 export const AlertActionCloseButton: React.FunctionComponent<AlertActionCloseButtonProps> = ({
24 className,
25 onClose = () => undefined as any,
26 'aria-label': ariaLabel = '',
27 variantLabel,
28 ...props
29 }: AlertActionCloseButtonProps) => {
30 const closeButtonRef = useRef(null);
31 const { hasAnimations, updateTransitionEnd } = useContext(AlertGroupContext);
32 const { offstageRight } = alertGroupStyles.modifiers;
33
34 const getParentAlertGroupItem = () => closeButtonRef.current?.closest(`.${alertGroupStyles.alertGroupItem}`);
35 const handleOnClick = () => {
36 if (hasAnimations) {
37 getParentAlertGroupItem()?.classList.add(offstageRight);
38 updateTransitionEnd(onClose);
39 } else {
40 onClose();
41 }
42 };
43
44 return (
45 <AlertContext.Consumer>
46 {({ title, variantLabel: alertVariantLabel }) => (
47 <Button
48 ref={closeButtonRef}
49 variant={ButtonVariant.plain}
50 onClick={handleOnClick}
51 aria-label={ariaLabel === '' ? `Close ${variantLabel || alertVariantLabel} alert: ${title}` : ariaLabel}
52 className={className}
53 icon={<RhMicronsCloseIcon />}
54 {...props}
55 />
56 )}
57 </AlertContext.Consumer>
58 );
59 };
60 AlertActionCloseButton.displayName = 'AlertActionCloseButton';