Skip to content

Commit

Permalink
fix: drawer failing to cancel move event (#440)
Browse files Browse the repository at this point in the history
  • Loading branch information
emilkowalski authored Sep 24, 2024
1 parent d0162ed commit 824ea35
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -731,6 +731,7 @@ export const Content = React.forwardRef<HTMLDivElement, ContentProps>(function (
const [delayedSnapPoints, setDelayedSnapPoints] = React.useState(false);
const composedRef = useComposedRefs(ref, drawerRef);
const pointerStartRef = React.useRef<{ x: number; y: number } | null>(null);
const lastKnownPointerEventRef = React.useRef<React.PointerEvent<HTMLDivElement> | null>(null);
const wasBeyondThePointRef = React.useRef(false);
const hasSnapPoints = snapPoints && snapPoints.length > 0;
useScaleBackground();
Expand Down Expand Up @@ -767,6 +768,12 @@ export const Content = React.forwardRef<HTMLDivElement, ContentProps>(function (
}
}, []);

function handleOnPointerUp(event: React.PointerEvent<HTMLDivElement>) {
pointerStartRef.current = null;
wasBeyondThePointRef.current = false;
onRelease(event);
}

return (
<DialogPrimitive.Content
data-vaul-drawer-direction={direction}
Expand Down Expand Up @@ -810,6 +817,7 @@ export const Content = React.forwardRef<HTMLDivElement, ContentProps>(function (
}
}}
onPointerMove={(event) => {
lastKnownPointerEventRef.current = event;
if (handleOnly) return;
rest.onPointerMove?.(event);
if (!pointerStartRef.current) return;
Expand All @@ -831,6 +839,14 @@ export const Content = React.forwardRef<HTMLDivElement, ContentProps>(function (
wasBeyondThePointRef.current = false;
onRelease(event);
}}
onPointerOut={(event) => {
rest.onPointerOut?.(event);
handleOnPointerUp(lastKnownPointerEventRef.current);
}}
onContextMenu={(event) => {
rest.onContextMenu?.(event);
handleOnPointerUp(lastKnownPointerEventRef.current);
}}
/>
);
});
Expand Down
10 changes: 10 additions & 0 deletions test/tests/base.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,13 @@ test.describe('Base tests', () => {
await expect(page.getByTestId('content')).toBeVisible();
});
});

test('should close when dragged down and cancelled', async ({ page }) => {
await openDrawer(page);
await page.hover('[data-vaul-drawer]');
await page.mouse.down();
await page.mouse.move(0, 800);
await page.dispatchEvent('[data-vaul-drawer]', 'contextmenu');
await page.waitForTimeout(ANIMATION_DURATION);
await expect(page.getByTestId('content')).not.toBeVisible();
});

0 comments on commit 824ea35

Please sign in to comment.