Debian Codegraph @

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

raw
1 import { __rest } from "tslib";
2 import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
3 import { Fragment, useCallback, useContext, useEffect, useRef, useState } from 'react';
4 import styles from '@patternfly/react-styles/css/components/Drawer/drawer.mjs';
5 import { css } from '@patternfly/react-styles';
6 import { DrawerColorVariant, DrawerContext } from './Drawer';
7 import { formatBreakpointMods, getLanguageDirection } from '../../helpers/util';
8 import { useSSRSafeId } from '../../helpers';
9 import { FocusTrap } from '../../helpers/FocusTrap/FocusTrap';
10 import cssPanelMdFlexBasis from '@patternfly/react-tokens/dist/esm/c_drawer__panel_md_FlexBasis';
11 import cssPanelMdFlexBasisMin from '@patternfly/react-tokens/dist/esm/c_drawer__panel_md_FlexBasis_min';
12 import cssPanelMdFlexBasisMax from '@patternfly/react-tokens/dist/esm/c_drawer__panel_md_FlexBasis_max';
13 let isResizing = null;
14 let newSize = 0;
15 export const DrawerPanelContent = (_a) => {
16 var { className, id, children, hasNoBorder = false, isResizable = false, hasNoGlass = false, isGlass = false, isPlain = false, isNoPlainOnGlass = false, onResize, minSize, defaultSize, maxSize, increment = 5, resizeAriaLabel = 'Resize', widths, colorVariant = DrawerColorVariant.default, focusTrap, style } = _a, props = __rest(_a, ["className", "id", "children", "hasNoBorder", "isResizable", "hasNoGlass", "isGlass", "isPlain", "isNoPlainOnGlass", "onResize", "minSize", "defaultSize", "maxSize", "increment", "resizeAriaLabel", "widths", "colorVariant", "focusTrap", "style"]);
17 const panelId = useSSRSafeId('pf-drawer-panel-');
18 const panel = useRef(undefined);
19 const splitterRef = useRef(undefined);
20 const [separatorValue, setSeparatorValue] = useState(0);
21 const { position, isExpanded, isStatic, onExpand, drawerRef, drawerContentRef, isInline } = useContext(DrawerContext);
22 const hidden = isStatic ? false : !isExpanded;
23 const [isExpandedInternal, setIsExpandedInternal] = useState(!hidden);
24 const [isFocusTrapActive, setIsFocusTrapActive] = useState(false);
25 const [shouldCollapseSpace, setShouldCollapseSpace] = useState(hidden);
26 const previouslyFocusedElement = useRef(null);
27 const resizeState = useRef({
28 initialPointerPos: 0,
29 initialPanelSize: 0,
30 currWidth: 0
31 });
32 if (isStatic && (focusTrap === null || focusTrap === void 0 ? void 0 : focusTrap.enabled)) {
33 // eslint-disable-next-line no-console
34 console.warn(`DrawerPanelContent: The focusTrap.enabled prop cannot be true if the Drawer's isStatic prop is true. This will cause a permanent focus trap.`);
35 }
36 useEffect(() => {
37 if (!isStatic && isExpanded) {
38 setIsExpandedInternal(isExpanded);
39 setShouldCollapseSpace(false);
40 }
41 }, [isStatic, isExpanded]);
42 const calcValueNow = () => {
43 let splitterPos;
44 let drawerSize;
45 const isRTL = getLanguageDirection(panel.current) === 'rtl';
46 if (isInline && (position === 'end' || position === 'right')) {
47 if (isRTL) {
48 splitterPos = panel.current.getBoundingClientRect().left - splitterRef.current.getBoundingClientRect().right;
49 drawerSize = drawerRef.current.getBoundingClientRect().left - drawerRef.current.getBoundingClientRect().right;
50 }
51 else {
52 splitterPos = panel.current.getBoundingClientRect().right - splitterRef.current.getBoundingClientRect().left;
53 drawerSize = drawerRef.current.getBoundingClientRect().right - drawerRef.current.getBoundingClientRect().left;
54 }
55 }
56 else if (isInline && (position === 'start' || position === 'left')) {
57 if (isRTL) {
58 splitterPos = splitterRef.current.getBoundingClientRect().left - panel.current.getBoundingClientRect().right;
59 drawerSize = drawerRef.current.getBoundingClientRect().left - drawerRef.current.getBoundingClientRect().right;
60 }
61 else {
62 splitterPos = splitterRef.current.getBoundingClientRect().right - panel.current.getBoundingClientRect().left;
63 drawerSize = drawerRef.current.getBoundingClientRect().right - drawerRef.current.getBoundingClientRect().left;
64 }
65 }
66 else if (position === 'end' || position === 'right') {
67 if (isRTL) {
68 splitterPos =
69 drawerContentRef.current.getBoundingClientRect().left - splitterRef.current.getBoundingClientRect().right;
70 drawerSize =
71 drawerContentRef.current.getBoundingClientRect().left -
72 drawerContentRef.current.getBoundingClientRect().right;
73 }
74 else {
75 splitterPos =
76 drawerContentRef.current.getBoundingClientRect().right - splitterRef.current.getBoundingClientRect().left;
77 drawerSize =
78 drawerContentRef.current.getBoundingClientRect().right -
79 drawerContentRef.current.getBoundingClientRect().left;
80 }
81 }
82 else if (position === 'start' || position === 'left') {
83 if (isRTL) {
84 splitterPos =
85 splitterRef.current.getBoundingClientRect().left - drawerContentRef.current.getBoundingClientRect().right;
86 drawerSize =
87 drawerContentRef.current.getBoundingClientRect().left -
88 drawerContentRef.current.getBoundingClientRect().right;
89 }
90 else {
91 splitterPos =
92 splitterRef.current.getBoundingClientRect().right - drawerContentRef.current.getBoundingClientRect().left;
93 drawerSize =
94 drawerContentRef.current.getBoundingClientRect().right -
95 drawerContentRef.current.getBoundingClientRect().left;
96 }
97 }
98 else if (position === 'bottom') {
99 splitterPos =
100 drawerContentRef.current.getBoundingClientRect().bottom - splitterRef.current.getBoundingClientRect().top;
101 drawerSize =
102 drawerContentRef.current.getBoundingClientRect().bottom - drawerContentRef.current.getBoundingClientRect().top;
103 }
104 const newSplitterPos = (splitterPos / drawerSize) * 100;
105 return Math.round((newSplitterPos + Number.EPSILON) * 100) / 100;
106 };
107 const setInitialResizeState = (controlPosition) => {
108 const panelRect = panel.current.getBoundingClientRect();
109 const initialPanelSize = position === 'bottom' ? panelRect.height : panelRect.width;
110 resizeState.current = {
111 initialPointerPos: controlPosition,
112 initialPanelSize,
113 currWidth: initialPanelSize
114 };
115 };
116 const handleTouchStart = (e) => {
117 e.stopPropagation();
118 setInitialResizeState(position === 'bottom' ? e.touches[0].clientY : e.touches[0].clientX);
119 document.addEventListener('touchmove', callbackTouchMove, { passive: false });
120 document.addEventListener('touchend', callbackTouchEnd);
121 isResizing = true;
122 };
123 const handleMousedown = (e) => {
124 e.stopPropagation();
125 e.preventDefault();
126 setInitialResizeState(position === 'bottom' ? e.clientY : e.clientX);
127 document.addEventListener('mousemove', callbackMouseMove);
128 document.addEventListener('mouseup', callbackMouseUp);
129 drawerRef.current.classList.add(css(styles.modifiers.resizing));
130 isResizing = true;
131 };
132 const handleMouseMove = (e) => {
133 const mousePos = position === 'bottom' ? e.clientY : e.clientX;
134 handleControlMove(e, mousePos);
135 };
136 const handleTouchMove = (e) => {
137 e.preventDefault();
138 e.stopImmediatePropagation();
139 const touchPos = position === 'bottom' ? e.touches[0].clientY : e.touches[0].clientX;
140 handleControlMove(e, touchPos);
141 };
142 const handleControlMove = (e, controlPosition) => {
143 const isRTL = getLanguageDirection(panel.current) === 'rtl';
144 e.stopPropagation();
145 if (!isResizing) {
146 return;
147 }
148 const { initialPointerPos, initialPanelSize } = resizeState.current;
149 const delta = controlPosition - initialPointerPos;
150 let newSize = 0;
151 if (position === 'end' || position === 'right') {
152 newSize = isRTL ? initialPanelSize + delta : initialPanelSize - delta;
153 }
154 else if (position === 'start' || position === 'left') {
155 newSize = isRTL ? initialPanelSize - delta : initialPanelSize + delta;
156 }
157 else {
158 newSize = initialPanelSize - delta;
159 }
160 if (position === 'bottom') {
161 panel.current.style.overflowAnchor = 'none';
162 }
163 panel.current.style.setProperty(cssPanelMdFlexBasis.name, newSize + 'px');
164 resizeState.current.currWidth = newSize;
165 setSeparatorValue(calcValueNow());
166 };
167 const handleMouseup = (e) => {
168 if (!isResizing) {
169 return;
170 }
171 drawerRef.current.classList.remove(css(styles.modifiers.resizing));
172 isResizing = false;
173 onResize && onResize(e, resizeState.current.currWidth, id);
174 document.removeEventListener('mousemove', callbackMouseMove);
175 document.removeEventListener('mouseup', callbackMouseUp);
176 };
177 const handleTouchEnd = (e) => {
178 e.stopPropagation();
179 if (!isResizing) {
180 return;
181 }
182 isResizing = false;
183 onResize && onResize(e, resizeState.current.currWidth, id);
184 document.removeEventListener('touchmove', callbackTouchMove);
185 document.removeEventListener('touchend', callbackTouchEnd);
186 };
187 const callbackMouseMove = useCallback(handleMouseMove, []);
188 const callbackTouchEnd = useCallback(handleTouchEnd, []);
189 const callbackTouchMove = useCallback(handleTouchMove, []);
190 const callbackMouseUp = useCallback(handleMouseup, []);
191 const handleKeys = (e) => {
192 const isRTL = getLanguageDirection(panel.current) === 'rtl';
193 const key = e.key;
194 if (key !== 'Escape' &&
195 key !== 'Enter' &&
196 key !== 'ArrowUp' &&
197 key !== 'ArrowDown' &&
198 key !== 'ArrowLeft' &&
199 key !== 'ArrowRight') {
200 if (isResizing) {
201 e.preventDefault();
202 }
203 return;
204 }
205 e.preventDefault();
206 if (key === 'Escape' || key === 'Enter') {
207 onResize && onResize(e, resizeState.current.currWidth, id);
208 }
209 const panelRect = panel.current.getBoundingClientRect();
210 newSize = position === 'bottom' ? panelRect.height : panelRect.width;
211 let delta = 0;
212 if (key === 'ArrowRight') {
213 if (isRTL) {
214 delta = position === 'left' || position === 'start' ? -increment : increment;
215 }
216 else {
217 delta = position === 'left' || position === 'start' ? increment : -increment;
218 }
219 }
220 else if (key === 'ArrowLeft') {
221 if (isRTL) {
222 delta = position === 'left' || position === 'start' ? increment : -increment;
223 }
224 else {
225 delta = position === 'left' || position === 'start' ? -increment : increment;
226 }
227 }
228 else if (key === 'ArrowUp') {
229 delta = increment;
230 }
231 else if (key === 'ArrowDown') {
232 delta = -increment;
233 }
234 newSize = newSize + delta;
235 if (position === 'bottom') {
236 panel.current.style.overflowAnchor = 'none';
237 }
238 panel.current.style.setProperty(cssPanelMdFlexBasis.name, newSize + 'px');
239 resizeState.current.currWidth = newSize;
240 setSeparatorValue(calcValueNow());
241 };
242 const boundaryCssVars = {};
243 if (defaultSize) {
244 boundaryCssVars[cssPanelMdFlexBasis.name] = defaultSize;
245 }
246 if (minSize) {
247 boundaryCssVars[cssPanelMdFlexBasisMin.name] = minSize;
248 }
249 if (maxSize) {
250 boundaryCssVars[cssPanelMdFlexBasisMax.name] = maxSize;
251 }
252 const isValidFocusTrap = (focusTrap === null || focusTrap === void 0 ? void 0 : focusTrap.enabled) && !isStatic;
253 const Component = isValidFocusTrap ? FocusTrap : 'div';
254 const focusTrapProps = {
255 tabIndex: -1,
256 'aria-modal': true,
257 role: 'dialog',
258 active: isFocusTrapActive,
259 'aria-labelledby': (focusTrap === null || focusTrap === void 0 ? void 0 : focusTrap['aria-labelledby']) || id || panelId,
260 focusTrapOptions: {
261 fallbackFocus: () => panel.current,
262 onActivate: () => {
263 if (previouslyFocusedElement.current !== document.activeElement) {
264 previouslyFocusedElement.current = document.activeElement;
265 }
266 },
267 onDeactivate: () => {
268 previouslyFocusedElement.current &&
269 previouslyFocusedElement.current.focus &&
270 previouslyFocusedElement.current.focus();
271 },
272 clickOutsideDeactivates: true,
273 returnFocusOnDeactivate: false,
274 // FocusTrap's initialFocus can accept false as a value to prevent initial focus.
275 // We want to prevent this in case false is ever passed in.
276 initialFocus: (focusTrap === null || focusTrap === void 0 ? void 0 : focusTrap.elementToFocusOnExpand) || undefined,
277 escapeDeactivates: false
278 }
279 };
280 return (_jsx(Component, Object.assign({}, (isValidFocusTrap && focusTrapProps), { id: id || panelId, className: css(styles.drawerPanel, isResizable && styles.modifiers.resizable, hasNoGlass && 'pf-m-no-glass', isGlass && styles.modifiers.glass, isPlain && styles.modifiers.plain, isNoPlainOnGlass && styles.modifiers.noPlainOnGlass, hasNoBorder && styles.modifiers.noBorder, formatBreakpointMods(widths, styles), colorVariant === DrawerColorVariant.noBackground && styles.modifiers.noBackground, colorVariant === DrawerColorVariant.secondary && styles.modifiers.secondary, className), onTransitionEnd: (ev) => {
281 if (ev.target === panel.current) {
282 if (!hidden && ev.nativeEvent.propertyName === 'transform') {
283 onExpand(ev);
284 }
285 setIsExpandedInternal(!hidden);
286 // We also need to collapse the space when the panel is hidden to prevent automation from scrolling to it
287 if (hidden && ev.nativeEvent.propertyName === 'transform') {
288 setShouldCollapseSpace(true);
289 }
290 if (isValidFocusTrap && ev.nativeEvent.propertyName === 'transform') {
291 setIsFocusTrapActive((prevIsFocusTrapActive) => !prevIsFocusTrapActive);
292 }
293 }
294 }, hidden: hidden, style: Object.assign(Object.assign({}, ((defaultSize || minSize || maxSize) && boundaryCssVars)), style) }, (shouldCollapseSpace && { inert: '' }), props, { ref: panel, children: isExpandedInternal && (_jsxs(Fragment, { children: [isResizable && (_jsxs(Fragment, { children: [_jsx("div", { className: css(styles.drawerSplitter, position !== 'bottom' && styles.modifiers.vertical), role: "separator", tabIndex: 0, "aria-orientation": position === 'bottom' ? 'horizontal' : 'vertical', "aria-label": resizeAriaLabel, "aria-valuenow": separatorValue, "aria-valuemin": 0, "aria-valuemax": 100, "aria-controls": id || panelId, onMouseDown: handleMousedown, onKeyDown: handleKeys, onTouchStart: handleTouchStart, ref: splitterRef, children: _jsx("div", { className: css(styles.drawerSplitterHandle), "aria-hidden": true }) }), _jsx("div", { className: css(styles.drawerPanelMain), children: children })] })), !isResizable && children] })) })));
295 };
296 DrawerPanelContent.displayName = 'DrawerPanelContent';
297 //# sourceMappingURL=DrawerPanelContent.js.map