{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"paper-counter-preloader-that-hands-over-to-the-headline","type":"registry:block","title":"Paper Counter Handover Preloader","description":"","dependencies":[],"files":[{"path":"components/we-inc/paper-counter-preloader-that-hands-over-to-the-headline.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 = 1100; // 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  .w2cnt { --w2cnt-paper: #f2ece1; --w2cnt-ink: #211c16; --w2cnt-rust: #9a4a26; height: 600px; background: var(--w2cnt-paper); color: var(--w2cnt-ink); font-family: ui-sans-serif, system-ui, -apple-system, 'Segoe UI', sans-serif; }\n  .w2cnt-page { height: 100%; display: flex; flex-direction: column; padding: 0 clamp(16px, 4vw, 48px); }\n  .w2cnt-nav { display: flex; align-items: center; justify-content: space-between; height: 72px; }\n  .w2cnt-brand { color: inherit; font-family: ui-serif, Georgia, 'Times New Roman', serif; font-size: 20px; font-style: italic; text-decoration: none; }\n  .w2cnt-link { color: var(--w2cnt-rust); font-size: 14px; font-weight: 600; text-underline-offset: 4px; display: inline-flex; align-items: center; min-height: 40px; }\n  .w2cnt-hero { flex: 1; display: grid; grid-template-columns: 1.4fr 1fr; gap: clamp(20px, 4vw, 64px); align-items: end; padding-bottom: 44px; min-height: 0; }\n  .w2cnt-title { margin: 0; font-size: clamp(44px, 8.4vw, 124px); line-height: .94; letter-spacing: -.045em; font-weight: 600; }\n  .w2cnt-mask { display: block; overflow: hidden; padding-bottom: .06em; }\n  .w2cnt-line { display: block; transform: translateY(105%); transition: transform .9s cubic-bezier(.2,.75,.15,1); }\n  .w2cnt-mask:nth-child(2) .w2cnt-line { transition-delay: .08s; } .w2cnt-mask:nth-child(3) .w2cnt-line { transition-delay: .16s; color: var(--w2cnt-rust); }\n  .w2cnt.is-out .w2cnt-line { transform: none; }\n  .w2cnt-frame { overflow: hidden; aspect-ratio: 4 / 3; border-radius: 4px; }\n  .w2cnt-frame img { width: 100%; height: 100%; object-fit: cover; display: block; transform: scale(1.08); transition: transform 1.3s cubic-bezier(.2,.75,.15,1) .2s; }\n  .w2cnt.is-out .w2cnt-frame img { transform: none; }\n  .w2cnt-copy { margin: 16px 0 14px; font-size: 15px; line-height: 1.6; color: #4b4339; }\n  .w2cnt-replay { border: 1px solid #c9bfae; background: #fbf8f2; color: var(--w2cnt-ink); }\n  .w2cnt-cover { position: absolute; inset: 0; z-index: 2; background: var(--w2cnt-paper); display: flex; flex-direction: column; justify-content: flex-end; padding: 0 clamp(16px, 4vw, 48px) 44px; transition: opacity .5s ease .45s; }\n  .w2cnt.is-out .w2cnt-cover { opacity: 0; }\n  .w2cnt-label { position: absolute; top: 26px; left: clamp(16px, 4vw, 48px); margin: 0; font-size: 13px; color: #6a6054; transition: opacity .3s ease; }\n  .w2cnt.is-out .w2cnt-label { opacity: 0; }\n  .w2cnt-big { margin: 0; font-size: clamp(110px, 22vw, 300px); line-height: .9; font-weight: 600; letter-spacing: -.06em; font-variant-numeric: tabular-nums; }\n  .w2cnt-num { display: block; transition: transform .6s cubic-bezier(.6,0,.3,1); }\n  .w2cnt-num small { font-size: .3em; letter-spacing: 0; vertical-align: top; color: var(--w2cnt-rust); }\n  .w2cnt.is-out .w2cnt-num { transform: translateY(-105%); }\n  .w2cnt-rule { display: block; height: 2px; margin-top: 18px; background: #ddd3c3; overflow: hidden; }\n  .w2cnt-rule span { display: block; height: 100%; background: var(--w2cnt-ink); transform: scaleX(0); transform-origin: left; }\n  @media (max-width: 767px) {\n    .w2cnt-hero { grid-template-columns: 1fr; align-content: end; gap: 18px; padding-bottom: 28px; }\n    .w2cnt-frame { aspect-ratio: 16 / 9; max-height: 170px; width: 100%; }\n    .w2cnt-copy { margin: 12px 0 10px; }\n  }\n\n  .w2cnt { position: relative; overflow: hidden; }\n  .w2cnt-cover[hidden] { display: none; }\n  .w2cnt.is-snap, .w2cnt.is-snap * { transition: none !important; }\n  .w2cnt[data-paused] * { animation-play-state: paused !important; }\n  .w2cnt-sr { position: absolute; width: 1px; height: 1px; margin: -1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }\n  .w2cnt-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  .w2cnt-replay:focus-visible, .w2cnt a:focus-visible { outline: 2px solid currentColor; outline-offset: 3px; }\n  @media (prefers-reduced-motion: reduce) {\n    .w2cnt *, .w2cnt *::before, .w2cnt *::after { animation: none !important; transition-duration: 0s !important; transition-delay: 0s !important; }\n    .w2cnt .w2cnt-cover { transition: opacity .45s ease !important; }\n    .w2cnt.is-out .w2cnt-cover { opacity: 0; }\n    .w2cnt-cover *:not([data-bar]), .w2cnt-page * { transform: none !important; opacity: 1; }\n  }\n`;\n\nexport default function PaperCounterHandoverPreloader() {\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=\"w2cnt\">\n      <style>{CSS}</style>\n      <div className=\"w2cnt-page\" data-page=\"\" aria-busy={true}>\n        <header className=\"w2cnt-nav\">\n          <a href=\"#\" className=\"w2cnt-brand\">Marrow &amp; Pine</a>\n          <a href=\"#\" className=\"w2cnt-link\">Start a project</a>\n        </header>\n        <div className=\"w2cnt-hero\">\n          <h2 className=\"w2cnt-title\">\n            <span className=\"w2cnt-mask\"><span className=\"w2cnt-line\">Packaging</span></span>\n            <span className=\"w2cnt-mask\"><span className=\"w2cnt-line\">people keep</span></span>\n            <span className=\"w2cnt-mask\"><span className=\"w2cnt-line\">on the shelf.</span></span>\n          </h2>\n          <div className=\"w2cnt-side\">\n            <div className=\"w2cnt-frame\"><img src=\"https://images.pexels.com/photos/414974/pexels-photo-414974.jpeg?auto=compress&cs=tinysrgb&w=1200\" alt=\"Desk seen from above with a laptop, colour swatches, pencils and sketches\" width=\"1200\" height=\"800\" /></div>\n            <p className=\"w2cnt-copy\">A four-person studio for labels, boxes and the small printed things that go inside them.</p>\n            <button type=\"button\" className=\"w2cnt-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=\"w2cnt-cover\" data-cover=\"\" aria-hidden=\"true\">\n        <p className=\"w2cnt-label\">Marrow &amp; Pine, design studio</p>\n        <p className=\"w2cnt-big\"><span className=\"w2cnt-mask\"><span className=\"w2cnt-num\"><span data-num=\"\">0</span><small>%</small></span></span></p>\n        <span className=\"w2cnt-rule\"><span data-bar=\"\"></span></span>\n      </div>\n      <p className=\"w2cnt-sr\" data-live=\"\" aria-live=\"polite\"></p>\n    </section>\n  );\n}\n"}]}
