forked from patternfly/patternfly-react
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAlertActionCloseButton.tsx
More file actions
60 lines (55 loc) · 2.13 KB
/
AlertActionCloseButton.tsx
File metadata and controls
60 lines (55 loc) · 2.13 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
import { useRef, useContext } from 'react';
import { Button, ButtonVariant, ButtonProps } from '../Button';
import RhMicronsCloseIcon from '@patternfly/react-icons/dist/esm/icons/rh-microns-close-icon';
import { AlertContext } from './AlertContext';
import { AlertGroupContext } from './AlertGroupContext';
import alertGroupStyles from '@patternfly/react-styles/css/components/Alert/alert-group';
/** Renders a close button for a dismissable alert when this sub-component is passed into
* the alert's actionClose property.
*/
export interface AlertActionCloseButtonProps extends ButtonProps {
/** Accessible label for the close button */
'aria-label'?: string;
/** Additional classes added to the alert action close button. */
className?: string;
/** A callback for when the close button is clicked. */
onClose?: () => void;
/** Variant Label for the close button. */
variantLabel?: string;
}
export const AlertActionCloseButton: React.FunctionComponent<AlertActionCloseButtonProps> = ({
className,
onClose = () => undefined as any,
'aria-label': ariaLabel = '',
variantLabel,
...props
}: AlertActionCloseButtonProps) => {
const closeButtonRef = useRef(null);
const { hasAnimations, updateTransitionEnd } = useContext(AlertGroupContext);
const { offstageRight } = alertGroupStyles.modifiers;
const getParentAlertGroupItem = () => closeButtonRef.current?.closest(`.${alertGroupStyles.alertGroupItem}`);
const handleOnClick = () => {
if (hasAnimations) {
getParentAlertGroupItem()?.classList.add(offstageRight);
updateTransitionEnd(onClose);
} else {
onClose();
}
};
return (
<AlertContext.Consumer>
{({ title, variantLabel: alertVariantLabel }) => (
<Button
ref={closeButtonRef}
variant={ButtonVariant.plain}
onClick={handleOnClick}
aria-label={ariaLabel === '' ? `Close ${variantLabel || alertVariantLabel} alert: ${title}` : ariaLabel}
className={className}
icon={<RhMicronsCloseIcon />}
{...props}
/>
)}
</AlertContext.Consumer>
);
};
AlertActionCloseButton.displayName = 'AlertActionCloseButton';