Skip to content

armgit / vaul

Public Imported from GitHub
0

A drawer component for React.

Fix scroll issues (#54)

Emil Kowalskif3b8333ebf99
diff --git a/src/index.tsx b/src/index.tsx
index 206c7e5..a9dc76a 100644
--- a/src/index.tsx
+++ b/src/index.tsx
@@ -128,6 +128,7 @@ function Root({
defaultProp: defaultOpen,
onChange: onOpenChange,
});
+ const [isFullyClosed, setIsFullyClosed] = React.useState(true);
const [isDragging, setIsDragging] = React.useState(false);
const [isAnimating, setIsAnimating] = React.useState(true);
const overlayRef = React.useRef<HTMLDivElement>(null);
@@ -149,7 +150,7 @@ function Root({
isDisabled: !isOpen || isDragging || isAnimating,
});
- usePositionFixed(isOpen);
+ usePositionFixed({ isOpen, isFullyClosed });
function getScale() {
return (window.innerWidth - WINDOW_TOP_OFFSET) / window.innerWidth;
@@ -315,6 +316,20 @@ function Root({
return () => window.visualViewport.removeEventListener('resize', onVisualViewportChange);
}, []);
+ React.useEffect(() => {
+ if (isOpen) {
+ setIsFullyClosed(false);
+ }
+
+ if (!isOpen) {
+ let id = setTimeout(() => {
+ setIsFullyClosed(true);
+ }, ANIMATION_DURATION);
+
+ return () => clearTimeout(id);
+ }
+ }, [isOpen, isAnimating]);
+
function closeDrawer() {
if (!dismissible) return;
drawerRef.current.setAttribute('vaul-closed-by-dragging', 'true');
diff --git a/src/use-position-fixed.ts b/src/use-position-fixed.ts
index d9057e1..238325b 100644
--- a/src/use-position-fixed.ts
+++ b/src/use-position-fixed.ts
@@ -1,67 +1,82 @@
-import { useEffect } from 'react';
+import React from 'react';
import { isIOS } from './use-prevent-scroll';
let previousBodyPosition = null;
-function setPositionFixed() {
- // If previousBodyPosition is already set, don't set it again.
- if (previousBodyPosition === null) {
- previousBodyPosition = {
- position: document.body.style.position,
- top: document.body.style.top,
- left: document.body.style.left,
- };
+export function usePositionFixed({ isOpen, isFullyClosed }: { isOpen: boolean; isFullyClosed: boolean }) {
+ const scrollPos = React.useRef(0);
+
+ function setPositionFixed() {
+ // If previousBodyPosition is already set, don't set it again.
+ if (previousBodyPosition === null) {
+ previousBodyPosition = {
+ position: document.body.style.position,
+ top: document.body.style.top,
+ left: document.body.style.left,
+ };
+
+ // Update the dom inside an animation frame
+ const { scrollY, scrollX, innerHeight } = window;
- // Update the dom inside an animation frame
- const { scrollY, scrollX, innerHeight } = window;
- document.body.style.setProperty('position', 'fixed', 'important');
- document.body.style.top = `${-scrollY}px`;
- document.body.style.left = `${-scrollX}px`;
- document.body.style.right = '0px';
+ document.body.style.setProperty('position', 'fixed', 'important');
+ document.body.style.top = `${-scrollPos.current}px`;
+ document.body.style.left = `${-scrollX}px`;
+ document.body.style.right = '0px';
- setTimeout(
- () =>
- requestAnimationFrame(() => {
- // Attempt to check if the bottom bar appeared due to the position change
- const bottomBarHeight = innerHeight - window.innerHeight;
- if (bottomBarHeight && scrollY >= innerHeight) {
- // Move the content further up so that the bottom bar doesn't hide it
- document.body.style.top = `${-(scrollY + bottomBarHeight)}px`;
- }
- }),
- 300,
- );
+ setTimeout(
+ () =>
+ requestAnimationFrame(() => {
+ // Attempt to check if the bottom bar appeared due to the position change
+ const bottomBarHeight = innerHeight - window.innerHeight;
+ if (bottomBarHeight && scrollY >= innerHeight) {
+ // Move the content further up so that the bottom bar doesn't hide it
+ document.body.style.top = `${-(scrollPos.current + bottomBarHeight)}px`;
+ }
+ }),
+ 300,
+ );
+ }
}
-}
-function restorePositionSetting() {
- if (previousBodyPosition !== null) {
- // Convert the position from "px" to Int
- const y = -parseInt(document.body.style.top, 10);
- const x = -parseInt(document.body.style.left, 10);
+ function restorePositionSetting() {
+ if (previousBodyPosition !== null) {
+ // Convert the position from "px" to Int
+ const y = -parseInt(document.body.style.top, 10);
+ const x = -parseInt(document.body.style.left, 10);
- // Restore styles
- document.body.style.position = previousBodyPosition.position;
- document.body.style.top = previousBodyPosition.top;
- document.body.style.left = previousBodyPosition.left;
- document.body.style.right = 'unset';
+ // Restore styles
+ document.body.style.position = previousBodyPosition.position;
+ document.body.style.top = previousBodyPosition.top;
+ document.body.style.left = previousBodyPosition.left;
+ document.body.style.right = 'unset';
- // Restore scroll
- requestAnimationFrame(() => {
- window.scrollTo(x, y);
- });
+ requestAnimationFrame(() => {
+ window.scrollTo(x, y);
+ });
- previousBodyPosition = null;
+ previousBodyPosition = null;
+ }
}
-}
-export function usePositionFixed(isOpen: boolean) {
- useEffect(() => {
+ React.useEffect(() => {
+ function onScroll() {
+ scrollPos.current = window.scrollY;
+ }
+
+ window.addEventListener('scroll', onScroll);
+
+ return () => {
+ window.removeEventListener('scroll', onScroll);
+ };
+ }, []);
+
+ React.useEffect(() => {
+ if (!isIOS()) return;
// This is needed to force Safari toolbar to show **before** the drawer starts animating to prevent a gnarly shift from happenning
- if (isOpen && isIOS()) {
+ if (isOpen) {
setPositionFixed();
- } else {
+ } else if (isFullyClosed) {
restorePositionSetting();
}
- }, [isOpen]);
+ }, [isOpen, isFullyClosed]);
}
diff --git a/src/use-prevent-scroll.ts b/src/use-prevent-scroll.ts
index 0bb3783..2b5a04f 100644
--- a/src/use-prevent-scroll.ts
+++ b/src/use-prevent-scroll.ts
@@ -253,7 +253,7 @@ function preventScrollMobileSafari() {
let restoreStyles = chain(
setStyle(document.documentElement, 'paddingRight', `${window.innerWidth - document.documentElement.clientWidth}px`),
setStyle(document.documentElement, 'overflow', 'hidden'),
- setStyle(document.body, 'marginTop', `-${scrollY}px`),
+ // setStyle(document.body, 'marginTop', `-${scrollY}px`),
);
// Scroll to the top. The negative margin on the body will make this appear the same.