Skip to content

armgit / vaul

Public Imported from GitHub
0

A drawer component for React.

fix: scroll top (#406)

Emil Kowalskic6118e728027
diff --git a/src/index.tsx b/src/index.tsx
index 2f6a766..0c3ae4e 100644
--- a/src/index.tsx
+++ b/src/index.tsx
@@ -1,12 +1,11 @@
'use client';
import * as DialogPrimitive from '@radix-ui/react-dialog';
-import React, { useRef, useState } from 'react';
+import React from 'react';
import { DrawerContext, useDrawerContext } from './context';
import './style.css';
import { usePreventScroll, isInput, isIOS, useIsomorphicLayoutEffect } from './use-prevent-scroll';
import { useComposedRefs } from './use-composed-refs';
-import { usePositionFixed } from './use-position-fixed';
import { useSnapPoints } from './use-snap-points';
import { set, reset, getTranslate, dampenValue, isVertical } from './helpers';
import { TRANSITIONS, VELOCITY_THRESHOLD } from './constants';
@@ -133,15 +132,6 @@ export function Root({
isDisabled: !isOpen || isDragging || !modal || justReleased || !hasBeenOpened || disablePreventScroll,
});
- const { restorePositionSetting } = usePositionFixed({
- isOpen,
- modal,
- nested,
- hasBeenOpened,
- preventScrollRestoration,
- noBodyStyles,
- });
-
function getScale() {
return (window.innerWidth - WINDOW_TOP_OFFSET) / window.innerWidth;
}
@@ -347,7 +337,6 @@ export function Root({
React.useEffect(() => {
return () => {
scaleBackground(false);
- restorePositionSetting();
};
}, []);
diff --git a/src/use-position-fixed.ts b/src/use-position-fixed.ts
deleted file mode 100644
index 1c81a2b..0000000
--- a/src/use-position-fixed.ts
+++ /dev/null
@@ -1,115 +0,0 @@
-import React from 'react';
-
-let previousBodyPosition: Record<string, string> | null = null;
-
-export function usePositionFixed({
- isOpen,
- modal,
- nested,
- hasBeenOpened,
- preventScrollRestoration,
- noBodyStyles,
-}: {
- isOpen: boolean;
- modal: boolean;
- nested: boolean;
- hasBeenOpened: boolean;
- preventScrollRestoration: boolean;
- noBodyStyles: boolean;
-}) {
- const [activeUrl, setActiveUrl] = React.useState(() => (typeof window !== 'undefined' ? window.location.href : ''));
- const scrollPos = React.useRef(0);
-
- const setPositionFixed = React.useCallback(() => {
- // If previousBodyPosition is already set, don't set it again.
- if (previousBodyPosition === null && isOpen && !noBodyStyles) {
- previousBodyPosition = {
- position: document.body.style.position,
- top: document.body.style.top,
- left: document.body.style.left,
- height: document.body.style.height,
- right: 'unset',
- };
-
- // Update the dom inside an animation frame
- const { scrollX, innerHeight } = window;
-
- document.body.style.setProperty('position', 'fixed', 'important');
- Object.assign(document.body.style, {
- top: `${-scrollPos.current}px`,
- left: `${-scrollX}px`,
- right: '0px',
- height: 'auto',
- });
-
- window.setTimeout(
- () =>
- window.requestAnimationFrame(() => {
- // Attempt to check if the bottom bar appeared due to the position change
- const bottomBarHeight = innerHeight - window.innerHeight;
- if (bottomBarHeight && scrollPos.current >= innerHeight) {
- // Move the content further up so that the bottom bar doesn't hide it
- document.body.style.top = `${-(scrollPos.current + bottomBarHeight)}px`;
- }
- }),
- 300,
- );
- }
- }, [isOpen]);
-
- const restorePositionSetting = React.useCallback(() => {
- if (previousBodyPosition !== null && !noBodyStyles) {
- // 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
- Object.assign(document.body.style, previousBodyPosition);
-
- window.requestAnimationFrame(() => {
- if (preventScrollRestoration && activeUrl !== window.location.href) {
- setActiveUrl(window.location.href);
- return;
- }
-
- window.scrollTo(x, y);
- });
-
- previousBodyPosition = null;
- }
- }, [activeUrl]);
-
- React.useEffect(() => {
- function onScroll() {
- scrollPos.current = window.scrollY;
- }
-
- onScroll();
-
- window.addEventListener('scroll', onScroll);
-
- return () => {
- window.removeEventListener('scroll', onScroll);
- };
- }, []);
-
- React.useEffect(() => {
- if (nested || !hasBeenOpened) return;
- // This is needed to force Safari toolbar to show **before** the drawer starts animating to prevent a gnarly shift from happening
- if (isOpen) {
- // avoid for standalone mode (PWA)
- const isStandalone = window.matchMedia('(display-mode: standalone)').matches;
- !isStandalone && setPositionFixed();
-
- if (!modal) {
- window.setTimeout(() => {
- restorePositionSetting();
- }, 500);
- }
- } else {
- restorePositionSetting();
- }
- }, [isOpen, hasBeenOpened, activeUrl, modal, nested, setPositionFixed, restorePositionSetting]);
-
- return { restorePositionSetting };
-}