{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"sand-tile-mosaic-preloader-for-a-photography-gallery","type":"registry:block","title":"Sand Tile Mosaic Preloader","description":"","dependencies":[],"files":[{"path":"components/we-inc/sand-tile-mosaic-preloader-for-a-photography-gallery.tsx","type":"registry:component","content":"import { useEffect, useRef } from \"react\";\n\nconst MIN = 1000; // shortest time the cover shows, in ms\nconst CAP = 1500; // longest the cover waits for images, in ms\nconst EXIT = 1050; // length of the exit, in ms\n// On a real page: give the cover position: fixed; inset: 0 and put data-page on your <main>.\n\nconst CSS = `\n  .w2til { --w2til-tile: #2b251d; --w2til-sand: #ebe0cc; --w2til-ink: #241f18; height: 600px; background: var(--w2til-sand); color: var(--w2til-ink); font-family: ui-sans-serif, system-ui, -apple-system, 'Segoe UI', sans-serif; }\n  .w2til-page { height: 100%; display: flex; flex-direction: column; padding: 0 clamp(16px, 4vw, 48px) clamp(16px, 3vw, 36px); }\n  .w2til-nav { display: flex; align-items: center; justify-content: space-between; height: 68px; }\n  .w2til-brand { color: inherit; font-weight: 700; font-size: 15px; letter-spacing: .06em; text-transform: uppercase; text-decoration: none; }\n  .w2til-link { color: inherit; font-size: 14px; display: inline-flex; align-items: center; min-height: 40px; text-underline-offset: 4px; }\n  .w2til-hero { flex: 1; display: grid; grid-template-columns: 1.5fr 1fr; gap: clamp(16px, 3vw, 40px); min-height: 0; }\n  .w2til-main { width: 100%; height: 100%; min-height: 0; object-fit: cover; display: block; border-radius: 2px; }\n  .w2til-text { display: flex; flex-direction: column; justify-content: flex-end; }\n  .w2til-kicker { margin: 0; font-size: 12px; letter-spacing: .2em; text-transform: uppercase; color: #6a5c45; }\n  .w2til-title { margin: 8px 0 0; font-family: ui-serif, Georgia, 'Times New Roman', serif; font-size: clamp(40px, 5.6vw, 76px); line-height: 1; font-weight: 400; }\n  .w2til-copy { margin: 14px 0 18px; font-size: 15px; line-height: 1.6; color: #4b4133; }\n  .w2til-small { width: 100%; height: auto; aspect-ratio: 2 / 1; object-fit: cover; margin-bottom: 18px; border-radius: 2px; }\n  .w2til-replay { align-self: flex-start; border: 1px solid #bfae90; background: #f5eee1; color: var(--w2til-ink); }\n  .w2til-cover { position: absolute; inset: 0; z-index: 2; }\n  .w2til-grid { position: absolute; inset: 0; display: grid; grid-template-columns: repeat(6, 1fr); grid-template-rows: repeat(4, 1fr); }\n  .w2til-tile { background: var(--w2til-tile); transition: transform .5s cubic-bezier(.6,0,.3,1), opacity .5s ease; margin: -1px; }\n  .w2til-tile:nth-child(1) { transition-delay: 0.000s; } .w2til-tile:nth-child(2) { transition-delay: 0.045s; } .w2til-tile:nth-child(3) { transition-delay: 0.090s; } .w2til-tile:nth-child(4) { transition-delay: 0.135s; } .w2til-tile:nth-child(5) { transition-delay: 0.180s; } .w2til-tile:nth-child(6) { transition-delay: 0.225s; } .w2til-tile:nth-child(7) { transition-delay: 0.045s; } .w2til-tile:nth-child(8) { transition-delay: 0.090s; } .w2til-tile:nth-child(9) { transition-delay: 0.135s; } .w2til-tile:nth-child(10) { transition-delay: 0.180s; } .w2til-tile:nth-child(11) { transition-delay: 0.225s; } .w2til-tile:nth-child(12) { transition-delay: 0.270s; } .w2til-tile:nth-child(13) { transition-delay: 0.090s; } .w2til-tile:nth-child(14) { transition-delay: 0.135s; } .w2til-tile:nth-child(15) { transition-delay: 0.180s; } .w2til-tile:nth-child(16) { transition-delay: 0.225s; } .w2til-tile:nth-child(17) { transition-delay: 0.270s; } .w2til-tile:nth-child(18) { transition-delay: 0.315s; } .w2til-tile:nth-child(19) { transition-delay: 0.135s; } .w2til-tile:nth-child(20) { transition-delay: 0.180s; } .w2til-tile:nth-child(21) { transition-delay: 0.225s; } .w2til-tile:nth-child(22) { transition-delay: 0.270s; } .w2til-tile:nth-child(23) { transition-delay: 0.315s; } .w2til-tile:nth-child(24) { transition-delay: 0.360s; }\n  .w2til.is-out .w2til-tile { transform: scale(0); opacity: 0; }\n  .w2til-pct { position: absolute; left: 0; right: 0; top: 50%; margin: -10px 0 0; text-align: center; font-size: 15px; letter-spacing: .24em; text-transform: uppercase; color: #f1e8d8; font-variant-numeric: tabular-nums; transition: opacity .2s ease; }\n  .w2til.is-out .w2til-pct { opacity: 0; }\n  @media (max-width: 767px) {\n    .w2til-hero { grid-template-columns: 1fr; grid-template-rows: 230px auto; gap: 18px; }\n    .w2til-text { justify-content: flex-start; }\n    .w2til-small { display: none; }\n    .w2til-copy { margin: 10px 0 14px; }\n  }\n\n  .w2til { position: relative; overflow: hidden; }\n  .w2til-cover[hidden] { display: none; }\n  .w2til.is-snap, .w2til.is-snap * { transition: none !important; }\n  .w2til[data-paused] * { animation-play-state: paused !important; }\n  .w2til-sr { position: absolute; width: 1px; height: 1px; margin: -1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }\n  .w2til-replay { display: inline-flex; align-items: center; gap: 8px; min-height: 40px; padding: 0 16px; border-radius: 999px; font: 600 13px/1 ui-sans-serif, system-ui, sans-serif; cursor: pointer; }\n  .w2til-replay:focus-visible, .w2til a:focus-visible { outline: 2px solid currentColor; outline-offset: 3px; }\n  @media (prefers-reduced-motion: reduce) {\n    .w2til *, .w2til *::before, .w2til *::after { animation: none !important; transition-duration: 0s !important; transition-delay: 0s !important; }\n    .w2til .w2til-cover { transition: opacity .45s ease !important; }\n    .w2til.is-out .w2til-cover { opacity: 0; }\n    .w2til-cover *:not([data-bar]), .w2til-page * { transform: none !important; opacity: 1; }\n  }\n`;\n\nexport default function SandTileMosaicPreloader() {\n  const ref = useRef<HTMLElement>(null);\n\n  useEffect(() => {\n    const root = ref.current;\n    if (!root) return;\n    const page = root.querySelector<HTMLElement>(\"[data-page]\")!;\n    const cover = root.querySelector<HTMLElement>(\"[data-cover]\")!;\n    const live = root.querySelector<HTMLElement>(\"[data-live]\")!;\n    const again = root.querySelector<HTMLButtonElement>(\"[data-replay]\")!;\n    const nums = root.querySelectorAll<HTMLElement>(\"[data-num]\");\n    const bars = root.querySelectorAll<HTMLElement>(\"[data-bar]\");\n    const ticks = root.querySelectorAll<HTMLElement>(\"[data-tick]\");\n    const rm = window.matchMedia(\"(prefers-reduced-motion: reduce)\");\n    let run = 0, frame = 0, seen = true, timer = 0;\n    let step: ((now: number) => void) | null = null;\n    const draw = (v: number) => {\n      const t = String(Math.round(v * 100));\n      nums.forEach((n) => { n.textContent = t; });\n      bars.forEach((b) => { b.style.transform = `scaleX(${v})`; });\n    };\n    const start = (refocus: boolean) => {\n      const id = ++run, t0 = performance.now(), imgs = page.querySelectorAll(\"img\"), total = imgs.length;\n      let done = 0, shown = 0;\n      cancelAnimationFrame(frame);\n      window.clearTimeout(timer);\n      root.classList.add(\"is-snap\");\n      root.classList.remove(\"is-out\");\n      cover.hidden = false;\n      void root.offsetWidth;\n      root.classList.remove(\"is-snap\");\n      page.inert = true;\n      page.setAttribute(\"aria-busy\", \"true\");\n      live.textContent = \"Loading\";\n      ticks.forEach((k) => k.classList.remove(\"is-on\"));\n      // Real progress: one step per image that fires load or error.\n      imgs.forEach((img, i) => {\n        const hit = () => { if (id !== run) return; done++; ticks[i]?.classList.add(\"is-on\"); };\n        if (img.complete) hit();\n        else { img.addEventListener(\"load\", hit, { once: true }); img.addEventListener(\"error\", hit, { once: true }); }\n      });\n      step = (now: number) => {\n        let real = total ? done / total : 1;\n        const t = now - t0;\n        if (t > CAP) real = 1;\n        shown += (real - shown) * (rm.matches ? 1 : 0.2);\n        if (real - shown < 0.004) shown = real;\n        draw(shown);\n        if (shown === 1 && t >= MIN) {\n          step = null;\n          frame = 0;\n          root.classList.add(\"is-out\");\n          live.textContent = \"Page ready\";\n          page.inert = false;\n          page.removeAttribute(\"aria-busy\");\n          timer = window.setTimeout(() => { cover.hidden = true; if (refocus) again.focus(); }, rm.matches ? 500 : EXIT);\n          return;\n        }\n        frame = seen ? requestAnimationFrame(step) : 0;\n      };\n      frame = requestAnimationFrame(step);\n    };\n    // Off screen: stop the frame loop and pause CSS loops.\n    const io = new IntersectionObserver(([e]) => {\n      seen = e.isIntersecting;\n      root.toggleAttribute(\"data-paused\", !seen);\n      if (seen && step && !frame) frame = requestAnimationFrame(step);\n    });\n    io.observe(root);\n    const onReplay = () => start(true);\n    again.addEventListener(\"click\", onReplay);\n    start(false);\n    return () => {\n      run++;\n      cancelAnimationFrame(frame);\n      window.clearTimeout(timer);\n      io.disconnect();\n      again.removeEventListener(\"click\", onReplay);\n    };\n  }, []);\n\n  return (\n    <section ref={ref} className=\"w2til\">\n      <style>{CSS}</style>\n      <div className=\"w2til-page\" data-page=\"\" aria-busy={true}>\n        <header className=\"w2til-nav\">\n          <a href=\"#\" className=\"w2til-brand\">Field Notes Gallery</a>\n          <a href=\"#\" className=\"w2til-link\">Visit</a>\n        </header>\n        <div className=\"w2til-hero\">\n          <img className=\"w2til-main\" src=\"https://images.pexels.com/photos/6877573/pexels-photo-6877573.jpeg?auto=compress&cs=tinysrgb&w=1200\" alt=\"Narrow corridor lined with framed photographs under warm lights and a polished wooden floor\" width=\"1200\" height=\"800\" />\n          <div className=\"w2til-text\">\n            <p className=\"w2til-kicker\">Now showing, ground floor</p>\n            <h2 className=\"w2til-title\">Quiet Rooms</h2>\n            <p className=\"w2til-copy\">Black and white photographs of empty interiors, hung close together along the long corridor.</p>\n            <img className=\"w2til-small\" src=\"https://images.pexels.com/photos/310435/pexels-photo-310435.jpeg?auto=compress&cs=tinysrgb&w=800\" alt=\"Ten small black and white photographs in black frames hung in two rows on a white wall\" width=\"800\" height=\"540\" />\n            <button type=\"button\" className=\"w2til-replay\" data-replay=\"\">\n              <svg viewBox=\"0 0 20 20\" width=\"16\" height=\"16\" fill=\"none\" stroke=\"currentColor\" strokeWidth=\"1.8\" strokeLinecap=\"round\" strokeLinejoin=\"round\" aria-hidden=\"true\"><path d=\"M4 10a6 6 0 1 0 1.8-4.3\" /><path d=\"M4 3.5v3.2h3.2\" /></svg>\n              Replay intro\n            </button>\n          </div>\n        </div>\n      </div>\n      <div className=\"w2til-cover\" data-cover=\"\" aria-hidden=\"true\">\n        <div className=\"w2til-grid\"><span className=\"w2til-tile\"></span><span className=\"w2til-tile\"></span><span className=\"w2til-tile\"></span><span className=\"w2til-tile\"></span><span className=\"w2til-tile\"></span><span className=\"w2til-tile\"></span><span className=\"w2til-tile\"></span><span className=\"w2til-tile\"></span><span className=\"w2til-tile\"></span><span className=\"w2til-tile\"></span><span className=\"w2til-tile\"></span><span className=\"w2til-tile\"></span><span className=\"w2til-tile\"></span><span className=\"w2til-tile\"></span><span className=\"w2til-tile\"></span><span className=\"w2til-tile\"></span><span className=\"w2til-tile\"></span><span className=\"w2til-tile\"></span><span className=\"w2til-tile\"></span><span className=\"w2til-tile\"></span><span className=\"w2til-tile\"></span><span className=\"w2til-tile\"></span><span className=\"w2til-tile\"></span><span className=\"w2til-tile\"></span></div>\n        <p className=\"w2til-pct\">Hanging the prints <span data-num=\"\">0</span>%</p>\n      </div>\n      <p className=\"w2til-sr\" data-live=\"\" aria-live=\"polite\"></p>\n    </section>\n  );\n}\n"}]}
