{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"grid-thumbnail-grows-into-the-detail-photo","type":"registry:block","title":"Grid Thumbnail Grows Into the Detail Photo","description":"","dependencies":["framer-motion"],"files":[{"path":"components/we-inc/grid-thumbnail-grows-into-the-detail-photo.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: --w6mph-bg, --w6mph-ink, --w6mph-clay (accent), the 3 / 2 aspect-ratio\n     (keep card photo and detail photo the same shape so the morph never stretches) and MS in the script. */\n  .w6mph { --w6mph-bg: #f4e9df; --w6mph-ink: #2e1d14; --w6mph-clay: #a2441f;\n    height: 640px; display: flex; flex-direction: column; background: var(--w6mph-bg); color: var(--w6mph-ink);\n    font-family: ui-sans-serif, system-ui, -apple-system, \"Segoe UI\", Roboto, sans-serif; overflow: hidden; }\n  .w6mph *, .w6mph *::before { box-sizing: border-box; }\n  .w6mph-top { display: flex; flex-wrap: wrap; justify-content: space-between; align-items: baseline; gap: 4px 20px; padding: 20px clamp(16px, 4.5vw, 52px) 14px; }\n  .w6mph-brand { margin: 0; font-size: 1.0625rem; font-weight: 700; letter-spacing: -.01em; }\n  .w6mph-brand span { font-weight: 400; color: var(--w6mph-clay); }\n  .w6mph-note { margin: 0; font-size: .8125rem; color: #6a4b3a; }\n  .w6mph-stage { flex: 1; min-height: 0; overflow-y: auto; overflow-x: hidden; padding: 8px clamp(16px, 4.5vw, 52px) 28px; }\n  .w6mph-view[hidden] { display: none; }\n  .w6mph h2 { margin: 0; font-family: Georgia, \"Iowan Old Style\", serif; font-weight: 400; font-size: clamp(1.7rem, 3.6vw, 2.5rem); line-height: 1.1; letter-spacing: -.015em; }\n  .w6mph h2:focus { outline: none; }\n  .w6mph h2:focus-visible { outline: 2px solid var(--w6mph-clay); outline-offset: 4px; }\n  .w6mph-intro { margin: 10px 0 0; max-width: 34rem; font-size: 1rem; line-height: 1.55; color: #5a3f30; }\n  .w6mph-cards { margin: 24px 0 0; padding: 0; list-style: none; display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 18px 14px; }\n  .w6mph-cards a { display: grid; gap: 2px; color: inherit; text-decoration: none; border-radius: 12px; }\n  .w6mph-cards a:focus-visible { outline: 2px solid var(--w6mph-clay); outline-offset: 4px; }\n  .w6mph img { display: block; width: 100%; height: auto; aspect-ratio: 3 / 2; object-fit: cover; border-radius: 10px; background: #e3cfbf; }\n  .w6mph-cards img { margin-bottom: 8px; box-shadow: 0 1px 2px rgba(46,29,20,.12); transition: transform .4s cubic-bezier(.2,.7,.2,1); }\n  .w6mph-cards a:hover img, .w6mph-cards a:focus-visible img { transform: translateY(-3px); }\n  .w6mph-cards b { font-size: .9375rem; font-weight: 600; }\n  .w6mph-cards span { font-size: .875rem; color: #6a4b3a; }\n  .w6mph-foot { margin: 26px 0 0; padding-top: 14px; border-top: 1px solid #e0c9b8; font-size: .875rem; color: #6a4b3a; }\n  .w6mph-crumbs { display: flex; justify-content: space-between; gap: 16px; margin: 0 0 10px; }\n  .w6mph-crumbs a { display: inline-flex; align-items: center; min-height: 40px; color: var(--w6mph-clay); font-size: .9375rem; font-weight: 600; text-underline-offset: 5px; border-radius: 6px; }\n  .w6mph-crumbs a:first-child::before { content: \"←\"; margin-right: 8px; }\n  .w6mph-crumbs a:last-child::after { content: \"→\"; margin-left: 8px; }\n  .w6mph-crumbs a:focus-visible, .w6mph-btn:focus-visible { outline: 2px solid var(--w6mph-ink); outline-offset: 3px; }\n  .w6mph-split { display: grid; gap: 20px; }\n  .w6mph-hero { box-shadow: 0 18px 40px -24px rgba(46,29,20,.55); }\n  .w6mph-price { margin: 8px 0 0; font-size: 1.0625rem; color: var(--w6mph-clay); font-weight: 600; }\n  .w6mph-desc { margin: 12px 0 0; font-size: 1rem; line-height: 1.6; color: #4d3528; max-width: 30rem; }\n  .w6mph-facts { margin: 16px 0 0; display: grid; gap: 0; border-top: 1px solid #e0c9b8; }\n  .w6mph-facts div { display: grid; grid-template-columns: 5rem 1fr; gap: 12px; padding: 9px 0; border-bottom: 1px solid #e0c9b8; font-size: .9rem; }\n  .w6mph-facts dt { color: #6a4b3a; }\n  .w6mph-facts dd { margin: 0; }\n  .w6mph-btn { margin-top: 20px; min-height: 46px; padding: 0 22px; border: 0; border-radius: 999px; background: var(--w6mph-clay); color: #fff;\n    font: inherit; font-size: .9375rem; font-weight: 600; cursor: pointer; }\n  .w6mph-btn:hover { background: #87381a; }\n  @media (min-width: 900px) {\n    .w6mph-cards { grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 20px; }\n    .w6mph-split { grid-template-columns: minmax(0, 1.25fr) minmax(0, 1fr); gap: 40px; align-items: start; }\n  }\n\n  /* The morph: the clicked photo carries the name w6mph-photo through the swap. The script moves the group with\n     transform only (same shape before and after, one even scale), so nothing animates width or height.\n     For separate pages, add  @view-transition { navigation: auto; }  and give the photo the same name on both pages. */\n  ::view-transition-group(w6mph-photo) { animation: none; transform-origin: 0 0; }\n  ::view-transition-old(w6mph-photo) { animation: none; opacity: 0; }\n  ::view-transition-new(w6mph-photo) { animation: none; }\n  /* Fallback without view transitions: the text of the new view fades up while the photo is moved by the script. */\n  @keyframes w6mph-up { from { opacity: 0; transform: translateY(12px); } }\n  .w6mph[data-fb] .w6mph-view:not([hidden]) .w6mph-txt, .w6mph[data-fb] .w6mph-view:not([hidden]) .w6mph-intro { animation: w6mph-up .45s .1s cubic-bezier(.2,.7,.2,1) both; }\n  @media (prefers-reduced-motion: reduce) {\n    ::view-transition-group(w6mph-photo), ::view-transition-old(w6mph-photo), ::view-transition-new(w6mph-photo) { animation: none; }\n    .w6mph .w6mph-txt, .w6mph .w6mph-intro, .w6mph-cards img { animation: none !important; transition: none; }\n  }\n`;\n\nconst PIECES = [\n  { name: \"Hearth bowl\", price: \"£24 each\", src: \"https://images.pexels.com/photos/33703946/pexels-photo-33703946.jpeg?auto=compress&cs=tinysrgb&w=1000\", alt: \"Stacks of rustic clay bowls on workshop shelves\",\n    desc: \"Wide enough for soup, shallow enough for salad.\", size: \"15 cm across, 7 cm deep\", glaze: \"Bare clay outside, honey glaze inside\", care: \"Wash by hand\" },\n  { name: \"Dipped bud vase\", price: \"£36\", src: \"https://images.pexels.com/photos/8063882/pexels-photo-8063882.jpeg?auto=compress&cs=tinysrgb&w=1000\", alt: \"Ceramic vases dipped in grey, coral and white glaze on a wooden shelf\",\n    desc: \"Dipped twice by hand, so no two bands sit at the same height.\", size: \"18 cm tall\", glaze: \"Grey, coral or white over a raw foot\", care: \"Rinse by hand\" },\n  { name: \"Tidepool dish\", price: \"£22\", src: \"https://images.pexels.com/photos/28271468/pexels-photo-28271468.jpeg?auto=compress&cs=tinysrgb&w=1000\", alt: \"Small dishes with a deep teal glaze laid out on a white cloth\",\n    desc: \"For olives, salt or rings by the sink. The teal pools darker where the glaze runs thick.\", size: \"11 cm across\", glaze: \"Reactive teal with an iron edge\", care: \"Wash by hand\" },\n  { name: \"Bisque mug\", price: \"£18\", src: \"https://images.pexels.com/photos/8063803/pexels-photo-8063803.jpeg?auto=compress&cs=tinysrgb&w=1000\", alt: \"Rows of unglazed white mugs on a dusty studio shelf\",\n    desc: \"Fired once and waiting. Choose the glaze and we fire it again for collection in a week.\", size: \"300 ml\", glaze: \"Your choice of five\", care: \"Ready in seven days\" },\n];\nconst MS = 520; // length of the photo morph\nconst DEMO = [[700, 2], [1500, 3], [1500, 0], [2600, 4], [1500, 1], [1500, 0]]; // idle demo: [wait ms, view]; [] turns it off\nconst NAME = \"w6mph-photo\";\nconst useIso = typeof window !== \"undefined\" ? useLayoutEffect : useEffect;\n\nexport default function GridThumbnailGrowsIntoTheDetailPhoto() {\n  const reduce = useReducedMotion();\n  const [cur, setCur] = useState(0); // 0 is the grid, 1 to 4 are the pieces\n  const [fb, setFb] = useState(false);\n  const rootRef = useRef<HTMLDivElement>(null);\n  const stageRef = useRef<HTMLDivElement>(null);\n  const cardRefs = useRef<(HTMLAnchorElement | null)[]>([]);\n  const heroRefs = useRef<(HTMLImageElement | null)[]>([]);\n  const headRefs = useRef<(HTMLHeadingElement | null)[]>([]);\n  const st = useRef({ cur: 0, busy: false, auto: DEMO.length > 0 });\n  const afterCommit = useRef<null | (() => void)>(null);\n\n  // Runs once React has written the new view, inside the view transition callback when there is one.\n  useIso(() => { const f = afterCommit.current; afterCommit.current = null; f?.(); }, [cur]);\n\n  const photoOf = (v: number, other: number) => (v ? heroRefs.current[v - 1] : cardRefs.current[other - 1]?.querySelector(\"img\")) as HTMLElement;\n\n  const go = (i: number, person: boolean) => {\n    const s = st.current;\n    if (s.busy || i === s.cur) return;\n    const was = s.cur, from = photoOf(was, i), morph = !was || !i;\n    s.cur = i;\n    const landed = () => {\n      if (stageRef.current) stageRef.current.scrollTop = 0;\n      if (person) (i ? headRefs.current[i - 1] : cardRefs.current[was - 1])?.focus({ preventScroll: true });\n      return photoOf(i, was);\n    };\n    if (reduce || matchMedia(\"(prefers-reduced-motion: reduce)\").matches) { afterCommit.current = landed; setCur(i); return; }\n    const a = from.getBoundingClientRect();\n    // Moves the photo from where it was (a) to where it lands (b) with one even scale: transform only.\n    const move = (to: HTMLElement, pseudo?: string) => {\n      const b = to.getBoundingClientRect(), k = ` scale(${a.width / b.width})`;\n      const frames = pseudo\n        ? [`translate(${a.left}px,${a.top}px)${k}`, `translate(${b.left}px,${b.top}px)`]\n        : [`translate(${a.left - b.left}px,${a.top - b.top}px)${k}`, \"none\"];\n      (pseudo ? document.documentElement : to).animate({ transform: frames }, { duration: MS, easing: \"cubic-bezier(.2,.75,.2,1)\", pseudoElement: pseudo } as KeyframeAnimationOptions);\n    };\n    s.busy = true;\n    const doc = document as any;\n    if (doc.startViewTransition) {\n      let to: HTMLElement | null = null;\n      if (morph) from.style.setProperty(\"view-transition-name\", NAME);\n      const t = doc.startViewTransition(() => new Promise<void>((done) => {\n        afterCommit.current = () => {\n          from.style.removeProperty(\"view-transition-name\");\n          to = landed();\n          if (morph) to.style.setProperty(\"view-transition-name\", NAME);\n          done();\n        };\n        setCur(i);\n      }));\n      if (morph) t.ready.then(() => to && move(to, `::view-transition-group(${NAME})`));\n      t.finished.finally(() => { to?.style.removeProperty(\"view-transition-name\"); s.busy = false; });\n    } else {\n      setFb(true);\n      afterCommit.current = () => { const to = landed(); if (morph) { to.style.transformOrigin = \"0 0\"; move(to); } };\n      setCur(i);\n      window.setTimeout(() => { s.busy = false; }, MS);\n    }\n  };\n  const goRef = useRef(go);\n  goRef.current = go;\n\n  // Idle demo: open a piece, step to the next one, go back, then repeat with other cards.\n  useEffect(() => {\n    const root = rootRef.current;\n    if (!root || reduce || !DEMO.length) return;\n    let timer = 0, step = 0, seen = false;\n    const tick = () => {\n      timer = 0;\n      if (!st.current.auto || !seen) return;\n      goRef.current(DEMO[step][1], false);\n      step = (step + 1) % DEMO.length;\n      timer = window.setTimeout(tick, DEMO[step][0]);\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[step][0]); };\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=\"w6mph\" ref={rootRef} data-fb={fb ? \"\" : undefined}>\n      <style>{STYLES}</style>\n      <header className=\"w6mph-top\">\n        <p className=\"w6mph-brand\">Ochre Lane <span>Pottery</span></p>\n        <p className=\"w6mph-note\">Studio sale, this weekend only</p>\n      </header>\n      <div className=\"w6mph-stage\" ref={stageRef}>\n        <section className=\"w6mph-view w6mph-grid\" aria-labelledby=\"w6mph-h0\" hidden={cur !== 0}>\n          <h2 id=\"w6mph-h0\" tabIndex={-1}>From this week's firing</h2>\n          <p className=\"w6mph-intro\">Four small batches out of the kiln on Thursday. Open a piece to see its glaze, size and how to look after it.</p>\n          <ul className=\"w6mph-cards\">\n            {PIECES.map((c, k) => (\n              <li key={c.name}>\n                <a href=\"#\" ref={(el) => { cardRefs.current[k] = el; }} onClick={link(k + 1)}>\n                  <img src={c.src} alt={c.alt} width={600} height={400} />\n                  <b>{c.name}</b><span>{c.price}</span>\n                </a>\n              </li>\n            ))}\n          </ul>\n          <p className=\"w6mph-foot\">Collect from the studio door on Saturday, 10am to 4pm, or we can post your order.</p>\n        </section>\n        {PIECES.map((p, k) => (\n          <section key={p.name} className=\"w6mph-view\" aria-labelledby={`w6mph-h${k + 1}`} hidden={cur !== k + 1}>\n            <p className=\"w6mph-crumbs\">\n              <a href=\"#\" onClick={link(0)}>All pieces</a>\n              <a href=\"#\" onClick={link(((k + 1) % PIECES.length) + 1)}>Next piece</a>\n            </p>\n            <div className=\"w6mph-split\">\n              <img className=\"w6mph-hero\" ref={(el) => { heroRefs.current[k] = el; }} src={p.src} alt={p.alt} width={600} height={400} />\n              <div className=\"w6mph-txt\">\n                <h2 id={`w6mph-h${k + 1}`} tabIndex={-1} ref={(el) => { headRefs.current[k] = el; }}>{p.name}</h2>\n                <p className=\"w6mph-price\">{p.price}</p>\n                <p className=\"w6mph-desc\">{p.desc}</p>\n                <dl className=\"w6mph-facts\">\n                  <div><dt>Size</dt><dd>{p.size}</dd></div>\n                  <div><dt>Glaze</dt><dd>{p.glaze}</dd></div>\n                  <div><dt>Care</dt><dd>{p.care}</dd></div>\n                </dl>\n                <button type=\"button\" className=\"w6mph-btn\">Reserve this piece</button>\n              </div>\n            </div>\n          </section>\n        ))}\n      </div>\n    </div>\n  );\n}\n"}]}
