Debian Codegraph @

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

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