{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"article-rows-cascade-out-and-in-between-sections","type":"registry:block","title":"Article Rows Cascade Out and In Between Sections","description":"","dependencies":["framer-motion"],"files":[{"path":"components/we-inc/article-rows-cascade-out-and-in-between-sections.tsx","type":"registry:component","content":"import { useEffect, useLayoutEffect, useRef, useState, type MouseEvent } from \"react\";\nimport { useReducedMotion } from \"framer-motion\";\n\n// Same styles as the plain HTML version; the comment at the top of STYLES names the values to change.\nconst STYLES = `\n  /* Change these: --w6stg-ink (background), --w6stg-paper (text), --w6stg-amber (accent), the 40ms delay steps\n     between rows further down, and the 28px travel in the w6stg keyframes. */\n  .w6stg { --w6stg-ink: #151412; --w6stg-paper: #eee7db; --w6stg-amber: #dba468;\n    height: 560px; display: flex; flex-direction: column; background: var(--w6stg-ink); color: var(--w6stg-paper);\n    font-family: ui-sans-serif, system-ui, -apple-system, \"Segoe UI\", Roboto, sans-serif; overflow: hidden; }\n  .w6stg *, .w6stg *::before { box-sizing: border-box; }\n  .w6stg-top { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 6px 24px; padding: 18px clamp(16px, 5vw, 56px); border-bottom: 1px solid #2c2a26; }\n  .w6stg-brand { margin: 0; font-family: Georgia, \"Iowan Old Style\", serif; font-size: 1.2rem; font-style: italic; }\n  .w6stg-brand span { font-family: ui-sans-serif, system-ui, sans-serif; font-style: normal; font-size: .7rem; letter-spacing: .2em; text-transform: uppercase; color: #a39b8e; margin-left: 4px; }\n  .w6stg-nav { display: flex; gap: 4px; margin: 0 -10px; padding: 0; list-style: none; }\n  .w6stg-nav a { display: inline-flex; align-items: center; min-height: 40px; padding: 0 10px; color: #b5ad9f; font-size: .9375rem; text-decoration: none; border-radius: 4px; }\n  .w6stg-nav a:hover { color: var(--w6stg-paper); }\n  .w6stg-nav a[aria-current=\"page\"] { color: var(--w6stg-amber); }\n  .w6stg a:focus-visible, .w6stg h2:focus-visible { outline: 2px solid var(--w6stg-amber); outline-offset: 3px; }\n  .w6stg h2:focus { outline: none; }\n  .w6stg-stage { flex: 1; min-height: 0; overflow-y: auto; overflow-x: hidden; padding: 26px clamp(16px, 5vw, 56px) 8px; }\n  .w6stg-view[hidden] { display: none; }\n  .w6stg-head { display: flex; flex-wrap: wrap; align-items: baseline; justify-content: space-between; gap: 4px 20px; }\n  .w6stg h2 { margin: 0; font-family: Georgia, \"Iowan Old Style\", serif; font-weight: 400; font-size: clamp(2rem, 5vw, 3.4rem); line-height: 1; letter-spacing: -.02em; }\n  .w6stg-head p { margin: 0; font-size: .875rem; color: #a39b8e; }\n  .w6stg-rows { margin: 18px 0 0; padding: 0; list-style: none; border-top: 1px solid #34312c; }\n  .w6stg-rows li { border-bottom: 1px solid #34312c; background: var(--w6stg-ink); }\n  .w6stg-rows a { display: grid; grid-template-columns: 2.25rem minmax(0, 1fr); gap: 2px 14px; padding: 15px 2px; color: inherit; text-decoration: none; border-radius: 4px; }\n  .w6stg-n { grid-row: span 2; padding-top: 4px; font-size: .75rem; color: var(--w6stg-amber); font-variant-numeric: tabular-nums; }\n  .w6stg-rows b { font-family: Georgia, serif; font-weight: 400; font-size: clamp(1.05rem, 2vw, 1.3rem); line-height: 1.3; }\n  .w6stg-rows small { font-size: .8125rem; color: #a39b8e; }\n  .w6stg-rows a:hover b { color: #fff; text-decoration: underline; text-decoration-color: var(--w6stg-amber); text-underline-offset: 4px; }\n  .w6stg-foot { margin: 0; padding: 14px clamp(16px, 5vw, 56px) 18px; border-top: 1px solid #2c2a26; font-size: .875rem; color: #b5ad9f; }\n  .w6stg-foot a { color: var(--w6stg-amber); text-underline-offset: 4px; }\n  @media (min-width: 860px) {\n    .w6stg-rows a { grid-template-columns: 3rem minmax(0, 1fr) auto; align-items: baseline; }\n    .w6stg-n { grid-row: auto; }\n  }\n\n  /* The heading and each row carry their own name during the change, so they can leave and arrive one after another.\n     Forward rows exit left and enter from the right; back reverses it. Delays step by 40ms per row.\n     Separate pages: add  @view-transition { navigation: auto; }  and the same names on every page. */\n  ::view-transition-group(w6stg-head), ::view-transition-group(w6stg-r1), ::view-transition-group(w6stg-r2),\n  ::view-transition-group(w6stg-r3), ::view-transition-group(w6stg-r4) { animation: none; }\n  ::view-transition-old(w6stg-head), ::view-transition-old(w6stg-r1), ::view-transition-old(w6stg-r2), ::view-transition-old(w6stg-r3), ::view-transition-old(w6stg-r4) {\n    mix-blend-mode: normal; animation: w6stg-out .2s cubic-bezier(.4, 0, 1, 1) both; }\n  ::view-transition-new(w6stg-head), ::view-transition-new(w6stg-r1), ::view-transition-new(w6stg-r2), ::view-transition-new(w6stg-r3), ::view-transition-new(w6stg-r4) {\n    mix-blend-mode: normal; animation: w6stg-in .4s .18s cubic-bezier(.2, .8, .2, 1) both; }\n  ::view-transition-old(w6stg-r1) { animation-delay: 40ms; } ::view-transition-new(w6stg-r1) { animation-delay: .22s; }\n  ::view-transition-old(w6stg-r2) { animation-delay: 80ms; } ::view-transition-new(w6stg-r2) { animation-delay: .26s; }\n  ::view-transition-old(w6stg-r3) { animation-delay: 120ms; } ::view-transition-new(w6stg-r3) { animation-delay: .3s; }\n  ::view-transition-old(w6stg-r4) { animation-delay: 160ms; } ::view-transition-new(w6stg-r4) { animation-delay: .34s; }\n  :root[data-w6stg-dir=\"b\"]::view-transition-old(w6stg-head), :root[data-w6stg-dir=\"b\"]::view-transition-old(w6stg-r1), :root[data-w6stg-dir=\"b\"]::view-transition-old(w6stg-r2),\n  :root[data-w6stg-dir=\"b\"]::view-transition-old(w6stg-r3), :root[data-w6stg-dir=\"b\"]::view-transition-old(w6stg-r4) { animation-name: w6stg-out-b; }\n  :root[data-w6stg-dir=\"b\"]::view-transition-new(w6stg-head), :root[data-w6stg-dir=\"b\"]::view-transition-new(w6stg-r1), :root[data-w6stg-dir=\"b\"]::view-transition-new(w6stg-r2),\n  :root[data-w6stg-dir=\"b\"]::view-transition-new(w6stg-r3), :root[data-w6stg-dir=\"b\"]::view-transition-new(w6stg-r4) { animation-name: w6stg-in-b; }\n  @keyframes w6stg-out { to { opacity: 0; transform: translateX(-28px); } }\n  @keyframes w6stg-in { from { opacity: 0; transform: translateX(28px); } }\n  @keyframes w6stg-out-b { to { opacity: 0; transform: translateX(28px); } }\n  @keyframes w6stg-in-b { from { opacity: 0; transform: translateX(-28px); } }\n  /* Fallback without view transitions: new rows arrive in the same cascade. */\n  .w6stg[data-fb] .w6stg-view:not([hidden]) .w6stg-head, .w6stg[data-fb] .w6stg-view:not([hidden]) li { animation: w6stg-in .46s cubic-bezier(.2, .8, .2, 1) both; }\n  .w6stg[data-fb] .w6stg-view:not([hidden]) li:nth-child(2) { animation-delay: 40ms; }\n  .w6stg[data-fb] .w6stg-view:not([hidden]) li:nth-child(3) { animation-delay: 80ms; }\n  .w6stg[data-fb] .w6stg-view:not([hidden]) li:nth-child(4) { animation-delay: 120ms; }\n  @media (prefers-reduced-motion: reduce) {\n    ::view-transition-old(w6stg-head), ::view-transition-new(w6stg-head), ::view-transition-old(w6stg-r1), ::view-transition-new(w6stg-r1),\n    ::view-transition-old(w6stg-r2), ::view-transition-new(w6stg-r2), ::view-transition-old(w6stg-r3), ::view-transition-new(w6stg-r3),\n    ::view-transition-old(w6stg-r4), ::view-transition-new(w6stg-r4) { animation: none; }\n    .w6stg .w6stg-head, .w6stg .w6stg-view li { animation: none !important; }\n  }\n`;\n\nconst SECTIONS = [\n  { name: \"Latest\", sub: \"Autumn issue, out now\", rows: [\n    [\"The year the river moved\", \"Hana Okafor\", \"14 min\"], [\"Notes on mending a dry stone wall\", \"Pete Garroway\", \"9 min\"],\n    [\"What the hedgerow remembers\", \"Lin Marsh\", \"11 min\"], [\"A field guide to October light\", \"Ruth Sallow\", \"6 min\"]] },\n  { name: \"Essays\", sub: \"Long reads from the back pages\", rows: [\n    [\"Against tidy gardens\", \"Owen Brack\", \"18 min\"], [\"The economics of a village shop\", \"Dee Annand\", \"16 min\"],\n    [\"On walking the same path for ten years\", \"Hana Okafor\", \"12 min\"], [\"Weather as a second language\", \"Cal Peary\", \"10 min\"]] },\n  { name: \"Interviews\", sub: \"Conversations at kitchen tables\", rows: [\n    [\"A shepherd on losing the hill farm\", \"with Morag Telfer\", \"21 min\"], [\"The last thatcher in the valley\", \"with Ivo Penhale\", \"15 min\"],\n    [\"Running a ferry with one engine\", \"with Sue Rendle\", \"13 min\"], [\"Bees, bookkeeping and patience\", \"with Amit Dhar\", \"17 min\"]] },\n];\nconst DEMO = [700, 1800, 3000]; // idle demo: ms before each automatic section change; [] turns it off\nconst useIso = typeof window !== \"undefined\" ? useLayoutEffect : useEffect;\n\n// Gives (or takes away) the heading and rows of a view their view-transition-name from data-vt.\nconst tag = (v: Element | null, on: boolean) => v?.querySelectorAll<HTMLElement>(\"[data-vt]\").forEach((n) => {\n  if (on) n.style.setProperty(\"view-transition-name\", n.dataset.vt!); else n.style.removeProperty(\"view-transition-name\");\n});\n\nexport default function ArticleRowsCascadeOutAndInBetweenSections() {\n  const reduce = useReducedMotion();\n  const [cur, setCur] = useState(0);\n  const [fb, setFb] = useState(false);\n  const rootRef = useRef<HTMLDivElement>(null);\n  const stageRef = useRef<HTMLDivElement>(null);\n  const views = useRef<(HTMLElement | null)[]>([]);\n  const heads = useRef<(HTMLHeadingElement | null)[]>([]);\n  const st = useRef({ cur: 0, busy: false, auto: DEMO.length > 0 });\n  const afterCommit = useRef<null | (() => void)>(null);\n\n  useIso(() => { const f = afterCommit.current; afterCommit.current = null; f?.(); }, [cur]);\n\n  const go = (i: number, person: boolean) => {\n    const s = st.current;\n    if (i === s.cur || s.busy) return;\n    const dir = i > s.cur ? \"f\" : \"b\", was = views.current[s.cur];\n    s.cur = i;\n    const landed = () => {\n      if (stageRef.current) stageRef.current.scrollTop = 0;\n      if (person) heads.current[i]?.focus({ preventScroll: true });\n    };\n    const doc = document as any;\n    if (reduce || matchMedia(\"(prefers-reduced-motion: reduce)\").matches) { afterCommit.current = landed; setCur(i); return; }\n    if (doc.startViewTransition) {\n      s.busy = true;\n      tag(was, true);\n      const html = document.documentElement;\n      html.setAttribute(\"data-w6stg-dir\", dir);\n      doc.startViewTransition(() => new Promise<void>((done) => {\n        afterCommit.current = () => { tag(was, false); tag(views.current[i], true); landed(); done(); };\n        setCur(i);\n      })).finished.finally(() => { tag(views.current[i], false); html.removeAttribute(\"data-w6stg-dir\"); s.busy = false; });\n    } else { afterCommit.current = landed; setFb(true); setCur(i); }\n  };\n  const goRef = useRef(go);\n  goRef.current = go;\n\n  // Idle demo: moves through the sections while the piece is on screen and nobody has used it.\n  useEffect(() => {\n    const root = rootRef.current;\n    if (!root || reduce || !DEMO.length) return;\n    let timer = 0, turns = 0, seen = false;\n    const tick = () => {\n      timer = 0;\n      if (!st.current.auto || !seen) return;\n      goRef.current((st.current.cur + 1) % SECTIONS.length, false);\n      turns++;\n      timer = window.setTimeout(tick, DEMO[Math.min(turns, DEMO.length - 1)]);\n    };\n    // The demo waits for the page to finish loading, so the first thing anyone sees is the resting view.\n    const kick = () => { if (seen && st.current.auto && !timer && document.readyState === \"complete\") timer = window.setTimeout(tick, DEMO[Math.min(turns, DEMO.length - 1)]); };\n    window.addEventListener(\"load\", kick);\n    const io = new IntersectionObserver((e) => {\n      seen = e[0].isIntersecting;\n      if (seen) kick(); else { clearTimeout(timer); timer = 0; }\n    });\n    io.observe(root);\n    const stop = () => { st.current.auto = false; };\n    root.addEventListener(\"pointerdown\", stop);\n    root.addEventListener(\"keydown\", stop);\n    return () => { io.disconnect(); clearTimeout(timer); window.removeEventListener(\"load\", kick); root.removeEventListener(\"pointerdown\", stop); root.removeEventListener(\"keydown\", stop); };\n  }, [reduce]);\n\n  const link = (i: number) => (e: MouseEvent) => { e.preventDefault(); st.current.auto = false; go(i, true); };\n\n  return (\n    <div className=\"w6stg\" ref={rootRef} data-fb={fb ? \"\" : undefined}>\n      <style>{STYLES}</style>\n      <header className=\"w6stg-top\">\n        <p className=\"w6stg-brand\">The Long Field <span>Journal</span></p>\n        <nav aria-label=\"Journal sections\">\n          <ul className=\"w6stg-nav\">\n            {SECTIONS.map((s, i) => (\n              <li key={s.name}><a href=\"#\" onClick={link(i)} aria-current={i === cur ? \"page\" : undefined}>{s.name}</a></li>\n            ))}\n          </ul>\n        </nav>\n      </header>\n      <div className=\"w6stg-stage\" ref={stageRef}>\n        {SECTIONS.map((s, i) => (\n          <section key={s.name} className=\"w6stg-view\" aria-labelledby={`w6stg-h${i}`} hidden={i !== cur} ref={(el) => { views.current[i] = el; }}>\n            <div className=\"w6stg-head\" data-vt=\"w6stg-head\">\n              <h2 id={`w6stg-h${i}`} tabIndex={-1} ref={(el) => { heads.current[i] = el; }}>{s.name}</h2>\n              <p>{s.sub}</p>\n            </div>\n            <ol className=\"w6stg-rows\">\n              {s.rows.map(([t, a, m], k) => (\n                <li key={t} data-vt={`w6stg-r${k + 1}`}>\n                  <a href=\"#\"><span className=\"w6stg-n\" aria-hidden=\"true\">{String(k + 1).padStart(2, \"0\")}</span><b>{t}</b><small>{a} · {m} read</small></a>\n                </li>\n              ))}\n            </ol>\n          </section>\n        ))}\n      </div>\n      <p className=\"w6stg-foot\">Printed four times a year on uncoated paper. <a href=\"#\">Subscribe for £36</a></p>\n    </div>\n  );\n}\n"}]}
