feat(ui): add liquid glass lens cursor and ultra-smooth Lenis scrolling
This commit is contained in:
@@ -0,0 +1,88 @@
|
||||
'use client';
|
||||
|
||||
import React, { useEffect } from 'react';
|
||||
import Lenis from 'lenis';
|
||||
|
||||
interface SmoothScrollProviderProps {
|
||||
children: React.ReactNode;
|
||||
}
|
||||
|
||||
/**
|
||||
* High-performance, lightweight Lenis Smooth Scrolling Provider.
|
||||
* Synchronizes with display refresh rate (60-120fps) via requestAnimationFrame.
|
||||
* Features:
|
||||
* - Fluid exponential easing (Apple/Linear quality)
|
||||
* - Native momentum & touch compatibility
|
||||
* - Smooth scroll interception for internal anchor links (#our-tools, #recommended-apps, etc.)
|
||||
* - Automatic offset handling for sticky floating navigation
|
||||
* - Respects prefers-reduced-motion
|
||||
*/
|
||||
export function SmoothScrollProvider({ children }: SmoothScrollProviderProps) {
|
||||
useEffect(() => {
|
||||
// Only run on client-side and non-reduced-motion environments
|
||||
if (typeof window === 'undefined') return;
|
||||
|
||||
const prefersReducedMotion = window.matchMedia('(prefers-reduced-motion: reduce)').matches;
|
||||
if (prefersReducedMotion) return;
|
||||
|
||||
// Initialize ultra-lightweight Lenis instance
|
||||
const lenis = new Lenis({
|
||||
duration: 1.15,
|
||||
easing: (t: number) => Math.min(1, 1.001 - Math.pow(2, -10 * t)), // Silky exponential ease-out
|
||||
orientation: 'vertical',
|
||||
gestureOrientation: 'vertical',
|
||||
smoothWheel: true,
|
||||
wheelMultiplier: 0.95,
|
||||
touchMultiplier: 1.25,
|
||||
infinite: false,
|
||||
});
|
||||
|
||||
// Expose lenis instance globally for programmatic scrolling
|
||||
(window as any).__lenis = lenis;
|
||||
|
||||
// Locked high-frequency RAF loop
|
||||
let animId = 0;
|
||||
const raf = (time: number) => {
|
||||
lenis.raf(time);
|
||||
animId = requestAnimationFrame(raf);
|
||||
};
|
||||
animId = requestAnimationFrame(raf);
|
||||
|
||||
// Intercept in-page anchor links for seamless animated scrolling
|
||||
const handleAnchorClick = (e: MouseEvent) => {
|
||||
const target = e.target as HTMLElement | null;
|
||||
if (!target) return;
|
||||
|
||||
const anchor = target.closest('a[href^="#"]') as HTMLAnchorElement | null;
|
||||
if (!anchor) return;
|
||||
|
||||
const href = anchor.getAttribute('href');
|
||||
if (!href || href === '#' || href === '#!') return;
|
||||
|
||||
try {
|
||||
const targetElement = document.querySelector(href);
|
||||
if (targetElement) {
|
||||
e.preventDefault();
|
||||
lenis.scrollTo(targetElement as HTMLElement, {
|
||||
offset: -80, // Offset for floating glass navbar
|
||||
duration: 1.25,
|
||||
easing: (t: number) => Math.min(1, 1.001 - Math.pow(2, -10 * t)),
|
||||
});
|
||||
}
|
||||
} catch {
|
||||
// Fallback gracefully to default navigation if querySelector fails
|
||||
}
|
||||
};
|
||||
|
||||
document.addEventListener('click', handleAnchorClick, { capture: true });
|
||||
|
||||
return () => {
|
||||
cancelAnimationFrame(animId);
|
||||
document.removeEventListener('click', handleAnchorClick, { capture: true });
|
||||
lenis.destroy();
|
||||
delete (window as any).__lenis;
|
||||
};
|
||||
}, []);
|
||||
|
||||
return <>{children}</>;
|
||||
}
|
||||
Reference in New Issue
Block a user