{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"ferry-departure-board-rows-scramble-into-place","type":"registry:block","title":"Ferry Departure Board Rows Scramble Into Place","description":"","dependencies":[],"files":[{"path":"components/we-inc/ferry-departure-board-rows-scramble-into-place.tsx","type":"registry:component","content":"import { useEffect, useRef } from \"react\";\n\nconst SAILINGS = [\n  [\"09:10\", \"Skerrow Isle\", \"Boarding\"],\n  [\"09:45\", \"Port Annet\", \"On time\"],\n  [\"10:20\", \"Holm Bay\", \"On time\"],\n  [\"11:05\", \"Calder Quay\", \"Delayed\"],\n  [\"11:40\", \"Linn Point\", \"On time\"],\n  [\"12:15\", \"Easter Ness\", \"On time\"],\n];\nconst WIDTHS = [5, 12, 8]; // letter tiles per column\nconst ROWS = 4;\nconst GLYPHS = \"ABCDEFGHKLMNPRSTVWXYZ0123456789\";\nconst STEP = 40; // ms between one tile settling and the next\nconst LEAD = 220; // ms of noise before the first tile settles\nconst ADVANCE = 6000; // ms between automatic board updates until someone presses the button\n\ntype Field = { sr: HTMLElement; cells: HTMLElement[]; f: number };\n\nexport default function FerryDepartureBoardRowsScrambleIntoPlace() {\n  const root = useRef<HTMLElement>(null);\n  const advance = useRef<() => void>(() => {});\n\n  useEffect(() => {\n    const el = root.current;\n    if (!el) return;\n    const reduce = window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches;\n    const step = reduce ? 0 : STEP;\n    const lead = reduce ? 0 : LEAD;\n    const rows = Array.from(el.querySelectorAll(\"tbody tr\"));\n    const fields: Field[] = [];\n    rows.forEach((tr) =>\n      tr.querySelectorAll(\"td\").forEach((td) =>\n        fields.push({ sr: td.firstElementChild as HTMLElement, cells: Array.from(td.querySelectorAll<HTMLElement>(\".w3dbd-tiles span\")), f: 0 }),\n      ),\n    );\n    let k = 0;\n    let auto = 0;\n    let live = false;\n    let seen = false;\n    const set = (fd: Field, text: string, delay: number) => {\n      const to = (text + \"            \").slice(0, fd.cells.length).toUpperCase();\n      const t0 = performance.now() + delay;\n      fd.sr.textContent = text;\n      cancelAnimationFrame(fd.f);\n      const frame = (now: number) => {\n        const t = now - t0;\n        let busy = false;\n        fd.cells.forEach((c, i) => {\n          if (c.textContent === to[i] && !c.hasAttribute(\"data-on\")) return;\n          if (t > lead + i * step) { c.textContent = to[i]; c.removeAttribute(\"data-on\"); }\n          else if (t > 0) { busy = true; c.setAttribute(\"data-on\", \"\"); c.textContent = GLYPHS[(Math.random() * GLYPHS.length) | 0]; }\n          else busy = true;\n        });\n        fd.f = busy ? requestAnimationFrame(frame) : 0;\n      };\n      frame(t0);\n    };\n    const show = () =>\n      fields.forEach((fd, n) => {\n        const row = (n / 3) | 0;\n        set(fd, SAILINGS[(k + row) % SAILINGS.length][n % 3], row * 120 + (n % 3) * 60);\n      });\n    const next = () => { k = (k + 1) % SAILINGS.length; show(); };\n    advance.current = () => { live = true; clearInterval(auto); next(); };\n    const offs = rows.map((tr, row) => {\n      const flip = () => {\n        if (reduce) return;\n        fields.slice(row * 3, row * 3 + 3).forEach((fd) => {\n          fd.cells.forEach((c) => { c.textContent = \"\"; });\n          set(fd, fd.sr.textContent || \"\", 0);\n        });\n      };\n      tr.addEventListener(\"pointerenter\", flip);\n      return () => tr.removeEventListener(\"pointerenter\", flip);\n    });\n    const io = new IntersectionObserver(([e]) => {\n      clearInterval(auto);\n      if (!e.isIntersecting) return;\n      if (!seen) { seen = true; show(); }\n      if (!live && !reduce) auto = window.setInterval(next, ADVANCE);\n    });\n    io.observe(el);\n    return () => {\n      io.disconnect();\n      clearInterval(auto);\n      offs.forEach((f) => f());\n      fields.forEach((fd) => cancelAnimationFrame(fd.f));\n    };\n  }, []);\n\n  return (\n    <section ref={root} className=\"w3dbd bg-[#0e1a2b] px-5 py-[72px] text-[#e8edf4]\">\n      <style>{`\n        .w3dbd { background: radial-gradient(120% 80% at 50% 0%, #17283f, #0e1a2b 70%); }\n        .w3dbd-board { font-size: clamp(13px, 2.1vw, 19px); box-shadow: inset 0 1px 0 rgba(255,255,255,.05), 0 24px 48px -24px rgba(0,0,0,.7); }\n        .w3dbd td + td, .w3dbd th + th { padding-left: .9em; }\n        @media (min-width: 641px) { .w3dbd td:not(:last-child), .w3dbd th:not(:last-child) { width: 1%; } }\n        .w3dbd-tiles span { width: 1.3em; height: 1.75em; display: flex; align-items: center; justify-content: center; border-radius: 3px;\n          background: linear-gradient(#22344f, #1a2a42 49%, #0c1626 50%, #1a2a42 52%); color: #f3e6c4;\n          font: 700 1em/1 ui-monospace, \"SF Mono\", Menlo, Consolas, monospace; }\n        .w3dbd-tiles span[data-on] { color: #9fb2cc; }\n        .w3dbd button:focus-visible { outline: 2px solid #e0b862; outline-offset: 3px; }\n        @media (max-width: 640px) {\n    .w3dbd-board { padding: 14px 12px; font-size: 14px; }\n    .w3dbd thead tr { display: none; }\n    .w3dbd tbody tr { display: grid; grid-template-columns: auto 1fr; column-gap: .9em; padding-bottom: 10px; }\n    .w3dbd td:nth-child(2) { grid-column: 1 / 3; grid-row: 1; padding-left: 0; }\n    .w3dbd td:last-child { padding-top: 0; }\n  }\n      `}</style>\n      <div className=\"mx-auto max-w-4xl\">\n        <div className=\"flex flex-wrap items-end justify-between gap-4\">\n          <div>\n            <p className=\"text-xs font-semibold uppercase tracking-[0.22em] text-[#e0b862]\">Harbour Line Ferries</p>\n            <h2 className=\"mt-2.5 text-[clamp(26px,3.6vw,36px)] font-semibold tracking-tight\">Departures from the north pier</h2>\n          </div>\n          <button\n            type=\"button\"\n            onClick={() => advance.current()}\n            className=\"min-h-[44px] rounded-full border border-[#3a4d6b] px-5 text-sm font-medium hover:bg-[#1a2a42]\"\n          >\n            Later sailings\n          </button>\n        </div>\n        <div className=\"w3dbd-board mt-7 rounded-[14px] bg-[#0a1422] p-[18px]\">\n          <table className=\"w-full border-collapse\">\n            <caption className=\"sr-only\">Next four sailings</caption>\n            <thead>\n              <tr>\n                {[\"Time\", \"Destination\", \"Status\"].map((h) => (\n                  <th key={h} scope=\"col\" className=\"pb-2.5 text-left text-[11px] font-semibold uppercase tracking-[0.18em] text-[#93a3bb]\">{h}</th>\n                ))}\n              </tr>\n            </thead>\n            <tbody>\n              {Array.from({ length: ROWS }, (_, r) => (\n                <tr key={r}>\n                  {WIDTHS.map((w, c) => (\n                    <td key={c} className=\"py-[5px] align-top\">\n                      <span className=\"sr-only\">{SAILINGS[r][c]}</span>\n                      <span className=\"w3dbd-tiles flex gap-[2px]\" aria-hidden=\"true\">\n                        {Array.from({ length: w }, (_, i) => <span key={i} />)}\n                      </span>\n                    </td>\n                  ))}\n                </tr>\n              ))}\n            </tbody>\n          </table>\n        </div>\n        <p className=\"mt-[22px] max-w-xl text-[15px] leading-relaxed text-[#a9b6c9]\">\n          Foot passengers board from gate A. Cars queue in lanes 1 to 4 from forty minutes before sailing.\n        </p>\n      </div>\n    </section>\n  );\n}\n"}]}
