{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"paper-menu-with-a-photo-preview-that-follows-focus","type":"registry:block","title":"Paper Menu With a Photo Preview That Follows Focus","description":"","dependencies":[],"files":[{"path":"components/we-inc/paper-menu-with-a-photo-preview-that-follows-focus.tsx","type":"registry:component","content":"import { useEffect, useRef, type CSSProperties } from \"react\";\n\n// Same styles as the plain HTML version; the comment at the top names the values to change.\nconst STYLES = `\n  /* Change these: page colour, menu colour, ink, accent, how often the demo moves to the next photo. */\n  .w7img { --w7img-bg: #f6f1e8; --w7img-ink: #2a241d; --w7img-sheet: #efe6d7; --w7img-acc: #8c5a2b; --w7img-out: .45s; }\n  .w7img { position: relative; height: 600px; overflow: hidden; background: var(--w7img-bg); color: var(--w7img-ink); font-family: ui-sans-serif, system-ui, -apple-system, \"Segoe UI\", sans-serif; }\n  .w7img *, .w7img *::before, .w7img *::after { box-sizing: border-box; }\n  .w7img-bar { position: absolute; z-index: 30; top: 0; left: 0; right: 0; display: flex; align-items: center; justify-content: space-between; padding: 14px clamp(16px, 4vw, 48px); pointer-events: none; }\n  .w7img-bar > * { pointer-events: auto; }\n  .w7img-brand { margin: 0; font-size: 17px; font-weight: 600; letter-spacing: .01em; }\n  .w7img-btn { display: inline-flex; align-items: center; gap: 12px; min-height: 44px; padding: 0 18px; border: 1px solid currentColor; border-radius: 999px; background: transparent; color: inherit; font: inherit; font-size: 14px; font-weight: 600; line-height: 1; letter-spacing: .08em; text-transform: uppercase; cursor: pointer; }\n  .w7img-icon { position: relative; width: 18px; height: 10px; }\n  .w7img-icon i { position: absolute; left: 0; right: 0; height: 2px; background: currentColor; transition: transform .35s ease; }\n  .w7img-icon i:first-child { top: 0; } .w7img-icon i:last-child { bottom: 0; }\n  .w7img-open .w7img-icon i:first-child { transform: translateY(4px) rotate(45deg); }\n  .w7img-open .w7img-icon i:last-child { transform: translateY(-4px) rotate(-45deg); }\n  .w7img a:focus-visible, .w7img-btn:focus-visible { outline: 2px solid currentColor; outline-offset: 4px; }\n  .w7img-menu { visibility: hidden; transition: visibility 0s var(--w7img-out, .5s); }\n  .w7img-open .w7img-menu { visibility: visible; transition: visibility 0s; }\n  .w7img-nav ul { margin: 0; padding: 0; list-style: none; }\n  .w7img-nav a { color: inherit; text-decoration: none; }\n\n  .w7img-page { display: flex; flex-direction: column; justify-content: center; height: 100%; padding: 84px clamp(16px, 4vw, 48px) 32px; }\n  .w7img-brand { font-family: ui-serif, Georgia, \"Times New Roman\", serif; font-weight: 400; font-size: 19px; }\n  .w7img-eye { margin: 0; font-size: 13px; font-style: italic; color: var(--w7img-acc); }\n  .w7img-title { margin: 12px 0 0; max-width: 16ch; font: 400 clamp(32px, 5vw, 60px)/1.05 ui-serif, Georgia, \"Times New Roman\", serif; }\n  .w7img-text { margin: 18px 0 0; max-width: 40ch; font-size: 16px; line-height: 1.6; color: #5a4f43; }\n  .w7img-cta { align-self: flex-start; display: inline-flex; align-items: center; min-height: 44px; margin-top: 22px; padding: 0 22px; border: 1px solid var(--w7img-ink); border-radius: 2px; color: inherit; font-size: 15px; text-decoration: none; }\n  .w7img-menu { position: absolute; z-index: 20; inset: 0; display: grid; align-items: center; gap: 32px; padding: 84px clamp(16px, 4vw, 48px) 32px; background: var(--w7img-sheet);\n    opacity: 0; transition: opacity var(--w7img-out) ease, visibility 0s var(--w7img-out); }\n  .w7img-open .w7img-menu { opacity: 1; transition: opacity .4s ease, visibility 0s; }\n  @media (min-width: 760px) { .w7img-menu { grid-template-columns: 1fr 1fr; } }\n  .w7img-nav li { border-bottom: 1px solid #d6c9b4; transform: translateY(18px); opacity: 0; transition: transform .2s, opacity .2s; }\n  .w7img-open .w7img-nav li { transform: none; opacity: 1; transition: transform .6s cubic-bezier(.2, .8, .2, 1), opacity .4s; transition-duration: .45s; transition-delay: calc(.1s + var(--i) * .07s); }\n  .w7img-nav a { display: flex; align-items: baseline; justify-content: space-between; gap: 16px; min-height: 56px; padding: 10px 0; font: 400 clamp(28px, 4.4vw, 50px)/1.1 ui-serif, Georgia, \"Times New Roman\", serif; }\n  .w7img-nav small { font: 500 12px/1 ui-sans-serif, system-ui, sans-serif; color: #75654f; }\n  .w7img-nav a span { transition: transform .35s cubic-bezier(.2, .8, .2, 1); }\n  .w7img-nav a[aria-current=\"page\"] small { color: var(--w7img-acc); }\n  /* The link whose photo is showing shifts right a little. */\n  .w7img[data-show=\"0\"] [data-n=\"0\"] span, .w7img[data-show=\"1\"] [data-n=\"1\"] span, .w7img[data-show=\"2\"] [data-n=\"2\"] span, .w7img[data-show=\"3\"] [data-n=\"3\"] span { transform: translateX(14px); font-style: italic; }\n  .w7img-fig { position: relative; overflow: hidden; aspect-ratio: 4 / 3; border-radius: 3px; background: #ddd0bb; box-shadow: 0 30px 60px -36px rgba(60, 40, 20, .6); transform: scale(.96); transition: transform .6s cubic-bezier(.2, .8, .2, 1); }\n  .w7img-open .w7img-fig { transform: none; }\n  .w7img-ph { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; opacity: 0; transform: scale(1.06); transition: opacity .5s ease, transform .8s cubic-bezier(.2, .8, .2, 1); }\n  .w7img[data-show=\"0\"] .w7img-ph[data-n=\"0\"], .w7img[data-show=\"1\"] .w7img-ph[data-n=\"1\"], .w7img[data-show=\"2\"] .w7img-ph[data-n=\"2\"], .w7img[data-show=\"3\"] .w7img-ph[data-n=\"3\"] { opacity: 1; transform: none; }\n  @media (max-width: 759px) { .w7img-fig { display: none; } }\n  @media (prefers-reduced-motion: reduce) { .w7img-menu, .w7img-open .w7img-menu, .w7img-nav li, .w7img-nav a span, .w7img-fig, .w7img-ph, .w7img-icon i { transition: none !important; } }\n`; // end STYLES\n\n// When the demo opens the menu by itself (ms). Set 0 on a real site.\nconst AUTO_OPEN = 500;\n\nexport default function PaperMenuWithPhotoPreview() {\n  const ref = useRef<HTMLDivElement>(null);\n\n  useEffect(() => {\n    const root = ref.current;\n    if (!root) return;\n    const btn = root.querySelector<HTMLButtonElement>(\".w7img-btn\")!, word = root.querySelector<HTMLElement>(\".w7img-word\")!;\n    const menu = root.querySelector<HTMLElement>(\".w7img-menu\")!, page = root.querySelector<HTMLElement>(\".w7img-page\")!;\n    let used = false;\n    const isOpen = () => root.classList.contains(\"w7img-open\");\n    const set = (open: boolean, moveFocus: boolean) => {\n      root.classList.toggle(\"w7img-open\", open);\n      btn.setAttribute(\"aria-expanded\", String(open));\n      word.textContent = open ? \"Close\" : \"Menu\";\n      menu.toggleAttribute(\"inert\", !open);\n      page.toggleAttribute(\"inert\", open); // on a real page, make main and footer inert instead\n      if (moveFocus) (open ? menu.querySelector<HTMLElement>(\"a\")! : btn).focus();\n    };\n    menu.toggleAttribute(\"inert\", true);\n    const touch = () => { used = true; };\n    const onClick = () => { touch(); set(!isOpen(), true); };\n    const onKey = (e: KeyboardEvent) => {\n      if (!isOpen()) return;\n      if (e.key === \"Escape\") { e.preventDefault(); set(false, true); return; }\n      if (e.key !== \"Tab\") return;\n      // focus trap: the button plus everything in the menu\n      const f: HTMLElement[] = [btn, ...Array.from(menu.querySelectorAll<HTMLElement>(\"a[href], button\"))];\n      const i = f.indexOf(document.activeElement as HTMLElement);\n      if (e.shiftKey && i <= 0) { e.preventDefault(); f[f.length - 1].focus(); }\n      else if (!e.shiftKey && i === f.length - 1) { e.preventDefault(); f[0].focus(); }\n    };\n    const onPick = (e: MouseEvent) => {\n      const a = (e.target as HTMLElement).closest(\"a\");\n      if (!a) return;\n      e.preventDefault(); // remove on a real site so the link navigates\n      if (a.closest(\"nav\")) { menu.querySelectorAll(\"[aria-current]\").forEach((x) => x.removeAttribute(\"aria-current\")); a.setAttribute(\"aria-current\", \"page\"); }\n      set(false, true);\n    };\n    btn.addEventListener(\"click\", onClick);\n    root.addEventListener(\"pointerdown\", touch);\n    root.addEventListener(\"focusin\", touch);\n    root.addEventListener(\"keydown\", onKey);\n    menu.addEventListener(\"click\", onPick);\n    // ms between photos while the demo runs on its own\n    const CYCLE = 2600;\n    let shown = 0, seen = true;\n    const show = (n: number) => { shown = n; root.setAttribute(\"data-show\", String(n)); };\n    show(0);\n    const follow = (e: Event) => { const a = (e.target as HTMLElement).closest(\"[data-n]\"); if (a && a.closest(\"nav\")) show(Number(a.getAttribute(\"data-n\"))); };\n    menu.addEventListener(\"pointerover\", follow);\n    menu.addEventListener(\"focusin\", follow);\n    // idle demo: steps through the photos while nobody is using the piece and it is on screen\n    const io = new IntersectionObserver((en) => { seen = en[0].isIntersecting; });\n    io.observe(root);\n    const rm = window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches;\n    const cyc = rm ? 0 : window.setInterval(() => { if (!used && seen && isOpen()) show((shown + 1) % 4); }, CYCLE);\n    const t = AUTO_OPEN ? window.setTimeout(() => { if (!used) set(true, false); }, AUTO_OPEN) : 0;\n    return () => {\n      window.clearTimeout(t);\n      window.clearInterval(cyc);\n      io.disconnect();\n      menu.removeEventListener(\"pointerover\", follow);\n      menu.removeEventListener(\"focusin\", follow);\n      btn.removeEventListener(\"click\", onClick);\n      root.removeEventListener(\"pointerdown\", touch);\n      root.removeEventListener(\"focusin\", touch);\n      root.removeEventListener(\"keydown\", onKey);\n      menu.removeEventListener(\"click\", onPick);\n    };\n  }, []);\n\n  return (\n    <div ref={ref} className=\"w7img\">\n      <style>{STYLES}</style>\n      <header className=\"w7img-bar\">\n        <p className=\"w7img-brand\">The Saltmarsh Rooms</p>\n        <button className=\"w7img-btn\" type=\"button\" aria-expanded=\"false\" aria-controls=\"w7img-menu\">\n          <span className=\"w7img-word\">Menu</span><span className=\"w7img-icon\" aria-hidden=\"true\"><i></i><i></i></span>\n        </button>\n      </header>\n      <div className=\"w7img-page\">\n        <p className=\"w7img-eye\">Six rooms by the estuary</p>\n        <h2 className=\"w7img-title\">A quiet house at the end of the lane.</h2>\n        <p className=\"w7img-text\">Breakfast in the kitchen, boots by the door, and the coast path ten minutes from the gate.</p>\n        <a className=\"w7img-cta\" href=\"#\">Check dates</a>\n      </div>\n      <div className=\"w7img-menu\" id=\"w7img-menu\">\n        <nav className=\"w7img-nav\" aria-label=\"Main\">\n          <ul>\n            <li style={{ \"--i\": 0 } as CSSProperties}><a href=\"#\" data-n=\"0\" aria-current=\"page\"><span>Rooms</span><small>01</small></a></li>\n            <li style={{ \"--i\": 1 } as CSSProperties}><a href=\"#\" data-n=\"1\"><span>Breakfast</span><small>02</small></a></li>\n            <li style={{ \"--i\": 2 } as CSSProperties}><a href=\"#\" data-n=\"2\"><span>The terrace</span><small>03</small></a></li>\n            <li style={{ \"--i\": 3 } as CSSProperties}><a href=\"#\" data-n=\"3\"><span>The coast path</span><small>04</small></a></li>\n          </ul>\n        </nav>\n        <div className=\"w7img-fig\" aria-hidden=\"true\">\n          <img className=\"w7img-ph\" data-n=\"0\" src=\"https://images.pexels.com/photos/34636330/pexels-photo-34636330.jpeg?auto=compress&cs=tinysrgb&w=1200\" alt=\"Rolled white towels on a made bed with a dark headboard\" width=\"1200\" height=\"800\" decoding=\"async\" />\n          <img className=\"w7img-ph\" data-n=\"1\" src=\"https://images.pexels.com/photos/18654566/pexels-photo-18654566.jpeg?auto=compress&cs=tinysrgb&w=1200\" alt=\"A bowl and cup on a wooden table by a chair in warm morning light\" width=\"1200\" height=\"800\" decoding=\"async\" />\n          <img className=\"w7img-ph\" data-n=\"2\" src=\"https://images.pexels.com/photos/30280895/pexels-photo-30280895.jpeg?auto=compress&cs=tinysrgb&w=1200\" alt=\"Green metal garden chairs on a flagstone terrace against a stone wall\" width=\"1200\" height=\"800\" decoding=\"async\" />\n          <img className=\"w7img-ph\" data-n=\"3\" src=\"https://images.pexels.com/photos/6725997/pexels-photo-6725997.jpeg?auto=compress&cs=tinysrgb&w=1200\" alt=\"Waves breaking white against dark rocky cliffs under a bright sky\" width=\"1200\" height=\"800\" decoding=\"async\" />\n        </div>\n      </div>\n    </div>\n  );\n}\n"}]}
