"use client"; import { useEffect, useMemo, useRef, useState } from "react"; /** * 1:1 re-implementation of the original scramble-reveal text (chunk * 2689132c4e070b68.js module 71358 component `d`): * * - charset `ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*+-=?/<>[]{}` picked * randomly per render while a character is "scrambling" * - char i starts scrambling at startDelayMs + i*letterDelayMs and settles * after a window of 4*letterDelayMs (2 phases × 2*letterDelayMs) * - phase 0 color #c0fe04, phase 1 color #607F02 (light theme) unless * scrambleColors is false * - refresh tick every 40ms; the whole text settles at * startDelayMs + (n-1)*letterDelayMs + 4*letterDelayMs * - starts once when the element enters the viewport (threshold 0.1) * * Parts accept strings or **slot objects** `{ length, scramble?, className?, * settled? }` (original slot parts): while scrambling, slot characters show * `scramble` (default: spaces) with the same per-char delays; once settled * the slot renders `settled` (a React node, e.g. the passcode button) in * place of the characters. */ const CHARSET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*+-=?/<>[]{}"; const COLORS_LIGHT = ["#c0fe04", "#607F02"]; const randomChar = () => CHARSET[Math.floor(Math.random() * CHARSET.length)]; export type ScrambleSlotPart = { /** Number of characters this slot occupies in the reveal sequence. */ length: number; /** Placeholder characters shown while the slot is scrambling. */ scramble?: string; /** Class for the slot wrapper span. */ className?: string; /** React node rendered once the reveal has fully settled. */ settled?: React.ReactNode; }; type Part = string | ScrambleSlotPart; type NormalizedPart = | { kind: "text"; value: string } | { kind: "slot"; length: number; scramble: string; settled?: React.ReactNode; className?: string; }; function normalizeParts(parts: Part[] | undefined, text: string | undefined): NormalizedPart[] { return (parts ?? [text ?? ""]).map((p) => { if (typeof p === "string") return { kind: "text" as const, value: p }; const length = Math.max(0, p.length); const scramble = (p.scramble ?? (length > 0 ? " ".repeat(length) : "")) .slice(0, length) .padEnd(length, " "); return { kind: "slot" as const, length, scramble, settled: p.settled, className: p.className, }; }); } type Props = { text?: string; parts?: Part[]; className?: string; startDelayMs?: number; letterDelayMs?: number; scrambleColors?: boolean; /** Reveal from the last character to the first (original `reverse`). */ reverse?: boolean; }; export default function ScrambleReveal({ text, parts, className, startDelayMs = 0, letterDelayMs = 80, scrambleColors = true, reverse = false, }: Props) { const normalized = useMemo( () => normalizeParts(parts, text), [parts, text] ); const ref = useRef(null); const [entered, setEntered] = useState(false); const [startedAt, setStartedAt] = useState(null); const [elapsed, setElapsed] = useState(0); const F = normalized.reduce( (acc, p) => acc + (p.kind === "text" ? p.value.length : p.length), 0 ); const phaseLen = 2 * letterDelayMs; // j const scrambleWindow = 2 * phaseLen; // D const total = startDelayMs + Math.max(0, F - 1) * letterDelayMs + scrambleWindow; useEffect(() => { const el = ref.current; if (!el) return; const observer = new IntersectionObserver( ([entry]) => { if (entry.isIntersecting) { setEntered(true); observer.disconnect(); } }, { threshold: 0.1 } ); observer.observe(el); return () => observer.disconnect(); }, []); useEffect(() => { if (!entered || startedAt !== null) return; const t0 = performance.now(); setStartedAt(t0); setElapsed(0); }, [entered, startedAt]); useEffect(() => { if (startedAt === null) return; if (elapsed >= total) return; const interval = window.setInterval(() => { setElapsed(performance.now() - startedAt); }, 40); return () => window.clearInterval(interval); }, [startedAt, elapsed, total]); const settled = startedAt !== null && elapsed >= total; const V = Math.max(0, elapsed - startDelayMs); const scrambling = startedAt !== null && !settled; if (settled || !entered) { return ( {normalized.map((p, i) => p.kind === "text" ? ( {entered ? p.value : ""} ) : ( {entered ? p.settled : null} ) )} ); } // recompute random chars on each elapsed tick let index = 0; return ( {normalized.map((p, pi) => { if (p.kind === "text") { return ( {p.value.split("").map((ch, ci) => { const i = index++; // Original `reverse`: character delays run last→first. const order = reverse ? F - 1 - i : i; if (ch === "\n") return
; if (ch === " ") return ; const n = order * letterDelayMs; if (V < n) { return ( {ch} ); } if (V < n + scrambleWindow) { const phase = Math.min(1, Math.floor((V - n) / phaseLen)); const color = scrambleColors ? COLORS_LIGHT[phase] : undefined; return ( {randomChar()} ); } return {ch}; })}
); } return ( {p.scramble.split("").map((ch, ci) => { const i = index++; const order = reverse ? F - 1 - i : i; const n = order * letterDelayMs; if (V < n) { return ( {ch} ); } if (V < n + scrambleWindow) { const phase = Math.min(1, Math.floor((V - n) / phaseLen)); const color = scrambleColors ? COLORS_LIGHT[phase] : undefined; return ( {randomChar()} ); } return {ch}; })} ); })}
); }