Debian Codegraph @

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

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