Skip to content

Commit 0cd3a4d

Browse files
authored
fix(Drawer): account for cursor position in resize (#12506)
1 parent 65c1213 commit 0cd3a4d

1 file changed

Lines changed: 28 additions & 29 deletions

File tree

packages/react-core/src/components/Drawer/DrawerPanelContent.tsx

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -105,12 +105,11 @@ export const DrawerPanelContent: React.FunctionComponent<DrawerPanelContentProps
105105
const [isFocusTrapActive, setIsFocusTrapActive] = useState(false);
106106
const [shouldCollapseSpace, setShouldCollapseSpace] = useState(hidden);
107107
const previouslyFocusedElement = useRef(null);
108-
let currWidth: number = 0;
109-
let panelRect: DOMRect;
110-
let end: number;
111-
let start: number;
112-
let bottom: number;
113-
let setInitialVals: boolean = true;
108+
const resizeState = useRef({
109+
initialPointerPos: 0,
110+
initialPanelSize: 0,
111+
currWidth: 0
112+
});
114113

115114
if (isStatic && focusTrap?.enabled) {
116115
// eslint-disable-next-line no-console
@@ -186,8 +185,20 @@ export const DrawerPanelContent: React.FunctionComponent<DrawerPanelContentProps
186185
return Math.round((newSplitterPos + Number.EPSILON) * 100) / 100;
187186
};
188187

188+
const setInitialResizeState = (controlPosition: number) => {
189+
const panelRect = panel.current.getBoundingClientRect();
190+
const initialPanelSize = position === 'bottom' ? panelRect.height : panelRect.width;
191+
192+
resizeState.current = {
193+
initialPointerPos: controlPosition,
194+
initialPanelSize,
195+
currWidth: initialPanelSize
196+
};
197+
};
198+
189199
const handleTouchStart = (e: React.TouchEvent) => {
190200
e.stopPropagation();
201+
setInitialResizeState(position === 'bottom' ? e.touches[0].clientY : e.touches[0].clientX);
191202
document.addEventListener('touchmove', callbackTouchMove, { passive: false });
192203
document.addEventListener('touchend', callbackTouchEnd);
193204
isResizing = true;
@@ -196,11 +207,11 @@ export const DrawerPanelContent: React.FunctionComponent<DrawerPanelContentProps
196207
const handleMousedown = (e: React.MouseEvent) => {
197208
e.stopPropagation();
198209
e.preventDefault();
210+
setInitialResizeState(position === 'bottom' ? e.clientY : e.clientX);
199211
document.addEventListener('mousemove', callbackMouseMove);
200212
document.addEventListener('mouseup', callbackMouseUp);
201213
drawerRef.current.classList.add(css(styles.modifiers.resizing));
202214
isResizing = true;
203-
setInitialVals = true;
204215
};
205216

206217
const handleMouseMove = (e: MouseEvent) => {
@@ -223,33 +234,22 @@ export const DrawerPanelContent: React.FunctionComponent<DrawerPanelContentProps
223234
return;
224235
}
225236

226-
if (setInitialVals) {
227-
panelRect = panel.current.getBoundingClientRect();
228-
if (isRTL) {
229-
start = panelRect.right;
230-
end = panelRect.left;
231-
} else {
232-
end = panelRect.right;
233-
start = panelRect.left;
234-
}
235-
bottom = panelRect.bottom;
236-
setInitialVals = false;
237-
}
238-
const mousePos = controlPosition;
237+
const { initialPointerPos, initialPanelSize } = resizeState.current;
238+
const delta = controlPosition - initialPointerPos;
239239
let newSize = 0;
240240
if (position === 'end' || position === 'right') {
241-
newSize = isRTL ? mousePos - end : end - mousePos;
241+
newSize = isRTL ? initialPanelSize + delta : initialPanelSize - delta;
242242
} else if (position === 'start' || position === 'left') {
243-
newSize = isRTL ? start - mousePos : mousePos - start;
243+
newSize = isRTL ? initialPanelSize - delta : initialPanelSize + delta;
244244
} else {
245-
newSize = bottom - mousePos;
245+
newSize = initialPanelSize - delta;
246246
}
247247

248248
if (position === 'bottom') {
249249
panel.current.style.overflowAnchor = 'none';
250250
}
251251
panel.current.style.setProperty(cssPanelMdFlexBasis.name, newSize + 'px');
252-
currWidth = newSize;
252+
resizeState.current.currWidth = newSize;
253253
setSeparatorValue(calcValueNow());
254254
};
255255

@@ -259,8 +259,7 @@ export const DrawerPanelContent: React.FunctionComponent<DrawerPanelContentProps
259259
}
260260
drawerRef.current.classList.remove(css(styles.modifiers.resizing));
261261
isResizing = false;
262-
onResize && onResize(e, currWidth, id);
263-
setInitialVals = true;
262+
onResize && onResize(e, resizeState.current.currWidth, id);
264263
document.removeEventListener('mousemove', callbackMouseMove);
265264
document.removeEventListener('mouseup', callbackMouseUp);
266265
};
@@ -271,7 +270,7 @@ export const DrawerPanelContent: React.FunctionComponent<DrawerPanelContentProps
271270
return;
272271
}
273272
isResizing = false;
274-
onResize && onResize(e, currWidth, id);
273+
onResize && onResize(e, resizeState.current.currWidth, id);
275274
document.removeEventListener('touchmove', callbackTouchMove);
276275
document.removeEventListener('touchend', callbackTouchEnd);
277276
};
@@ -301,7 +300,7 @@ export const DrawerPanelContent: React.FunctionComponent<DrawerPanelContentProps
301300
e.preventDefault();
302301

303302
if (key === 'Escape' || key === 'Enter') {
304-
onResize && onResize(e, currWidth, id);
303+
onResize && onResize(e, resizeState.current.currWidth, id);
305304
}
306305
const panelRect = panel.current.getBoundingClientRect();
307306
newSize = position === 'bottom' ? panelRect.height : panelRect.width;
@@ -328,7 +327,7 @@ export const DrawerPanelContent: React.FunctionComponent<DrawerPanelContentProps
328327
panel.current.style.overflowAnchor = 'none';
329328
}
330329
panel.current.style.setProperty(cssPanelMdFlexBasis.name, newSize + 'px');
331-
currWidth = newSize;
330+
resizeState.current.currWidth = newSize;
332331
setSeparatorValue(calcValueNow());
333332
};
334333
const boundaryCssVars: any = {};

0 commit comments

Comments
 (0)