Open History to see the last commit
1import * as React from 'react';
2import { useViewportScroll, useTransform } from 'framer-motion';
3import { throttle } from './utils/throttle';
4β
5const throttleFn = cb => throttle(cb, 100);
6β
7export const useOverScroll = () => {
8 const {
9 scrollY
10 } = useViewportScroll();
11 const ref = React.useRef(null);
12 const refInner = React.useRef(null);
13 const [height, setHeight] = React.useState(typeof window !== 'undefined' ? window.innerHeight : 0);
14 const [rect, setRect] = React.useState({
15 top: 0,
16 left: 0,
17 right: 0,
18 bottom: 0,
19 x: 0,
20 y: 0
21 });
22 const [rectInner, setRectInner] = React.useState({
23 top: 0,
24 left: 0,
25 right: 0,
26 bottom: 0,
27 x: 0,
28 y: 0
29 });
30 const y = useTransform(scrollY, [rect.bottom - height, rect.top + 300], [0, -rectInner.height + rect.height]);
31 React.useEffect(() => {
32 if (ref.current) {
33 setRect(ref.current.getBoundingClientRect());
34 }
35 }, [setRect, ref]);
36 React.useEffect(() => {
37 if (refInner.current) {
38 setRectInner(refInner.current.getBoundingClientRect());
39 }
40 }, [setRectInner, refInner]);
41 return {
42 ref,
43 refInner,
44 y
45 };
46};