Files
personal/components/dom/RouteTransitionOverlay.tsx
T
virtheart a844b26250 feat: 路由转场+详情页+自定义滚动条+常显网格线+邮箱修复
- 新增 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
2026-09-01 20:33:36 +08:00

165 lines
5.0 KiB
TypeScript

"use client";
/**
* RouteTransitionOverlay — 1:1 port of the original orchestrator (chunk
* 3c6cc5b2fcccdee5.js `function M`, module 34841), simplified to the pixel
* wipe without the WebGL mask material:
*
* startNavigation(href)
* → phase "cover" : b1-colored fixed layer wipes IN over the page
* (0.8s, cubic-bezier(0.66, 0, 0.01, 1)); on complete → router.push
* (or replace) to the target href.
* → phase "wait" : new page mounts; heavy assets may still load.
* → phase "reveal" : layer wipes OUT (same easing), then → idle and
* the overlay unmounts; scramble lines re-enabled 100ms in.
*
* The b1 background matches the page body so the wipe reads as the page
* turning into a blank sheet — the original achieves the same look via the
* WebGL mask + grid colors.
*/
import { useEffect, useMemo, useRef, useState } from "react";
import { usePathname, useRouter } from "next/navigation";
import { useRouteTransitionController } from "@/lib/routeTransition";
type Phase = "wait" | "cover" | "reveal";
type Mode = "idle" | "entry" | "route";
const DURATION_MS = 800;
const EASING = "cubic-bezier(0.66, 0, 0.01, 1)";
const BG = "var(--color-b1)";
export default function RouteTransitionOverlay() {
const router = useRouter();
const pathname = usePathname();
const { targetHref, navigationReplace, clearNavigation, setAllowScrambleLines } =
useRouteTransitionController();
const routeFrom = useRef<{ path: string | null; coverDone: boolean }>({
path: null,
coverDone: false,
});
const [mode, setMode] = useState<Mode>("idle");
const [phase, setPhase] = useState<Phase>("wait");
const navigateRef = useRef<() => void>(() => {});
navigateRef.current = () => {
if (mode === "route" && phase === "cover") {
routeFrom.current.coverDone = true;
setAllowScrambleLines(false);
if (targetHref) {
if (navigationReplace) router.replace(targetHref);
else router.push(targetHref);
}
setPhase("wait");
}
};
const targetPath = useMemo(() => {
if (!targetHref) return null;
if (targetHref.startsWith("/")) {
const [p] = targetHref.split("?");
return p || "/";
}
try {
return new URL(targetHref, window.location.origin).pathname || "/";
} catch {
return null;
}
}, [targetHref]);
// Intent → start cover.
useEffect(() => {
if (targetHref && mode === "idle") {
routeFrom.current.path = pathname;
routeFrom.current.coverDone = false;
setMode("route");
setPhase("cover");
}
}, [targetHref, mode, pathname]);
// New page mounted → reveal.
useEffect(() => {
if (
mode === "route" &&
phase === "wait" &&
routeFrom.current.coverDone &&
routeFrom.current.path &&
pathname !== routeFrom.current.path
) {
setPhase("reveal");
}
}, [mode, phase, pathname]);
// Reveal finished → idle.
const onRevealComplete = () => {
if (mode !== "idle" && phase === "reveal") {
setMode("idle");
clearNavigation();
routeFrom.current.path = null;
routeFrom.current.coverDone = false;
}
};
// Scramble lines gate (original: idle → true immediately; 100ms after
// a reveal starts).
useEffect(() => {
if (mode === "idle") {
setAllowScrambleLines(true);
return;
}
if (phase !== "reveal") return;
const t = window.setTimeout(() => setAllowScrambleLines(true), 100);
return () => window.clearTimeout(t);
}, [mode, phase, setAllowScrambleLines]);
const overlayVisible = mode !== "idle" && phase !== "wait";
const wipeIn = phase === "cover";
return (
<div
aria-hidden={!overlayVisible}
style={{
position: "fixed",
inset: 0,
zIndex: 60,
background: BG,
pointerEvents: overlayVisible ? "auto" : "none",
opacity: overlayVisible ? 1 : 0,
visibility: overlayVisible ? "visible" : "hidden",
transition: overlayVisible
? `opacity 80ms linear`
: `opacity 200ms linear`,
clipPath: overlayVisible
? wipeIn
? "inset(0 0 100% 0)"
: "inset(0 0 0 0)"
: "inset(0 0 100% 0)",
...(overlayVisible
? {
animation: `route-wipe ${DURATION_MS}ms ${EASING} forwards`,
animationDirection: wipeIn ? "normal" : "reverse",
}
: {}),
}}
onAnimationEnd={(e) => {
if (e.animationName !== "route-wipe") return;
if (wipeIn) navigateRef.current();
else onRevealComplete();
}}
>
<style>{`@keyframes route-wipe { from { clip-path: inset(0 0 100% 0); } to { clip-path: inset(0 0 0 0); } }`}</style>
{/* 16px checker texture (original pixelSize:16 maskCells) */}
<div
className="absolute inset-0 pointer-events-none"
style={{
opacity: 0.05,
backgroundImage:
"repeating-conic-gradient(var(--color-l1) 0% 25%, transparent 0% 50%)",
backgroundSize: "32px 32px",
}}
/>
</div>
);
}