Skip to content

Commit

Permalink
Cleanup drag state when a resize handle unmounts while dragging
Browse files Browse the repository at this point in the history
  • Loading branch information
bvaughn committed Sep 14, 2024
1 parent 7988684 commit a5c602c
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 2 deletions.
1 change: 1 addition & 0 deletions packages/react-resizable-panels/src/PanelGroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -816,6 +816,7 @@ function PanelGroupWithForwardedRef({
[resizePanel]
);

// TODO Multiple drag handles can be active at the same time so this API is a bit awkward now
const startDragging = useCallback(
(dragHandleId: string, event: ResizeEvent) => {
const { direction } = committedValuesRef.current;
Expand Down
6 changes: 5 additions & 1 deletion packages/react-resizable-panels/src/PanelResizeHandle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,13 +129,15 @@ export function PanelResizeHandle({
const setResizeHandlerState = (
action: ResizeHandlerAction,
isActive: boolean,
event: ResizeEvent
event: ResizeEvent | null
) => {
if (isActive) {
switch (action) {
case "down": {
setState("drag");

assert(event, 'Expected event to be defined for "down" action');

startDragging(resizeHandleId, event);

const { onDragging } = callbacksRef.current;
Expand All @@ -151,6 +153,8 @@ export function PanelResizeHandle({
setState("hover");
}

assert(event, 'Expected event to be defined for "move" action');

resizeHandler(event);
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export type ResizeHandlerAction = "down" | "move" | "up";
export type SetResizeHandlerState = (
action: ResizeHandlerAction,
isActive: boolean,
event: ResizeEvent
event: ResizeEvent | null
) => void;

export type PointerHitAreaMargins = {
Expand Down Expand Up @@ -83,6 +83,10 @@ export function registerResizeHandle(
}

updateCursor();

// Also instruct the handle to stop dragging; this prevents the parent group from being left in an inconsistent state
// See github.com/bvaughn/react-resizable-panels/issues/402
setResizeHandlerState("up", true, null);
}
};
}
Expand Down

0 comments on commit a5c602c

Please sign in to comment.