File size: 597 Bytes
63c7991
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { animate, ValueAnimationTransition } from "framer-motion";
import { useRef } from "react";

export function useScrollTo() {
  let ref = useRef<HTMLDivElement>(null);

  function scrollTo(options: ValueAnimationTransition = {}) {
    if (!ref.current) return;

    let defaultOptions: ValueAnimationTransition = {
      type: "spring",
      bounce: 0,
      duration: 0.6,
    };

    animate(window.scrollY, ref.current.offsetTop, {
      ...defaultOptions,
      ...options,
      onUpdate: (latest) => window.scrollTo({ top: latest }),
    });
  }

  return [ref, scrollTo] as const;
}