"use client"; /** * Custom scrollbar — 1:1 port of the original (chunk 2689132c4e070b68.js * module 72443): * * - Desktop (lg+): a right-edge fixed 32×200 SVG track + draggable thumb. * Thumb geometry = progress mapping with min length 20 of a 188 track * (max(20, 188·clientH/totalH), start 6+progress·(188-len)). Clicking the * track jumps; dragging the thumb scrubs (lenisScrollTo immediate). * - Mobile: a 24px circular "Scroll to top" button with a progress ring. * - Auto-hides after 2s without scroll activity (o), shows on hover / drag. */ import { useCallback, useEffect, useRef, useState } from "react"; import { scrollEnv } from "@/lib/scrollEnv"; const RING_CIRCUMFERENCE = 2 * Math.PI * 12; const HIDE_AFTER_MS = 2000; const clamp = (v: number, min: number, max: number) => Math.min(max, Math.max(min, v)); export default function CustomScrollbar({ hidden = false }: { hidden?: boolean }) { const thumbRef = useRef(null); const ringRef = useRef(null); const draggingRef = useRef(false); const dragStart = useRef({ startY: 0, startScrollTop: 0 }); const abortRef = useRef(null); const hideTimer = useRef | null>(null); const rafRef = useRef(null); const [visible, setVisible] = useState(false); const [transitioning, setTransitioning] = useState(false); const [mounted, setMounted] = useState(false); const [nearBottom, setNearBottom] = useState(false); const [progress, setProgress] = useState(0); const scheduleHide = useCallback((delay = HIDE_AFTER_MS) => { setVisible(true); if (hideTimer.current) clearTimeout(hideTimer.current); hideTimer.current = setTimeout(() => setVisible(false), delay); }, []); const update = useCallback(() => { const container = scrollEnv.getContainerEl(); const scrollTop = scrollEnv.getScrollTopPx(); const near = Math.max(0, (container?.scrollHeight ?? document.documentElement.scrollHeight) - scrollTop) <= window.innerHeight * clamp(nearBottomRatio(), 0, 1); setNearBottom((prev) => (prev === near ? prev : near)); const totalH = Math.max(1, container?.scrollHeight ?? document.documentElement.scrollHeight); const clientH = Math.max(1, container?.clientHeight ?? window.innerHeight); const p = clamp(scrollTop / Math.max(1, totalH - clientH), 0, 1); setProgress(p); const thumbLen = Math.max(20, 188 * clamp(clientH / totalH, 0, 1)); const start = 6 + p * (188 - thumbLen); thumbRef.current?.setAttribute("d", `M 16 ${start} V ${start + thumbLen}`); ringRef.current?.setAttribute("stroke-dashoffset", String(RING_CIRCUMFERENCE * (1 - p))); // nearBottom threshold (original useNearBottom(.5)): keep simple ratio hook void near; }, []); useEffect(() => { const container = scrollEnv.getContainerEl(); if (!container) return; setMounted(true); scheduleHide(3000); // First-paint show (original x flag via rAF). if (!rafRef.current) { rafRef.current = requestAnimationFrame(() => setTransitioning(true)); } const onScroll = () => { update(); scheduleHide(); }; update(); const ro = new ResizeObserver(update); ro.observe(container); const firstChild = container.firstElementChild; if (firstChild instanceof HTMLElement) ro.observe(firstChild); const rafs: number[] = []; let n = 3; const tick = () => { update(); if (--n > 0) rafs.push(requestAnimationFrame(tick)); }; rafs.push(requestAnimationFrame(tick)); const onLoad = () => update(); window.addEventListener("load", onLoad, { passive: true }); window.addEventListener("scroll", onScroll, { passive: true }); return () => { if (hideTimer.current) clearTimeout(hideTimer.current); if (rafRef.current) cancelAnimationFrame(rafRef.current); for (const r of rafs) cancelAnimationFrame(r); ro.disconnect(); window.removeEventListener("load", onLoad); window.removeEventListener("scroll", onScroll); }; }, [scheduleHide, update]); const measure = useCallback(() => { const container = scrollEnv.getContainerEl(); const totalH = container?.scrollHeight ?? Math.max(document.documentElement.scrollHeight, document.body.scrollHeight, 1); const clientH = container?.clientHeight ?? Math.max(1, window.innerHeight || document.documentElement.clientHeight || 1); return { el: container, totalH, clientH, validH: Math.max(1, totalH - clientH) }; }, []); const onDragMove = useCallback( (e: MouseEvent) => { if (!draggingRef.current) return; const dy = e.clientY - dragStart.current.startY; const { totalH, clientH, validH } = measure(); const trackLen = 188 - Math.max(20, 188 * (totalH > 0 ? clamp(clientH / totalH, 0, 1) : 1)); if (trackLen <= 0) return; const target = dragStart.current.startScrollTop + (validH / trackLen) * dy; scrollEnv.lenisScrollTo(target, { immediate: true }); }, [measure] ); const onDragEnd = useCallback(() => { draggingRef.current = false; abortRef.current?.abort(); abortRef.current = null; document.body.style.userSelect = ""; if (hideTimer.current) clearTimeout(hideTimer.current); hideTimer.current = setTimeout(() => setVisible(false), HIDE_AFTER_MS); }, []); const onThumbDown = useCallback( (e: React.MouseEvent) => { e.preventDefault(); e.stopPropagation(); draggingRef.current = true; dragStart.current = { startY: e.clientY, startScrollTop: scrollEnv.getScrollTopPx(), }; if (hideTimer.current) clearTimeout(hideTimer.current); setVisible(true); document.body.style.userSelect = "none"; abortRef.current?.abort(); abortRef.current = new AbortController(); const signal = abortRef.current.signal; document.addEventListener("mousemove", onDragMove, { signal }); document.addEventListener("mouseup", onDragEnd, { signal }); }, [onDragEnd, onDragMove] ); const desktopCls = [ "pointer-events-auto top-1/2 right-0 bottom-0 z-30 fixed w-14 px-3 h-50 -translate-y-1/2 hidden lg:block", transitioning ? "transition-opacity duration-500" : "", visible ? "opacity-100" : "opacity-0", hidden ? " hidden!" : "", ] .filter(Boolean) .join(" "); return ( <> {mounted && (
{ setVisible(true); if (hideTimer.current) clearTimeout(hideTimer.current); }} onMouseLeave={() => { if (hideTimer.current) clearTimeout(hideTimer.current); hideTimer.current = setTimeout(() => setVisible(false), HIDE_AFTER_MS); }} > { const rect = e.currentTarget.getBoundingClientRect(); const y = clamp((e.clientY - rect.top) * (200 / rect.height), 6, 194); const ratio = (y - 6) / 188; const { validH } = measure(); scrollEnv.lenisScrollTo(ratio * validH, { immediate: false }); }} > e.stopPropagation()} />
)} {mounted && (
scrollEnv.scrollToTop("smooth")} onKeyDown={(e) => { if (e.key === "Enter" || e.key === " ") { e.preventDefault(); scrollEnv.scrollToTop("smooth"); } }} >
)} ); } /** Original useNearBottom(.5) ratio. */ function nearBottomRatio() { return 0.5; }