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 #339

Closed
wants to merge 5 commits into from
Closed
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
25 changes: 20 additions & 5 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -930,9 +930,10 @@ const Content = React.forwardRef<HTMLDivElement, ContentProps>(function (
} = useDrawerContext();
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 isDeltaInDirection = (delta: { x: number; y: number }, direction: DrawerDirection, threshold = 0) => {
function isDeltaInDirection(delta: { x: number; y: number }, direction: DrawerDirection, threshold = 0) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed to regular function for the sake of consistency

if (wasBeyondThePointRef.current) return true;

const deltaY = Math.abs(delta.y);
Expand All @@ -954,13 +955,19 @@ const Content = React.forwardRef<HTMLDivElement, ContentProps>(function (

wasBeyondThePointRef.current = true;
return true;
};
}

React.useEffect(() => {
// Trigger enter animation without using CSS animation
setVisible(true);
}, []);

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

return (
<DialogPrimitive.Content
vaul-drawer=""
Expand Down Expand Up @@ -1020,8 +1027,10 @@ const Content = React.forwardRef<HTMLDivElement, ContentProps>(function (
}
}}
onPointerMove={(event) => {
lastKnownPointerEventRef.current = event;
if (handleOnly) return;
rest.onPointerMove?.(event);

if (!pointerStartRef.current) return;
const yPosition = event.clientY - pointerStartRef.current.y;
const xPosition = event.clientX - pointerStartRef.current.x;
Expand All @@ -1037,9 +1046,15 @@ const Content = React.forwardRef<HTMLDivElement, ContentProps>(function (
}}
onPointerUp={(event) => {
rest.onPointerUp?.(event);
pointerStartRef.current = null;
wasBeyondThePointRef.current = false;
onRelease(event);
handleOnPointerUp(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 @@ -50,6 +50,16 @@ test.describe('Base tests', () => {
await expect(page.getByTestId('content')).not.toBeVisible();
});

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

test('should not close when dragged up', async ({ page }) => {
await openDrawer(page);
await page.hover('[vaul-drawer]');
Expand Down
Loading