Files
personal/lib/scrollEnv.ts
T

46 lines
1.3 KiB
TypeScript

"use client";
/**
* Scroll environment — module singleton mirroring the original
* `scrollEnv` (module 29680): holds the Lenis instance so header
* shortcuts (coords click → top, [T] top, [B] bottom, Work/Contact
* anchors) can scroll from anywhere. SmoothScroller registers the
* instance on mount.
*/
type LenisLike = {
scrollTo: (
target: number | string | HTMLElement,
opts?: Record<string, unknown>
) => void;
} | null;
let lenis: LenisLike = null;
export const scrollEnv = {
setLenis(instance: LenisLike) {
lenis = instance;
},
getLenis() {
return lenis;
},
scrollToTop(behavior: "smooth" | "auto" = "smooth") {
if (lenis) lenis.scrollTo(0, { lerp: 0.1, immediate: behavior === "auto" });
else window.scrollTo({ top: 0, behavior });
},
scrollToBottom(behavior: "smooth" | "auto" = "smooth") {
const limit = Math.max(
0,
(document.documentElement?.scrollHeight ?? 0) - window.innerHeight
);
if (lenis) lenis.scrollTo(limit, { lerp: 0.1, immediate: behavior === "auto" });
else window.scrollTo({ top: limit, behavior });
},
scrollToSection(selector: string) {
const el = document.querySelector(selector);
if (!el) return;
if (lenis) lenis.scrollTo(el as HTMLElement, { lerp: 0.1 });
else (el as HTMLElement).scrollIntoView({ behavior: "smooth" });
},
};