Skip to content

armgit / vaul

Public Imported from GitHub
0

A drawer component for React.

Issues when using drawer with autofocus input on iOS (#258)

Kirill Kleymenov7e340d342521
diff --git a/README.md b/README.md
index 74041ac..7d2cdca 100644
--- a/README.md
+++ b/README.md
@@ -66,6 +66,8 @@ Additional props:
`preventScrollRestoration`: When `true` it prevents scroll restoration when the drawer is closed after a navigation happens inside of it. Defaults to `true`.
+`disablePreventScroll`: When `true` scroll prevention mechanism will be disabled. Scroll prevention ensures that page will not scroll on mobile when opening drawer. However this mechanism gets confused when drawer has an input with autofocus and therefore opens simulataneosly with touch keyboard. Defaults to `true`. `modal` set to `false` also disables it.
+
`[data-vaul-no-drag]`: When interacting with an element with this data attribute, the drawer won't be dragged.
### Trigger
diff --git a/src/index.tsx b/src/index.tsx
index c64327e..f3b9cfb 100644
--- a/src/index.tsx
+++ b/src/index.tsx
@@ -52,6 +52,7 @@ type DialogProps = {
onClose?: () => void;
direction?: 'top' | 'bottom' | 'left' | 'right';
preventScrollRestoration?: boolean;
+ disablePreventScroll?: boolean;
} & (WithFadeFromProps | WithoutFadeFromProps);
function Root({
@@ -74,6 +75,7 @@ function Root({
onClose,
direction = 'bottom',
preventScrollRestoration = true,
+ disablePreventScroll = false,
}: DialogProps) {
const [isOpen = false, setIsOpen] = React.useState<boolean>(false);
const [hasBeenOpened, setHasBeenOpened] = React.useState<boolean>(false);
@@ -122,7 +124,7 @@ function Root({
});
usePreventScroll({
- isDisabled: !isOpen || isDragging || !modal || justReleased || !hasBeenOpened,
+ isDisabled: !isOpen || isDragging || !modal || justReleased || !hasBeenOpened || disablePreventScroll,
});
const { restorePositionSetting } = usePositionFixed({
@@ -151,7 +153,7 @@ function Root({
// Ensure we maintain correct pointer capture even when going outside of the drawer
(event.target as HTMLElement).setPointerCapture(event.pointerId);
- pointerStart.current = isVertical(direction) ? event.screenY : event.screenX;
+ pointerStart.current = isVertical(direction) ? event.clientY : event.clientX;
}
function shouldDrag(el: EventTarget, isDraggingInDirection: boolean) {
@@ -233,7 +235,7 @@ function Root({
if (isDragging) {
const directionMultiplier = direction === 'bottom' || direction === 'right' ? 1 : -1;
const draggedDistance =
- (pointerStart.current - (isVertical(direction) ? event.screenY : event.screenX)) * directionMultiplier;
+ (pointerStart.current - (isVertical(direction) ? event.clientY : event.clientX)) * directionMultiplier;
const isDraggingInDirection = draggedDistance > 0;
// Pre condition for disallowing dragging in the close direction.
@@ -443,8 +445,8 @@ function Root({
}
}, [isOpen, shouldScaleBackground]);
- // This can be done much better
- React.useEffect(() => {
+ // LayoutEffect to prevent extra render where openProp and isOpen are not synced yet
+ React.useLayoutEffect(() => {
if (openProp) {
setIsOpen(true);
setHasBeenOpened(true);
@@ -527,7 +529,7 @@ function Root({
if (dragStartTime.current === null) return;
const timeTaken = dragEndTime.current.getTime() - dragStartTime.current.getTime();
- const distMoved = pointerStart.current - (isVertical(direction) ? event.screenY : event.screenX);
+ const distMoved = pointerStart.current - (isVertical(direction) ? event.clientY : event.clientX);
const velocity = Math.abs(distMoved) / timeTaken;
if (velocity > 0.05) {
diff --git a/test/tests/base.spec.ts b/test/tests/base.spec.ts
index 37090e6..207098d 100644
--- a/test/tests/base.spec.ts
+++ b/test/tests/base.spec.ts
@@ -40,15 +40,15 @@ test.describe('Base tests', () => {
await expect(page.getByTestId('content')).not.toBeVisible();
});
- // test('should close when dragged down', async ({ page }) => {
- // await openDrawer(page);
- // await page.hover('[vaul-drawer]');
- // await page.mouse.down();
- // await page.mouse.move(0, 800);
- // await page.mouse.up();
- // await page.waitForTimeout(ANIMATION_DURATION);
- // await expect(page.getByTestId('content')).not.toBeVisible();
- // });
+ test('should close when dragged down', async ({ page }) => {
+ await openDrawer(page);
+ await page.hover('[vaul-drawer]');
+ await page.mouse.down();
+ await page.mouse.move(0, 800);
+ await page.mouse.up();
+ await page.waitForTimeout(ANIMATION_DURATION);
+ await expect(page.getByTestId('content')).not.toBeVisible();
+ });
test('should not close when dragged up', async ({ page }) => {
await openDrawer(page);