{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"linen-mat-aperture-preloader-that-opens-to-the-edges","type":"registry:block","title":"Linen Mat Aperture Preloader","description":"","dependencies":[],"files":[{"path":"components/we-inc/linen-mat-aperture-preloader-that-opens-to-the-edges.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 = 1000; // 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  .w2mat { --w2mat-mat: #efe7da; --w2mat-ink: #2f2a22; --w2mat-shadow: rgba(60,48,30,.28); height: 600px; background: #d9cfbf; color: var(--w2mat-ink); font-family: ui-sans-serif, system-ui, -apple-system, 'Segoe UI', sans-serif; }\n  .w2mat-page { position: relative; height: 100%; display: flex; flex-direction: column; justify-content: space-between; padding: 0 clamp(16px, 4vw, 48px) clamp(16px, 4vw, 44px); }\n  .w2mat-bg { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; transform: scale(1.12); transition: transform 1.6s cubic-bezier(.2,.75,.15,1); }\n  .w2mat.is-out .w2mat-bg { transform: none; }\n  .w2mat-nav, .w2mat-panel { position: relative; }\n  .w2mat-nav { display: flex; align-items: center; justify-content: space-between; height: 76px; }\n  .w2mat-brand { color: inherit; font-family: ui-serif, Georgia, 'Times New Roman', serif; font-size: 22px; text-decoration: none; }\n  .w2mat-link { display: inline-flex; align-items: center; min-height: 42px; padding: 0 18px; border-radius: 999px; background: var(--w2mat-ink); color: #f7f2ea; font-size: 14px; text-decoration: none; }\n  .w2mat-panel { max-width: 27rem; background: rgba(250,246,239,.94); padding: clamp(20px, 3vw, 30px); border-radius: 4px; box-shadow: 0 20px 50px -24px var(--w2mat-shadow); }\n  .w2mat-kicker { margin: 0; font-size: 12px; letter-spacing: .2em; text-transform: uppercase; color: #6b5d48; }\n  .w2mat-title { margin: 10px 0 0; font-family: ui-serif, Georgia, 'Times New Roman', serif; font-size: clamp(28px, 3.6vw, 42px); line-height: 1.08; font-weight: 400; }\n  .w2mat-row { display: flex; gap: 14px; align-items: center; margin: 16px 0 18px; }\n  .w2mat-row img { width: 88px; height: 60px; object-fit: cover; border-radius: 2px; flex: none; }\n  .w2mat-row p { margin: 0; font-size: 15px; line-height: 1.5; color: #4f4637; }\n  .w2mat-replay { border: 1px solid #cdbfa8; background: transparent; color: var(--w2mat-ink); }\n  .w2mat-cover { position: absolute; inset: 0; z-index: 2; }\n  .w2mat-t, .w2mat-b, .w2mat-l, .w2mat-r { position: absolute; background: var(--w2mat-mat); transition: transform .9s cubic-bezier(.7,0,.2,1) .1s; }\n  .w2mat-t { top: 0; left: 0; right: 0; height: 34%; display: flex; align-items: flex-end; justify-content: center; padding-bottom: 18px; }\n  .w2mat-b { bottom: 0; left: 0; right: 0; height: 34%; display: flex; align-items: flex-start; justify-content: center; padding-top: 18px; }\n  .w2mat-l { top: 33.9%; bottom: 33.9%; left: 0; width: 34%; }\n  .w2mat-r { top: 33.9%; bottom: 33.9%; right: 0; width: 34%; }\n  .w2mat-edge { position: absolute; top: 34%; bottom: 34%; left: 34%; right: 34%; box-shadow: inset 0 2px 10px var(--w2mat-shadow); transition: opacity .3s ease; }\n  .w2mat.is-out .w2mat-edge, .w2mat.is-out .w2mat-name, .w2mat.is-out .w2mat-pct { opacity: 0; }\n  .w2mat.is-out .w2mat-t { transform: translateY(-101%); }\n  .w2mat.is-out .w2mat-b { transform: translateY(101%); }\n  .w2mat.is-out .w2mat-l { transform: translateX(-101%); }\n  .w2mat.is-out .w2mat-r { transform: translateX(101%); }\n  .w2mat-name { font-family: ui-serif, Georgia, 'Times New Roman', serif; font-size: clamp(26px, 4vw, 40px); color: var(--w2mat-ink); transition: opacity .25s ease; }\n  .w2mat-pct { font-size: 13px; letter-spacing: .2em; text-transform: uppercase; color: #5f5340; font-variant-numeric: tabular-nums; transition: opacity .25s ease; }\n  @media (max-width: 767px) {\n    .w2mat-l, .w2mat-r { width: 18%; }\n    .w2mat-edge { left: 18%; right: 18%; }\n    .w2mat-row img { width: 72px; height: 50px; }\n  }\n\n  .w2mat { position: relative; overflow: hidden; }\n  .w2mat-cover[hidden] { display: none; }\n  .w2mat.is-snap, .w2mat.is-snap * { transition: none !important; }\n  .w2mat[data-paused] * { animation-play-state: paused !important; }\n  .w2mat-sr { position: absolute; width: 1px; height: 1px; margin: -1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }\n  .w2mat-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  .w2mat-replay:focus-visible, .w2mat a:focus-visible { outline: 2px solid currentColor; outline-offset: 3px; }\n  @media (prefers-reduced-motion: reduce) {\n    .w2mat *, .w2mat *::before, .w2mat *::after { animation: none !important; transition-duration: 0s !important; transition-delay: 0s !important; }\n    .w2mat .w2mat-cover { transition: opacity .45s ease !important; }\n    .w2mat.is-out .w2mat-cover { opacity: 0; }\n    .w2mat-cover *:not([data-bar]), .w2mat-page * { transform: none !important; opacity: 1; }\n  }\n`;\n\nexport default function LinenMatAperturePreloader() {\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=\"w2mat\">\n      <style>{CSS}</style>\n      <div className=\"w2mat-page\" data-page=\"\" aria-busy={true}>\n        <img className=\"w2mat-bg\" src=\"https://images.pexels.com/photos/7232397/pexels-photo-7232397.jpeg?auto=compress&cs=tinysrgb&w=1600\" alt=\"Close view of soft beige linen fabric gathered in loose folds\" width=\"1600\" height=\"900\" />\n        <header className=\"w2mat-nav\">\n          <a href=\"#\" className=\"w2mat-brand\">Weft &amp; Fold</a>\n          <a href=\"#\" className=\"w2mat-link\">Shop bedding</a>\n        </header>\n        <div className=\"w2mat-panel\">\n          <p className=\"w2mat-kicker\">Stonewashed linen</p>\n          <h2 className=\"w2mat-title\">Sheets that soften with every wash.</h2>\n          <div className=\"w2mat-row\">\n            <img src=\"https://images.pexels.com/photos/11733651/pexels-photo-11733651.jpeg?auto=compress&cs=tinysrgb&w=600\" alt=\"Stack of folded sage green linen napkins\" width=\"600\" height=\"400\" />\n            <p>Sage, oat and chalk, cut and hemmed in a small mill.</p>\n          </div>\n          <button type=\"button\" className=\"w2mat-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 className=\"w2mat-cover\" data-cover=\"\" aria-hidden=\"true\">\n        <span className=\"w2mat-t\"><span className=\"w2mat-name\">Weft &amp; Fold</span></span>\n        <span className=\"w2mat-b\"><span className=\"w2mat-pct\"><span data-num=\"\">0</span>% woven</span></span>\n        <span className=\"w2mat-l\"></span>\n        <span className=\"w2mat-r\"></span>\n        <span className=\"w2mat-edge\"></span>\n      </div>\n      <p className=\"w2mat-sr\" data-live=\"\" aria-live=\"polite\"></p>\n    </section>\n  );\n}\n"}]}
