Skip to content

armgit / vaul

Public Imported from GitHub
0

A drawer component for React.

Fix no scroll upon navigation (#155)

Emil Kowalski59937a54d68a
diff --git a/src/index.tsx b/src/index.tsx
index 319ff66..c194ba0 100644
--- a/src/index.tsx
+++ b/src/index.tsx
@@ -117,14 +117,19 @@ function Root({
isDisabled: !isOpen || isDragging || !modal || justReleased || !hasBeenOpened,
});
- const { restorePositionSetting } = usePositionFixed({ isOpen, modal, nested, hasBeenOpened });
+ const { restorePositionSetting } = usePositionFixed({
+ isOpen,
+ modal,
+ nested,
+ hasBeenOpened,
+ });
function getScale() {
return (window.innerWidth - WINDOW_TOP_OFFSET) / window.innerWidth;
}
function onPress(event: React.PointerEvent<HTMLDivElement>) {
- if ((!dismissible && !snapPoints) || isDragging) return;
+ if (!dismissible && !snapPoints) return;
if (drawerRef.current && !drawerRef.current.contains(event.target as Node)) return;
drawerHeightRef.current = drawerRef.current?.getBoundingClientRect().height || 0;
setIsDragging(true);
diff --git a/src/use-position-fixed.ts b/src/use-position-fixed.ts
index 3a17f53..f98e05c 100644
--- a/src/use-position-fixed.ts
+++ b/src/use-position-fixed.ts
@@ -13,6 +13,7 @@ export function usePositionFixed({
nested: boolean;
hasBeenOpened: boolean;
}) {
+ const [activeUrl, setActiveUrl] = React.useState(typeof window !== 'undefined' ? window.location.href : '');
const scrollPos = React.useRef(0);
function setPositionFixed() {
@@ -63,6 +64,11 @@ export function usePositionFixed({
document.body.style.right = 'unset';
requestAnimationFrame(() => {
+ if (activeUrl !== window.location.href) {
+ setActiveUrl(window.location.href);
+ return;
+ }
+
window.scrollTo(x, y);
});
@@ -98,7 +104,7 @@ export function usePositionFixed({
} else {
restorePositionSetting();
}
- }, [isOpen, hasBeenOpened]);
+ }, [isOpen, hasBeenOpened, activeUrl]);
return { restorePositionSetting };
}