forked from emilkowalski/vaul
-
Notifications
You must be signed in to change notification settings - Fork 0
/
use-position-fixed.ts
145 lines (120 loc) · 4.42 KB
/
use-position-fixed.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import React from 'react';
import { isSafari } from './use-prevent-scroll';
let previousBodyPosition: Record<string, string> | null = null;
/**
* This hook is necessary to prevent buggy behavior on iOS devices (need to test on Android).
* I won't get into too much detail about what bugs it solves, but so far I've found that setting the body to `position: fixed` is the most reliable way to prevent those bugs.
* Issues that this hook solves:
* https://github.com/emilkowalski/vaul/issues/435
* https://github.com/emilkowalski/vaul/issues/433
* And more that I discovered, but were just not reported.
*/
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(() => {
// All browsers on iOS will return true here.
if (!isSafari()) return;
// 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(() => {
// All browsers on iOS will return true here.
if (!isSafari()) return;
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 (!modal) return;
return () => {
if (typeof document === 'undefined') return;
// Another drawer is opened, safe to ignore the execution
const hasDrawerOpened = !!document.querySelector('[data-vaul-drawer]');
if (hasDrawerOpened) return;
restorePositionSetting();
};
}, [modal, restorePositionSetting]);
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 };
}