{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"sibling-pages-slide-in-reading-direction","type":"registry:block","title":"Sibling Pages Slide in Reading Direction","description":"","dependencies":["framer-motion"],"files":[{"path":"components/we-inc/sibling-pages-slide-in-reading-direction.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: --w6sld-paper (background), --w6sld-ink (text), --w6sld-red (accent),\n     the 22% and 12% in the w6sld keyframes (how far pages slide) and the .26s and .5s durations. */\n  .w6sld { --w6sld-paper: #f4eee3; --w6sld-ink: #2b211a; --w6sld-red: #8c2f24;\n    height: 600px; display: flex; flex-direction: column; box-sizing: border-box; background: var(--w6sld-paper); color: var(--w6sld-ink);\n    font-family: ui-sans-serif, system-ui, -apple-system, \"Segoe UI\", Roboto, sans-serif; overflow: hidden; }\n  .w6sld *, .w6sld *::before { box-sizing: border-box; }\n  .w6sld-top { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 8px 24px; padding: 20px clamp(16px, 5vw, 56px);\n    border-bottom: 1px solid #ddd0bd; }\n  .w6sld-brand { margin: 0; font-family: Georgia, \"Iowan Old Style\", serif; font-size: 1.25rem; letter-spacing: -.01em; }\n  .w6sld-brand span { font-style: italic; color: var(--w6sld-red); }\n  .w6sld-brand small { margin-left: 6px; font-family: ui-sans-serif, system-ui, sans-serif; font-size: .7rem; letter-spacing: .16em; text-transform: uppercase; color: #6b5a4b; }\n  .w6sld-nav { display: flex; gap: 2px; margin: 0 -10px; padding: 0; list-style: none; }\n  .w6sld a { color: inherit; }\n  .w6sld-nav a { display: inline-flex; align-items: center; min-height: 40px; padding: 0 10px; font-size: .9375rem; text-decoration: none; border-radius: 6px;\n    text-underline-offset: 6px; }\n  .w6sld-nav a:hover { text-decoration: underline; }\n  .w6sld-nav a[aria-current=\"page\"] { color: var(--w6sld-red); text-decoration: underline; text-decoration-thickness: 2px; }\n  .w6sld a:focus-visible, .w6sld h2:focus-visible { outline: 2px solid var(--w6sld-red); outline-offset: 3px; }\n  .w6sld h2:focus { outline: none; }\n  .w6sld-stage { position: relative; flex: 1; min-height: 0; overflow-x: hidden; overflow-y: auto; }\n  .w6sld-view { min-height: 100%; display: grid; grid-template-columns: minmax(0, 1fr); align-content: start; gap: 18px;\n    padding: 28px clamp(16px, 5vw, 56px) 24px; }\n  .w6sld-view[hidden] { display: none; }\n  .w6sld-num { margin: 0; font-family: Georgia, serif; font-style: italic; font-size: 2.5rem; line-height: 1; color: var(--w6sld-red); }\n  .w6sld-eye { margin: 0; font-size: .75rem; font-weight: 700; letter-spacing: .18em; text-transform: uppercase; color: #6b5a4b; }\n  .w6sld h2 { margin: 8px 0 0; max-width: 18ch; font-family: Georgia, \"Iowan Old Style\", serif; font-weight: 400; font-size: clamp(1.75rem, 4.2vw, 2.75rem);\n    line-height: 1.08; letter-spacing: -.02em; }\n  .w6sld-lead { margin: 14px 0 0; max-width: 36rem; font-size: 1.0625rem; line-height: 1.6; color: #4a3b2f; }\n  .w6sld-list { margin: 20px 0 0; padding: 0; list-style: none; max-width: 36rem; border-top: 1px solid #ddd0bd; }\n  .w6sld-list li { display: flex; flex-wrap: wrap; justify-content: space-between; gap: 2px 16px; padding: 11px 0; border-bottom: 1px solid #ddd0bd; font-size: .9375rem; }\n  .w6sld-list b { font-weight: 600; }\n  .w6sld-list span { color: #5c4b3d; }\n  @media (max-width: 520px) { .w6sld-list li { flex-direction: column; } }\n  .w6sld-pager { display: flex; flex-wrap: wrap; gap: 8px 28px; margin: 4px 0 0; font-size: .9375rem; }\n  .w6sld-pager a { display: inline-flex; align-items: center; min-height: 40px; font-weight: 600; text-underline-offset: 5px; }\n  .w6sld-pager a:last-child:not(:first-child) { margin-left: auto; }\n  @media (min-width: 860px) {\n    .w6sld-view { grid-template-columns: minmax(0, 1fr) 15rem; column-gap: 48px; padding-top: 40px; }\n    .w6sld-num { grid-column: 2; grid-row: 1; justify-self: end; font-size: 9rem; letter-spacing: -.04em; opacity: .9; }\n    .w6sld-body { grid-column: 1; grid-row: 1; }\n    .w6sld-pager { grid-column: 1 / -1; }\n  }\n\n  /* The slide. Only the stage is named, and only while a transition runs, so the header stays put.\n     For a multi-page site add  @view-transition { navigation: auto; }  and give the main element the name on every page. */\n  ::view-transition-group(w6sld-stage) { animation: none; overflow: clip; }\n  ::view-transition-old(w6sld-stage), ::view-transition-new(w6sld-stage) { mix-blend-mode: normal; }\n  /* The old page leaves quickly, the new one follows a beat later, so the two never sit on top of each other for long. */\n  ::view-transition-old(w6sld-stage) { animation: w6sld-out .26s cubic-bezier(.2, 0, .2, 1) both; }\n  ::view-transition-new(w6sld-stage) { animation: w6sld-in .5s .1s cubic-bezier(.2, .7, .2, 1) both; }\n  :root[data-w6sld-dir=\"b\"]::view-transition-old(w6sld-stage) { animation-name: w6sld-out-b; }\n  :root[data-w6sld-dir=\"b\"]::view-transition-new(w6sld-stage) { animation-name: w6sld-in-b; }\n  @keyframes w6sld-out { to { opacity: 0; transform: translateX(-12%); } }\n  @keyframes w6sld-in { from { opacity: 0; transform: translateX(22%); } }\n  @keyframes w6sld-out-b { to { opacity: 0; transform: translateX(12%); } }\n  @keyframes w6sld-in-b { from { opacity: 0; transform: translateX(-22%); } }\n  /* Fallback for browsers without view transitions: the new page slides in on its own. */\n  .w6sld[data-fb=\"f\"] .w6sld-view:not([hidden]) { animation: w6sld-in .5s cubic-bezier(.2, .7, .2, 1) both; }\n  .w6sld[data-fb=\"b\"] .w6sld-view:not([hidden]) { animation: w6sld-in-b .5s cubic-bezier(.2, .7, .2, 1) both; }\n  @media (prefers-reduced-motion: reduce) {\n    ::view-transition-group(w6sld-stage), ::view-transition-old(w6sld-stage), ::view-transition-new(w6sld-stage) { animation: none; }\n    .w6sld .w6sld-view { animation: none !important; }\n  }\n`;\n\nconst PAGES = [\n  {\n    nav: \"Shop\", eye: \"The shop\", title: \"Two rooms of books above the old print works\",\n    lead: \"New fiction, poetry and local history at the front, and a back room of secondhand finds sorted by whoever had the afternoon free.\",\n    rows: [[\"The Salt Year\", \"Ines Harrow, paperback\"], [\"Field Notes from a Flooded Valley\", \"Tom Ashdown, hardback\"], [\"Small Hours\", \"Mira Coyle, poems\"]],\n  },\n  {\n    nav: \"Events\", eye: \"Events\", title: \"Readings in the back room\",\n    lead: \"Thirty folding chairs, a kettle and a writer. Seats are free, but tell us you are coming so we set out enough cups.\",\n    rows: [[\"Thu 23 Oct, 7pm\", \"Poetry night with three local poets\"], [\"Sat 1 Nov, 11am\", \"Story hour for under sevens\"], [\"Wed 12 Nov, 7pm\", \"Tom Ashdown on rivers and memory\"]],\n  },\n  {\n    nav: \"Visit\", eye: \"Visit\", title: \"Up the stairs at 14 Tanner Row\",\n    lead: \"The door is the green one next to the bakery. There is a ramp at the side entrance and a bench by the poetry shelves.\",\n    rows: [[\"Tuesday to Friday\", \"10am to 6pm\"], [\"Saturday\", \"9am to 5pm\"], [\"Sunday and Monday\", \"Closed, except for readings\"]],\n  },\n  {\n    nav: \"Orders\", eye: \"Orders\", title: \"Ask for any book still in print\",\n    lead: \"Leave the title at the counter or by phone and we will call when it lands, usually within three working days.\",\n    rows: [[\"Phone\", \"01632 960 418\"], [\"Counter\", \"Any opening hour\"], [\"Collection\", \"Kept for two weeks under your name\"]],\n  },\n];\nconst useIso = typeof window !== \"undefined\" ? useLayoutEffect : useEffect;\nconst DEMO = [700, 1700, 3200]; // idle demo: ms before each automatic page turn; [] turns it off\n\nexport default function SiblingPagesSlideInReadingDirection() {\n  const reduce = useReducedMotion();\n  const [cur, setCur] = useState(0);\n  const [fb, setFb] = useState(\"\");\n  const rootRef = useRef<HTMLDivElement>(null);\n  const stageRef = useRef<HTMLDivElement>(null);\n  const heads = useRef<(HTMLHeadingElement | null)[]>([]);\n  const curRef = useRef(0);\n  const busy = useRef(false);\n  const auto = useRef(DEMO.length > 0);\n  const focusNext = useRef(false);\n  const commit = useRef<null | (() => void)>(null);\n\n  // Runs after React has written the new page: lets the view transition take its \"after\" snapshot, then moves focus.\n  useIso(() => {\n    commit.current?.();\n    commit.current = null;\n    if (focusNext.current) { focusNext.current = false; heads.current[cur]?.focus({ preventScroll: true }); }\n  }, [cur]);\n\n  const go = (i: number, person: boolean) => {\n    if (i === curRef.current || busy.current) return;\n    const dir = i > curRef.current ? \"f\" : \"b\";\n    curRef.current = i;\n    focusNext.current = person;\n    const doc = document as any;\n    if (reduce || matchMedia(\"(prefers-reduced-motion: reduce)\").matches) { setCur(i); return; }\n    if (doc.startViewTransition && stageRef.current) {\n      busy.current = true;\n      const stage = stageRef.current, html = document.documentElement;\n      stage.style.setProperty(\"view-transition-name\", \"w6sld-stage\");\n      html.setAttribute(\"data-w6sld-dir\", dir);\n      doc.startViewTransition(() => new Promise<void>((done) => { commit.current = done; setCur(i); })).finished.finally(() => {\n        stage.style.removeProperty(\"view-transition-name\"); html.removeAttribute(\"data-w6sld-dir\"); busy.current = false;\n      });\n    } else { setFb(dir); setCur(i); }\n  };\n  const goRef = useRef(go);\n  goRef.current = go;\n\n  // Idle demo while on screen and untouched.\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 (!auto.current || !seen) return;\n      goRef.current((curRef.current + 1) % PAGES.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 && auto.current && !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 = () => { auto.current = 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(); auto.current = false; go(i, true); };\n\n  return (\n    <div className=\"w6sld\" ref={rootRef} data-fb={fb || undefined}>\n      <style>{STYLES}</style>\n      <header className=\"w6sld-top\">\n        <p className=\"w6sld-brand\">Hollins <span>&amp;</span> Vale <small>Booksellers</small></p>\n        <nav aria-label=\"Bookshop pages\">\n          <ul className=\"w6sld-nav\">\n            {PAGES.map((p, i) => (\n              <li key={p.nav}><a href=\"#\" onClick={link(i)} aria-current={i === cur ? \"page\" : undefined}>{p.nav}</a></li>\n            ))}\n          </ul>\n        </nav>\n      </header>\n      <div className=\"w6sld-stage\" ref={stageRef}>\n        {PAGES.map((p, i) => (\n          <section key={p.nav} className=\"w6sld-view\" aria-labelledby={`w6sld-h${i}`} hidden={i !== cur}>\n            <p className=\"w6sld-num\" aria-hidden=\"true\">{String(i + 1).padStart(2, \"0\")}</p>\n            <div className=\"w6sld-body\">\n              <p className=\"w6sld-eye\">{p.eye}</p>\n              <h2 id={`w6sld-h${i}`} tabIndex={-1} ref={(el) => { heads.current[i] = el; }}>{p.title}</h2>\n              <p className=\"w6sld-lead\">{p.lead}</p>\n              <ul className=\"w6sld-list\">\n                {p.rows.map(([a, b]) => <li key={a}><b>{a}</b><span>{b}</span></li>)}\n              </ul>\n            </div>\n            <p className=\"w6sld-pager\">\n              {i > 0 && <a href=\"#\" onClick={link(i - 1)}>Previous: {PAGES[i - 1].nav}</a>}\n              {i < PAGES.length - 1 && <a href=\"#\" onClick={link(i + 1)}>Next: {PAGES[i + 1].nav}</a>}\n            </p>\n          </section>\n        ))}\n      </div>\n    </div>\n  );\n}\n"}]}
