11"use client" ;
22
33import { ReactLenis } from "lenis/react" ;
4- import { ReactNode , useEffect , useRef } from "react" ;
4+ import { ReactNode , useEffect , useRef , useState } from "react" ;
55import gsap from "gsap" ;
66import { ScrollTrigger } from "gsap/ScrollTrigger" ;
77
@@ -13,30 +13,46 @@ interface SmoothScrollingProps {
1313
1414export default function SmoothScrolling ( { children } : SmoothScrollingProps ) {
1515 const lenisRef = useRef < any > ( null ) ;
16+ const [ isTouch , setIsTouch ] = useState < boolean > ( false ) ;
1617
1718 useEffect ( ( ) => {
19+ const isTouchDevice =
20+ "ontouchstart" in window ||
21+ navigator . maxTouchPoints > 0 ||
22+ window . matchMedia ( "(pointer: coarse)" ) . matches ;
23+
24+ setIsTouch ( isTouchDevice ) ;
25+
1826 gsap . config ( { force3D : true } ) ;
1927 ScrollTrigger . config ( {
2028 limitCallbacks : true ,
2129 ignoreMobileResize : true
2230 } ) ;
2331
24- // Sync Lenis scroll with GSAP ticker to prevent jitter/bouncing
25- function update ( time : number ) {
26- lenisRef . current ?. lenis ?. raf ( time * 1000 ) ;
32+ if ( ! isTouchDevice ) {
33+ function update ( time : number ) {
34+ if ( lenisRef . current ?. lenis ) {
35+ lenisRef . current . lenis . raf ( time * 1000 ) ;
36+ }
37+ }
38+
39+ gsap . ticker . add ( update ) ;
40+ gsap . ticker . lagSmoothing ( 0 ) ; // Important for perfect sync
41+
42+ return ( ) => {
43+ gsap . ticker . remove ( update ) ;
44+ } ;
2745 }
28-
29- gsap . ticker . add ( update ) ;
30- gsap . ticker . lagSmoothing ( 0 ) ; // Important for perfect sync
31-
32- return ( ) => {
33- gsap . ticker . remove ( update ) ;
34- } ;
3546 } , [ ] ) ;
3647
48+ if ( isTouch ) {
49+ return < > { children } </ > ;
50+ }
51+
3752 return (
3853 < ReactLenis root ref = { lenisRef } autoRaf = { false } options = { { lerp : 0.1 , duration : 1.5 , smoothWheel : true } } >
3954 { children }
4055 </ ReactLenis >
4156 ) ;
4257}
58+
0 commit comments