Debian Codegraph @

node/@patternfly/react-core/dist/js/components/Popover/Popover.js

raw
1 "use strict";
2 Object.defineProperty(exports, "__esModule", { value: true });
3 exports.Popover = exports.PopoverPosition = 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 constants_1 = require("../../helpers/constants");
8 const popover_1 = tslib_1.__importDefault(require("@patternfly/react-styles/css/components/Popover/popover"));
9 const react_styles_1 = require("@patternfly/react-styles");
10 const PopoverContext_1 = require("./PopoverContext");
11 const PopoverContent_1 = require("./PopoverContent");
12 const PopoverBody_1 = require("./PopoverBody");
13 const PopoverHeader_1 = require("./PopoverHeader");
14 const PopoverFooter_1 = require("./PopoverFooter");
15 const PopoverCloseButton_1 = require("./PopoverCloseButton");
16 const PopoverArrow_1 = require("./PopoverArrow");
17 const c_popover_MaxWidth_1 = tslib_1.__importDefault(require('@patternfly/react-tokens/dist/js/c_popover_MaxWidth'));
18 const c_popover_MinWidth_1 = tslib_1.__importDefault(require('@patternfly/react-tokens/dist/js/c_popover_MinWidth'));
19 const helpers_1 = require("../../helpers");
20 const Popper_1 = require("../../helpers/Popper/Popper");
21 const helpers_2 = require("../../helpers");
22 var PopoverPosition;
23 (function (PopoverPosition) {
24 PopoverPosition["auto"] = "auto";
25 PopoverPosition["top"] = "top";
26 PopoverPosition["bottom"] = "bottom";
27 PopoverPosition["left"] = "left";
28 PopoverPosition["right"] = "right";
29 PopoverPosition["topStart"] = "top-start";
30 PopoverPosition["topEnd"] = "top-end";
31 PopoverPosition["bottomStart"] = "bottom-start";
32 PopoverPosition["bottomEnd"] = "bottom-end";
33 PopoverPosition["leftStart"] = "left-start";
34 PopoverPosition["leftEnd"] = "left-end";
35 PopoverPosition["rightStart"] = "right-start";
36 PopoverPosition["rightEnd"] = "right-end";
37 })(PopoverPosition || (exports.PopoverPosition = PopoverPosition = {}));
38 const alertStyle = {
39 custom: popover_1.default.modifiers.custom,
40 info: popover_1.default.modifiers.info,
41 success: popover_1.default.modifiers.success,
42 warning: popover_1.default.modifiers.warning,
43 danger: popover_1.default.modifiers.danger
44 };
45 const Popover = (_a) => {
46 var { children, position = 'top', enableFlip = true, className = '', isVisible = null, shouldClose = () => null, shouldOpen = () => null, 'aria-label': ariaLabel = '', bodyContent, headerContent = null, headerComponent = 'h6', headerIcon = null, alertSeverityVariant, alertSeverityScreenReaderText, footerContent = null, appendTo = () => document.body, hideOnOutsideClick = true, onHide = () => null, onHidden = () => null, onShow = () => null, onShown = () => null, onMount = () => null, zIndex = 9999, triggerAction = 'click', minWidth = c_popover_MinWidth_1.default && c_popover_MinWidth_1.default.value, maxWidth = c_popover_MaxWidth_1.default && c_popover_MaxWidth_1.default.value, closeBtnAriaLabel = 'Close', showClose = true, distance = 25, flipBehavior = [
47 'top',
48 'bottom',
49 'left',
50 'right',
51 'top-start',
52 'top-end',
53 'bottom-start',
54 'bottom-end',
55 'left-start',
56 'left-end',
57 'right-start',
58 'right-end'
59 ], animationDuration = 300, id, withFocusTrap: propWithFocusTrap = triggerAction === 'hover' ? false : undefined, triggerRef, hasNoPadding = false, hasAutoWidth = false, elementToFocus } = _a, rest = tslib_1.__rest(_a, ["children", "position", "enableFlip", "className", "isVisible", "shouldClose", "shouldOpen", 'aria-label', "bodyContent", "headerContent", "headerComponent", "headerIcon", "alertSeverityVariant", "alertSeverityScreenReaderText", "footerContent", "appendTo", "hideOnOutsideClick", "onHide", "onHidden", "onShow", "onShown", "onMount", "zIndex", "triggerAction", "minWidth", "maxWidth", "closeBtnAriaLabel", "showClose", "distance", "flipBehavior", "animationDuration", "id", "withFocusTrap", "triggerRef", "hasNoPadding", "hasAutoWidth", "elementToFocus"]);
60 // could make this a prop in the future (true | false | 'toggle')
61 // const hideOnClick = true;
62 const generatedId = (0, helpers_2.useSSRSafeId)();
63 const uniqueId = id || generatedId;
64 const triggerManually = isVisible !== null;
65 const [visible, setVisible] = (0, react_1.useState)(false);
66 const [focusTrapActive, setFocusTrapActive] = (0, react_1.useState)(Boolean(propWithFocusTrap));
67 const popoverRef = (0, react_1.useRef)(null);
68 (0, react_1.useEffect)(() => {
69 onMount();
70 }, []);
71 (0, react_1.useEffect)(() => {
72 if (triggerManually) {
73 if (isVisible) {
74 show(undefined, true);
75 }
76 else {
77 hide();
78 }
79 }
80 }, [isVisible, triggerManually]);
81 const show = (event, withFocusTrap) => {
82 event && onShow(event);
83 setVisible(true);
84 propWithFocusTrap !== false && withFocusTrap && setFocusTrapActive(true);
85 };
86 const hide = (event) => {
87 event && onHide(event);
88 setVisible(false);
89 };
90 const positionModifiers = {
91 top: popover_1.default.modifiers.top,
92 bottom: popover_1.default.modifiers.bottom,
93 left: popover_1.default.modifiers.left,
94 right: popover_1.default.modifiers.right,
95 'top-start': popover_1.default.modifiers.topLeft,
96 'top-end': popover_1.default.modifiers.topRight,
97 'bottom-start': popover_1.default.modifiers.bottomLeft,
98 'bottom-end': popover_1.default.modifiers.bottomRight,
99 'left-start': popover_1.default.modifiers.leftTop,
100 'left-end': popover_1.default.modifiers.leftBottom,
101 'right-start': popover_1.default.modifiers.rightTop,
102 'right-end': popover_1.default.modifiers.rightBottom
103 };
104 const hasCustomMinWidth = minWidth !== c_popover_MinWidth_1.default.value;
105 const hasCustomMaxWidth = maxWidth !== c_popover_MaxWidth_1.default.value;
106 const onDocumentKeyDown = (event) => {
107 if (event.key === constants_1.KeyTypes.Escape && visible) {
108 if (triggerManually) {
109 shouldClose(event, hide);
110 }
111 else {
112 hide(event);
113 }
114 }
115 };
116 const onDocumentClick = (event, triggerElement, popperElement) => {
117 if (hideOnOutsideClick && visible) {
118 const isFromChild = popperElement && popperElement.contains(event.target);
119 const isFromTrigger = triggerElement && triggerElement.contains(event.target);
120 if (isFromChild || isFromTrigger) {
121 // if clicked within the popper or on the trigger, ignore this event
122 return;
123 }
124 if (triggerManually) {
125 shouldClose(event, hide);
126 }
127 else {
128 hide(event);
129 }
130 }
131 };
132 const onTriggerClick = (event) => {
133 if (triggerManually) {
134 if (visible) {
135 shouldClose(event, hide);
136 }
137 else {
138 shouldOpen(event, show);
139 }
140 }
141 else {
142 if (visible) {
143 hide(event);
144 }
145 else {
146 show(event, true);
147 }
148 }
149 };
150 const onContentMouseDown = () => {
151 if (focusTrapActive) {
152 setFocusTrapActive(false);
153 }
154 };
155 const onMouseEnter = (event) => {
156 if (triggerManually) {
157 shouldOpen(event, show);
158 }
159 else {
160 show(event, false);
161 }
162 };
163 const onMouseLeave = (event) => {
164 if (triggerManually) {
165 shouldClose(event, hide);
166 }
167 else {
168 hide(event);
169 }
170 };
171 const onFocus = (event) => {
172 if (triggerManually) {
173 shouldOpen(event, show);
174 }
175 else {
176 show(event, false);
177 }
178 };
179 const onBlur = (event) => {
180 if (triggerManually) {
181 shouldClose(event, hide);
182 }
183 else {
184 hide(event);
185 }
186 };
187 const closePopover = (event) => {
188 event.stopPropagation();
189 if (triggerManually) {
190 shouldClose(event, hide);
191 }
192 else {
193 hide(event);
194 }
195 };
196 const content = ((0, jsx_runtime_1.jsxs)(helpers_1.FocusTrap, Object.assign({ ref: popoverRef, active: focusTrapActive, focusTrapOptions: {
197 returnFocusOnDeactivate: propWithFocusTrap !== false,
198 clickOutsideDeactivates: true,
199 // FocusTrap's initialFocus can accept false as a value to prevent initial focus.
200 // We want to prevent this in case false is ever passed in.
201 initialFocus: elementToFocus || undefined,
202 checkCanFocusTrap: (containers) => new Promise((resolve) => {
203 const interval = setInterval(() => {
204 if (containers.every((container) => getComputedStyle(container).visibility !== 'hidden')) {
205 clearInterval(interval);
206 resolve();
207 }
208 }, 10);
209 // Clear interval if promise is never resolved (component unmount)
210 const timeout = setTimeout(() => {
211 clearInterval(interval);
212 resolve(); // Resolve to prevent hanging promise
213 }, 5000); // 5 second timeout as safety net
214 // Store cleanup function for potential external cleanup
215 resolve._cleanup = () => {
216 clearInterval(interval);
217 clearTimeout(timeout);
218 };
219 }),
220 tabbableOptions: { displayCheck: 'none' },
221 fallbackFocus: () => {
222 // If the popover's trigger is focused but scrolled out of view,
223 // FocusTrap will throw an error when the Enter button is used on the trigger.
224 // That is because the Popover is hidden when its trigger is out of view.
225 // Provide a fallback in that case.
226 let node = null;
227 if (document && document.activeElement) {
228 node = document.activeElement;
229 }
230 return node;
231 }
232 }, preventScrollOnDeactivate: true, className: (0, react_styles_1.css)(popover_1.default.popover, alertSeverityVariant && alertStyle[alertSeverityVariant], hasNoPadding && popover_1.default.modifiers.noPadding, hasAutoWidth && popover_1.default.modifiers.widthAuto, className), role: "dialog", "aria-modal": "true", "aria-label": headerContent ? undefined : ariaLabel, "aria-labelledby": headerContent ? `popover-${uniqueId}-header` : undefined, "aria-describedby": `popover-${uniqueId}-body`, onMouseDown: onContentMouseDown, style: {
233 minWidth: hasCustomMinWidth ? minWidth : null,
234 maxWidth: hasCustomMaxWidth ? maxWidth : null
235 } }, rest, { children: [(0, jsx_runtime_1.jsx)(PopoverArrow_1.PopoverArrow, {}), (0, jsx_runtime_1.jsxs)(PopoverContent_1.PopoverContent, { children: [showClose && triggerAction === 'click' && ((0, jsx_runtime_1.jsx)(PopoverCloseButton_1.PopoverCloseButton, { onClose: closePopover, "aria-label": closeBtnAriaLabel })), headerContent && ((0, jsx_runtime_1.jsx)(PopoverHeader_1.PopoverHeader, { id: `popover-${uniqueId}-header`, icon: headerIcon, alertSeverityVariant: alertSeverityVariant, alertSeverityScreenReaderText: alertSeverityScreenReaderText || `${alertSeverityVariant} alert:`, titleHeadingLevel: headerComponent, children: typeof headerContent === 'function' ? headerContent(hide) : headerContent })), (0, jsx_runtime_1.jsx)(PopoverBody_1.PopoverBody, { id: `popover-${uniqueId}-body`, children: typeof bodyContent === 'function' ? bodyContent(hide) : bodyContent }), footerContent && ((0, jsx_runtime_1.jsx)(PopoverFooter_1.PopoverFooter, { id: `popover-${uniqueId}-footer`, children: typeof footerContent === 'function' ? footerContent(hide) : footerContent }))] })] })));
236 return ((0, jsx_runtime_1.jsx)(PopoverContext_1.PopoverContext.Provider, { value: { headerComponent }, children: (0, jsx_runtime_1.jsx)(Popper_1.Popper, { trigger: children, triggerRef: triggerRef, popper: content, popperRef: popoverRef, minWidth: minWidth, appendTo: appendTo, isVisible: visible, onMouseEnter: triggerAction === 'hover' && onMouseEnter, onMouseLeave: triggerAction === 'hover' && onMouseLeave, onPopperMouseEnter: triggerAction === 'hover' && onMouseEnter, onPopperMouseLeave: triggerAction === 'hover' && onMouseLeave, onFocus: triggerAction === 'hover' && onFocus, onBlur: triggerAction === 'hover' && onBlur, positionModifiers: positionModifiers, distance: distance, placement: position, onTriggerClick: triggerAction === 'click' && onTriggerClick, onDocumentClick: onDocumentClick, onDocumentKeyDown: onDocumentKeyDown, enableFlip: enableFlip, zIndex: zIndex, flipBehavior: flipBehavior, animationDuration: animationDuration, onHidden: onHidden, onShown: onShown, onHide: () => setFocusTrapActive(false) }) }));
237 };
238 exports.Popover = Popover;
239 exports.Popover.displayName = 'Popover';
240 //# sourceMappingURL=Popover.js.map