Debian Codegraph @

node/@patternfly/react-core/dist/esm/components/Alert/Alert.js

raw
1 import { __rest } from "tslib";
2 import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
3 import { Fragment, useEffect, useRef, useState, useContext } from 'react';
4 import { css } from '@patternfly/react-styles';
5 import styles from '@patternfly/react-styles/css/components/Alert/alert.mjs';
6 import alertGroupStyles from '@patternfly/react-styles/css/components/Alert/alert-group.mjs';
7 import { AlertIcon } from './AlertIcon';
8 import { capitalize, useOUIAProps } from '../../helpers';
9 import { AlertContext } from './AlertContext';
10 import { AlertGroupContext } from './AlertGroupContext';
11 import maxLines from '@patternfly/react-tokens/dist/esm/c_alert__title_max_lines';
12 import { Tooltip } from '../Tooltip';
13 import { AlertToggleExpandButton } from './AlertToggleExpandButton';
14 export var AlertVariant;
15 (function (AlertVariant) {
16 AlertVariant["success"] = "success";
17 AlertVariant["danger"] = "danger";
18 AlertVariant["warning"] = "warning";
19 AlertVariant["info"] = "info";
20 AlertVariant["custom"] = "custom";
21 })(AlertVariant || (AlertVariant = {}));
22 export const Alert = (_a) => {
23 var { 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 } = _a, props = __rest(_a, ["variant", "isInline", "isPlain", "isLiveRegion", "variantLabel", "actionClose", "actionLinks", "title", "component", "children", "className", "ouiaId", "ouiaSafe", "timeout", "timeoutAnimation", "onTimeout", "truncateTitle", "tooltipPosition", "customIcon", "isExpandable", "toggleAriaLabel", "onMouseEnter", "onMouseLeave", "id"]);
24 const ouiaProps = useOUIAProps(Alert.displayName, ouiaId, ouiaSafe, variant);
25 const getHeadingContent = (_jsxs(Fragment, { children: [_jsx("span", { className: "pf-v6-screen-reader", children: variantLabel }), title] }));
26 const titleRef = useRef(null);
27 const TitleComponent = component;
28 const divRef = useRef(undefined);
29 const [isTooltipVisible, setIsTooltipVisible] = useState(false);
30 useEffect(() => {
31 if (!titleRef.current || !truncateTitle) {
32 return;
33 }
34 titleRef.current.style.setProperty(maxLines.name, truncateTitle.toString());
35 const showTooltip = titleRef.current && titleRef.current.offsetHeight < titleRef.current.scrollHeight;
36 if (isTooltipVisible !== showTooltip) {
37 setIsTooltipVisible(showTooltip);
38 }
39 }, [titleRef, truncateTitle, isTooltipVisible]);
40 const [timedOut, setTimedOut] = useState(false);
41 const [timedOutAnimation, setTimedOutAnimation] = useState(true);
42 const [isMouseOver, setIsMouseOver] = useState();
43 const [containsFocus, setContainsFocus] = useState();
44 const shouldDismiss = timedOut && timedOutAnimation && !isMouseOver && !containsFocus;
45 const [isDismissed, setIsDismissed] = useState(false);
46 const { hasAnimations, updateTransitionEnd } = useContext(AlertGroupContext);
47 const { offstageRight } = alertGroupStyles.modifiers;
48 const getParentAlertGroupItem = () => { var _a; return (_a = divRef.current) === null || _a === void 0 ? void 0 : _a.closest(`.${alertGroupStyles.alertGroupItem}`); };
49 useEffect(() => {
50 const shouldSetDismissed = shouldDismiss && !isDismissed;
51 if (!shouldSetDismissed) {
52 return;
53 }
54 const alertGroupItem = getParentAlertGroupItem();
55 alertGroupItem === null || alertGroupItem === void 0 ? void 0 : alertGroupItem.classList.add(offstageRight);
56 if (hasAnimations) {
57 updateTransitionEnd(() => {
58 setIsDismissed(true);
59 });
60 }
61 else {
62 setIsDismissed(true);
63 }
64 }, [shouldDismiss, isDismissed]);
65 useEffect(() => {
66 const calculatedTimeout = timeout === true ? 8000 : Number(timeout);
67 if (calculatedTimeout > 0) {
68 const timer = setTimeout(() => setTimedOut(true), calculatedTimeout);
69 return () => clearTimeout(timer);
70 }
71 }, [timeout]);
72 useEffect(() => {
73 const onDocumentFocus = () => {
74 if (divRef.current) {
75 if (divRef.current.contains(document.activeElement)) {
76 setContainsFocus(true);
77 setTimedOutAnimation(false);
78 }
79 else if (containsFocus) {
80 setContainsFocus(false);
81 }
82 }
83 };
84 document.addEventListener('focus', onDocumentFocus, true);
85 return () => document.removeEventListener('focus', onDocumentFocus, true);
86 }, [containsFocus]);
87 useEffect(() => {
88 if (containsFocus === false || isMouseOver === false) {
89 const timer = setTimeout(() => setTimedOutAnimation(true), timeoutAnimation);
90 return () => clearTimeout(timer);
91 }
92 }, [containsFocus, isMouseOver, timeoutAnimation]);
93 useEffect(() => {
94 isDismissed && onTimeout();
95 }, [isDismissed, onTimeout]);
96 useEffect(() => {
97 const alertGroupItem = getParentAlertGroupItem();
98 setTimeout(() => alertGroupItem === null || alertGroupItem === void 0 ? void 0 : alertGroupItem.classList.remove(alertGroupStyles.modifiers.offstageTop), 0);
99 }, []);
100 const [isExpanded, setIsExpanded] = useState(false);
101 const onToggleExpand = () => {
102 setIsExpanded(!isExpanded);
103 };
104 const myOnMouseEnter = (ev) => {
105 setIsMouseOver(true);
106 setTimedOutAnimation(false);
107 onMouseEnter(ev);
108 };
109 const myOnMouseLeave = (ev) => {
110 setIsMouseOver(false);
111 onMouseLeave(ev);
112 };
113 if (shouldDismiss && isDismissed) {
114 return null;
115 }
116 const Title = (_jsx(TitleComponent, Object.assign({}, (isTooltipVisible && { tabIndex: 0 }), { ref: titleRef, className: css(styles.alertTitle, truncateTitle && styles.modifiers.truncate), children: getHeadingContent })));
117 return (_jsxs("div", Object.assign({ 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 && {
118 'aria-live': 'polite',
119 'aria-atomic': 'false'
120 }), { onMouseEnter: myOnMouseEnter, onMouseLeave: myOnMouseLeave, id: id }, props, { children: [isExpandable && (_jsx(AlertContext.Provider, { value: { title, variantLabel }, children: _jsx("div", { className: css(styles.alertToggle), children: _jsx(AlertToggleExpandButton, { isExpanded: isExpanded, onToggleExpand: onToggleExpand, "aria-label": toggleAriaLabel }) }) })), _jsx(AlertIcon, { variant: variant, customIcon: customIcon }), isTooltipVisible ? (_jsx(Tooltip, { content: getHeadingContent, position: tooltipPosition, children: Title })) : (Title), actionClose && (_jsx(AlertContext.Provider, { value: { title, variantLabel }, children: _jsx("div", { className: css(styles.alertAction), children: actionClose }) })), children && (!isExpandable || (isExpandable && isExpanded)) && (_jsx("div", { className: css(styles.alertDescription), children: children })), actionLinks && _jsx("div", { className: css(styles.alertActionGroup), children: actionLinks })] })));
121 };
122 Alert.displayName = 'Alert';
123 //# sourceMappingURL=Alert.js.map