Files
webb/src/components/ui/SmoothScroll.tsx
T

59 lines
1.7 KiB
TypeScript
Raw Normal View History

'use client';
import { useEffect } from 'react';
import Lenis from 'lenis';
export function SmoothScroll() {
useEffect(() => {
// Check prefers-reduced-motion
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)),
orientation: 'vertical',
gestureOrientation: 'vertical',
smoothWheel: true,
syncTouch: false, // Preserves hardware 120Hz momentum touch scrolling on phones & tablets
wheelMultiplier: 0.95,
touchMultiplier: 1.0,
});
let rafId: number;
function raf(time: number) {
lenis.raf(time);
rafId = requestAnimationFrame(raf);
}
rafId = requestAnimationFrame(raf);
// Smooth scroll for anchor clicks (e.g. #studio-suite, #flagship-models)
const handleAnchorClick = (e: MouseEvent) => {
const target = e.target as HTMLElement;
const anchor = target.closest('a');
if (!anchor) return;
const href = anchor.getAttribute('href');
if (href && href.startsWith('#') && href.length > 1) {
const targetEl = document.querySelector(href);
if (targetEl) {
e.preventDefault();
lenis.scrollTo(targetEl as HTMLElement, { offset: -90, duration: 1.2 });
}
}
};
document.addEventListener('click', handleAnchorClick);
// Expose lenis to window for external triggers
(window as any).lenis = lenis;
return () => {
cancelAnimationFrame(rafId);
document.removeEventListener('click', handleAnchorClick);
lenis.destroy();
delete (window as any).lenis;
};
}, []);
return null;
}