{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"espresso-circle-preloader-that-shrinks-into-the-brand-dot","type":"registry:block","title":"Espresso Circle Shrink Preloader","description":"","dependencies":[],"files":[{"path":"components/we-inc/espresso-circle-preloader-that-shrinks-into-the-brand-dot.tsx","type":"registry:component","content":"import { useEffect, useRef } from \"react\";\n\nconst MIN = 800; // shortest time the cover shows, in ms\nconst CAP = 1400; // longest the cover waits for images, in ms\nconst EXIT = 800; // 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  .w2cir { --w2cir-espresso: #251913; --w2cir-crema: #d9a66b; --w2cir-milk: #f4ece2; height: 600px; background: var(--w2cir-milk); color: #241812; font-family: ui-sans-serif, system-ui, -apple-system, 'Segoe UI', sans-serif; }\n  .w2cir-page { height: 100%; display: flex; flex-direction: column; padding: 0 clamp(16px, 4vw, 48px); }\n  .w2cir-nav { display: flex; align-items: center; justify-content: space-between; gap: 12px; height: 72px; }\n  .w2cir-brand { display: inline-flex; align-items: center; gap: 10px; color: inherit; font-weight: 700; font-size: 17px; text-decoration: none; }\n  .w2cir-dot { width: 14px; height: 14px; border-radius: 50%; background: var(--w2cir-espresso); }\n  .w2cir-link { display: inline-flex; align-items: center; min-height: 40px; color: #7a4a22; font-size: 14px; font-weight: 600; text-underline-offset: 4px; }\n  .w2cir-hero { flex: 1; display: grid; grid-template-columns: 1fr 1fr; gap: clamp(20px, 4vw, 56px); align-items: center; min-height: 0; padding-bottom: 24px; }\n  .w2cir-kicker { margin: 0; font-size: 12px; letter-spacing: .2em; text-transform: uppercase; color: #7a4a22; }\n  .w2cir-title { margin: 12px 0 0; font-size: clamp(30px, 4.4vw, 54px); line-height: 1.04; letter-spacing: -.03em; font-weight: 700; }\n  .w2cir-copy { margin: 16px 0 0; font-size: 16px; line-height: 1.6; color: #5b4637; max-width: 28rem; }\n  .w2cir-row { display: flex; flex-wrap: wrap; gap: 12px; margin-top: 24px; }\n  .w2cir-cta { display: inline-flex; align-items: center; min-height: 44px; padding: 0 22px; border-radius: 999px; background: var(--w2cir-espresso); color: var(--w2cir-milk); font-size: 14px; font-weight: 600; text-decoration: none; }\n  .w2cir-replay { border: 1px solid #d6c3ad; background: transparent; color: #241812; }\n  .w2cir-pics { position: relative; height: 100%; max-height: 430px; }\n  .w2cir-pics img { position: absolute; object-fit: cover; display: block; }\n  .w2cir-a { top: 0; left: 0; width: 78%; height: 100%; border-radius: 220px 220px 8px 8px; transform: scale(.94); transition: transform 1.2s cubic-bezier(.2,.75,.15,1) .35s; }\n  .w2cir-b { right: 0; bottom: 30px; width: 40%; aspect-ratio: 1; height: auto; border-radius: 50%; border: 6px solid var(--w2cir-milk); transform: scale(.7); transition: transform 1.2s cubic-bezier(.3,1.3,.5,1) .5s; }\n  .w2cir.is-out .w2cir-a, .w2cir.is-out .w2cir-b { transform: none; }\n  .w2cir-cover { position: absolute; inset: 0; z-index: 2; overflow: hidden; }\n  .w2cir-disc { --w2cir-d: max(300%, 1500px); position: absolute; left: calc(clamp(16px, 4vw, 48px) + 7px); top: 36px; width: var(--w2cir-d); height: auto; aspect-ratio: 1; margin-left: calc(var(--w2cir-d) / -2); margin-top: calc(var(--w2cir-d) / -2); border-radius: 50%; background: var(--w2cir-espresso); transition: transform .75s cubic-bezier(.5,0,.85,.35); }\n  .w2cir.is-out .w2cir-disc { transform: scale(0); }\n  .w2cir-mid { position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 8px; color: var(--w2cir-milk); transition: opacity .25s ease; }\n  .w2cir.is-out .w2cir-mid { opacity: 0; }\n  .w2cir-bean { position: relative; width: 30px; height: 42px; border-radius: 50%; background: var(--w2cir-crema); animation: w2cir-turn 2.4s cubic-bezier(.6,0,.4,1) infinite; }\n  .w2cir-bean span { position: absolute; left: 50%; top: 6px; bottom: 6px; width: 3px; margin-left: -1.5px; border-radius: 3px; background: var(--w2cir-espresso); transform: rotate(8deg); }\n  @keyframes w2cir-turn { 0%, 20% { transform: rotate(0deg); } 50%, 70% { transform: rotate(180deg); } 100% { transform: rotate(360deg); } }\n  .w2cir-pct { margin: 10px 0 0; font-size: clamp(44px, 7vw, 76px); font-weight: 300; line-height: 1; font-variant-numeric: tabular-nums; }\n  .w2cir-note { margin: 0; font-size: 13px; letter-spacing: .2em; text-transform: uppercase; color: var(--w2cir-crema); }\n  @media (max-width: 767px) {\n    .w2cir-link { font-size: 13px; }\n    .w2cir-hero { grid-template-columns: 1fr; align-content: start; gap: 18px; }\n    .w2cir-pics { height: 170px; order: -1; }\n    .w2cir-a { width: 100%; border-radius: 120px 120px 8px 8px; }\n    .w2cir-b { display: none; }\n    .w2cir-copy { font-size: 15px; margin-top: 12px; }\n  }\n\n  .w2cir { position: relative; overflow: hidden; }\n  .w2cir-cover[hidden] { display: none; }\n  .w2cir.is-snap, .w2cir.is-snap * { transition: none !important; }\n  .w2cir[data-paused] * { animation-play-state: paused !important; }\n  .w2cir-sr { position: absolute; width: 1px; height: 1px; margin: -1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }\n  .w2cir-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  .w2cir-replay:focus-visible, .w2cir a:focus-visible { outline: 2px solid currentColor; outline-offset: 3px; }\n  @media (prefers-reduced-motion: reduce) {\n    .w2cir *, .w2cir *::before, .w2cir *::after { animation: none !important; transition-duration: 0s !important; transition-delay: 0s !important; }\n    .w2cir .w2cir-cover { transition: opacity .45s ease !important; }\n    .w2cir.is-out .w2cir-cover { opacity: 0; }\n    .w2cir-cover *:not([data-bar]), .w2cir-page * { transform: none !important; opacity: 1; }\n  }\n`;\n\nexport default function EspressoCircleShrinkPreloader() {\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=\"w2cir\">\n      <style>{CSS}</style>\n      <div className=\"w2cir-page\" data-page=\"\" aria-busy={true}>\n        <header className=\"w2cir-nav\">\n          <a href=\"#\" className=\"w2cir-brand\"><span className=\"w2cir-dot\" aria-hidden=\"true\"></span>Ember &amp; Ash</a>\n          <a href=\"#\" className=\"w2cir-link\">Coffee subscriptions</a>\n        </header>\n        <div className=\"w2cir-hero\">\n          <div className=\"w2cir-text\">\n            <p className=\"w2cir-kicker\">Small batch roastery</p>\n            <h2 className=\"w2cir-title\">Roasted on Monday, at your door by Wednesday.</h2>\n            <p className=\"w2cir-copy\">Single origin beans roasted in twelve kilo batches, light enough to taste where they came from.</p>\n            <div className=\"w2cir-row\"><a href=\"#\" className=\"w2cir-cta\">Choose a coffee</a><button type=\"button\" className=\"w2cir-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></div>\n          </div>\n          <div className=\"w2cir-pics\">\n            <img className=\"w2cir-a\" src=\"https://images.pexels.com/photos/894695/pexels-photo-894695.jpeg?auto=compress&cs=tinysrgb&w=1200\" alt=\"Freshly roasted coffee beans turning in the drum of a large roaster\" width=\"1200\" height=\"800\" />\n            <img className=\"w2cir-b\" src=\"https://images.pexels.com/photos/14848712/pexels-photo-14848712.jpeg?auto=compress&cs=tinysrgb&w=800\" alt=\"Close view of dark roasted coffee beans filling a container\" width=\"800\" height=\"533\" />\n          </div>\n        </div>\n      </div>\n      <div className=\"w2cir-cover\" data-cover=\"\" aria-hidden=\"true\">\n        <span className=\"w2cir-disc\"></span>\n        <div className=\"w2cir-mid\">\n          <span className=\"w2cir-bean\"><span></span></span>\n          <p className=\"w2cir-pct\"><span data-num=\"\">0</span>%</p>\n          <p className=\"w2cir-note\">Roasting the page</p>\n        </div>\n      </div>\n      <p className=\"w2cir-sr\" data-live=\"\" aria-live=\"polite\"></p>\n    </section>\n  );\n}\n"}]}
