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