a844b26250
- 新增 FullscreenTransitionProvider + RouteTransitionOverlay: cover(0.8s b1 遮罩 wipe) → push → reveal 状态机(原 73475/34841 移植), Work 卡片与详情页链接全部走 startNavigation(同路径点击回顶部) - 新增 app/work/[slug] 详情页:content/workArticles.ts 从原站 6 个详情页 1:1 提取(MDX 文章+Metadata+iframe 预览+Links),/2026 锁定时渲染解锁屏 - 新增 CustomScrollbar:桌面右缘 SVG 轨道+拖拽 thumb(188 轨道/min20)、 点击跳转、2s 自动隐藏;移动端底部进度环回顶按钮(原 72443 移植) - BackgroundGrid 重写为原版算法(3 行十字/12px 缺口/12 列内分数纵线, 主页 loading 时隐藏其余时间常显);层叠改为原版结构:canvas z-auto 最前 → grid z-20 → DOM 内容,移除各 section 的 z-30 - 修复 BGM:持久化开启时首次手势自动恢复播放(autoplay 策略被拒后 pointerdown/keydown/touchstart 重试),enabled 闭包改用 ref - 页脚邮箱改为 chenchun@virtheart.com
274 lines
9.9 KiB
TypeScript
274 lines
9.9 KiB
TypeScript
"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<SVGPathElement | null>(null);
|
||
const ringRef = useRef<SVGCircleElement | null>(null);
|
||
const draggingRef = useRef(false);
|
||
const dragStart = useRef({ startY: 0, startScrollTop: 0 });
|
||
const abortRef = useRef<AbortController | null>(null);
|
||
const hideTimer = useRef<ReturnType<typeof setTimeout> | null>(null);
|
||
const rafRef = useRef<number | null>(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 && (
|
||
<div
|
||
className={desktopCls}
|
||
aria-hidden={hidden}
|
||
onMouseEnter={() => {
|
||
setVisible(true);
|
||
if (hideTimer.current) clearTimeout(hideTimer.current);
|
||
}}
|
||
onMouseLeave={() => {
|
||
if (hideTimer.current) clearTimeout(hideTimer.current);
|
||
hideTimer.current = setTimeout(() => setVisible(false), HIDE_AFTER_MS);
|
||
}}
|
||
>
|
||
<svg
|
||
width="32"
|
||
height="200"
|
||
viewBox="0 0 32 200"
|
||
fill="none"
|
||
xmlns="http://www.w3.org/2000/svg"
|
||
onClick={(e) => {
|
||
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 });
|
||
}}
|
||
>
|
||
<path
|
||
d="M 16 6 V 194"
|
||
stroke="currentColor"
|
||
strokeOpacity={0.2}
|
||
strokeWidth="6"
|
||
strokeLinejoin="round"
|
||
strokeLinecap="round"
|
||
/>
|
||
<path
|
||
ref={thumbRef}
|
||
d="M 16 6 V 6"
|
||
stroke="currentColor"
|
||
strokeWidth="6"
|
||
strokeLinejoin="round"
|
||
strokeLinecap="round"
|
||
onMouseDown={onThumbDown}
|
||
onClick={(e) => e.stopPropagation()}
|
||
/>
|
||
</svg>
|
||
</div>
|
||
)}
|
||
{mounted && (
|
||
<div
|
||
className="pointer-events-auto lg:hidden right-0 bottom-0 z-30 fixed flex justify-center items-center px-6 py-6"
|
||
role="button"
|
||
tabIndex={hidden ? -1 : 0}
|
||
aria-label="Scroll to top"
|
||
aria-hidden={hidden}
|
||
onClick={() => scrollEnv.scrollToTop("smooth")}
|
||
onKeyDown={(e) => {
|
||
if (e.key === "Enter" || e.key === " ") {
|
||
e.preventDefault();
|
||
scrollEnv.scrollToTop("smooth");
|
||
}
|
||
}}
|
||
>
|
||
<svg
|
||
width="24px"
|
||
height="24px"
|
||
viewBox="0 0 32 32"
|
||
className={`${nearBottom ? "scale-100" : "scale-50"} transition-all duration-[0.66s] ease-66 pointer-events-none`}
|
||
>
|
||
<circle
|
||
r="12"
|
||
cy="16"
|
||
cx="16"
|
||
fill="none"
|
||
strokeLinejoin="round"
|
||
strokeLinecap="round"
|
||
strokeWidth={nearBottom ? 4 : 8}
|
||
stroke="currentColor"
|
||
strokeOpacity={nearBottom ? 0.2 : 1}
|
||
transform="rotate(-90 16 16)"
|
||
style={{
|
||
transition:
|
||
"stroke-opacity 0.66s cubic-bezier(0.66, 0, 0.01, 1), stroke-width 0.66s cubic-bezier(0.66, 0, 0.01, 1)",
|
||
}}
|
||
/>
|
||
<circle
|
||
ref={ringRef}
|
||
r="12"
|
||
cy="16"
|
||
cx="16"
|
||
fill="none"
|
||
strokeLinejoin="round"
|
||
strokeLinecap="round"
|
||
strokeWidth={nearBottom ? 4 : 8}
|
||
strokeDashoffset={RING_CIRCUMFERENCE * (1 - progress)}
|
||
strokeDasharray={RING_CIRCUMFERENCE}
|
||
transform="rotate(-90 16 16)"
|
||
style={{
|
||
transition:
|
||
"stroke-opacity 0.66s cubic-bezier(0.66, 0, 0.01, 1), stroke-width 0.66s cubic-bezier(0.66, 0, 0.01, 1)",
|
||
}}
|
||
/>
|
||
</svg>
|
||
</div>
|
||
)}
|
||
</>
|
||
);
|
||
}
|
||
|
||
/** Original useNearBottom(.5) ratio. */
|
||
function nearBottomRatio() {
|
||
return 0.5;
|
||
}
|