forked from codechefPesuecc/CodeChef-PESUECC-Chapter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSmoothScroll.tsx
More file actions
35 lines (29 loc) · 893 Bytes
/
Copy pathSmoothScroll.tsx
File metadata and controls
35 lines (29 loc) · 893 Bytes
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
"use client";
import { useEffect } from "react";
import Lenis from "lenis";
/**
* Enables Lenis momentum ("smooth & premium") scrolling site-wide.
* Uses native scroll under the hood, so window.scrollY and motion's
* useScroll keep working. Renders nothing.
*/
export default function SmoothScroll() {
useEffect(() => {
// Respect the user's OS "reduce motion" preference — keep native scroll.
if (window.matchMedia("(prefers-reduced-motion: reduce)").matches) return;
const lenis = new Lenis({
duration: 1.15,
easing: (t) => Math.min(1, 1.001 - Math.pow(2, -10 * t)),
});
let rafId = 0;
const raf = (time: number) => {
lenis.raf(time);
rafId = requestAnimationFrame(raf);
};
rafId = requestAnimationFrame(raf);
return () => {
cancelAnimationFrame(rafId);
lenis.destroy();
};
}, []);
return null;
}