58 lines
1.6 KiB
TypeScript
58 lines
1.6 KiB
TypeScript
'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.2,
|
||
|
|
easing: (t) => Math.min(1, 1.001 - Math.pow(2, -10 * t)),
|
||
|
|
orientation: 'vertical',
|
||
|
|
gestureOrientation: 'vertical',
|
||
|
|
smoothWheel: true,
|
||
|
|
wheelMultiplier: 0.95,
|
||
|
|
touchMultiplier: 1.5,
|
||
|
|
});
|
||
|
|
|
||
|
|
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;
|
||
|
|
}
|