Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: drawer failing to cancel move event #440

Merged
merged 1 commit into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
});
Loading