An open source design system that's fully customizable and agent ready
TypeScript
fix(resizable): release window drag listeners when the handle unmounts mid-drag (#3768)
Shahrier Islam Arham060283f784c9
diff --git a/.changeset/resizehandle-drag-listener-leak.md b/.changeset/resizehandle-drag-listener-leak.mdnew file mode 100644index 0000000000..2584d88d8f--- /dev/null+++ b/.changeset/resizehandle-drag-listener-leak.md@@ -0,0 +1,8 @@+---+'@astryxdesign/core': patch+---++[fix] ResizeHandle: release window drag listeners when unmounted mid-drag++A drag in flight when the handle unmounts never receives its pointerup, so the window pointermove/pointerup/pointercancel listeners leaked — every subsequent pointer move kept resizing the still-mounted region, and the body cursor/user-select overrides stuck. Unmount now tears down the in-flight drag's listeners and restores the body styles.+@arham766diff --git a/packages/core/src/Resizable/ResizeHandle.test.tsx b/packages/core/src/Resizable/ResizeHandle.test.tsxindex 3eebdf1697..fc430156dc 100644--- a/packages/core/src/Resizable/ResizeHandle.test.tsx+++ b/packages/core/src/Resizable/ResizeHandle.test.tsx@@ -20,7 +20,7 @@ import {render, screen, fireEvent, act} from '@testing-library/react';import {ResizeHandle} from './ResizeHandle';import type {ResizeHandleProps} from './ResizeHandle';import {useResizable} from './useResizable';-import type {UseResizableSingleConfig} from './useResizable';+import type {ResizableProps, UseResizableSingleConfig} from './useResizable';const KEYBOARD_STEP = 10;const KEYBOARD_LARGE_STEP = 50;@@ -163,6 +163,44 @@ describe('ResizeHandle', () => {expect(separator).toHaveAttribute('aria-valuenow', '200');});+ // --- Drag listener lifecycle ---++ it('stops driving the region and releases window listeners when unmounted mid-drag', () => {+ const resizable: ResizableProps = {+ _size: 200,+ _isCollapsed: false,+ _onResizeStart: vi.fn(),+ _onResizeMove: vi.fn(),+ _onResizeEnd: vi.fn(),+ _minSizePx: 100,+ _maxSizePx: 400,+ _snaps: [],+ _collapsedSize: 40,+ _collapsible: false,+ _isResizableProps: true,+ };+ const {unmount} = render(+ <ResizeHandle resizable={resizable} label="Resize" />,+ );+ const separator = screen.getByRole('separator');+ const hitArea = separator.firstElementChild as HTMLElement;++ // Start a drag and confirm moves reach the region.+ fireEvent.pointerDown(hitArea, {clientX: 0, clientY: 0});+ expect(resizable._onResizeStart).toHaveBeenCalledTimes(1);+ fireEvent.pointerMove(window, {clientX: 10, clientY: 0});+ expect(resizable._onResizeMove).toHaveBeenCalledTimes(1);++ // Unmount mid-drag: the window listeners must be torn down, so further+ // pointer moves no longer resize the (still-mounted) region, and the+ // body cursor/user-select overrides are released.+ unmount();+ fireEvent.pointerMove(window, {clientX: 50, clientY: 0});+ expect(resizable._onResizeMove).toHaveBeenCalledTimes(1);+ expect(document.body.style.cursor).toBe('');+ expect(document.body.style.userSelect).toBe('');+ });+// --- Prop composition (ordering choice: handler sits after {...props}) ---it('runs a consumer onKeyDown alongside keyboard resizing', () => {diff --git a/packages/core/src/Resizable/ResizeHandle.tsx b/packages/core/src/Resizable/ResizeHandle.tsxindex 45626f07f4..ee10f87976 100644--- a/packages/core/src/Resizable/ResizeHandle.tsx+++ b/packages/core/src/Resizable/ResizeHandle.tsx@@ -316,6 +316,9 @@ export function ResizeHandle({...props}: ResizeHandleProps) {const handleRef = useRef<HTMLDivElement>(null);+ // Removes the in-flight drag's window listeners (and resets body styles).+ // Held in a ref so unmount can tear down a drag that never got a pointerup.+ const dragCleanupRef = useRef<(() => void) | null>(null);const [isDragging, setIsDragging] = useState(false);const [isHovered, setIsHovered] = useState(false);const [isFocused, setIsFocused] = useState(false);@@ -375,10 +378,12 @@ export function ResizeHandle({window.removeEventListener('pointermove', onMove);window.removeEventListener('pointerup', onUp);window.removeEventListener('pointercancel', onCancel);+ dragCleanupRef.current = null;}window.addEventListener('pointermove', onMove);window.addEventListener('pointerup', onUp);window.addEventListener('pointercancel', onCancel);+ dragCleanupRef.current = cleanup;},[isDisabled, resizable, isHorizontal, getRTLMultiplier, sign],);@@ -454,14 +459,19 @@ export function ResizeHandle({}, [isDisabled, resizable]);// --- Cleanup on unmount ---+ // A drag in flight when the handle unmounts never gets its pointerup, so+ // tear down the window listeners here too — otherwise every pointermove+ // keeps driving the (still-mounted) region's resize state after the handle+ // is gone, and the body cursor/user-select overrides stick.useEffect(() => {return () => {- if (isDragging) {+ if (dragCleanupRef.current) {+ dragCleanupRef.current();document.body.style.cursor = '';document.body.style.userSelect = '';}};- }, [isDragging]);+ }, []);// --- ARIA ---const ariaValueNow = resizable ? resizable._size : undefined;