{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"calm-countdown-timer-to-a-date-in-days-hours-minutes","type":"registry:component","title":"Calm Countdown Timer to a Date","description":"","dependencies":["framer-motion"],"files":[{"path":"components/we-inc/calm-countdown-timer-to-a-date-in-days-hours-minutes.tsx","type":"registry:component","content":"import { useEffect, useRef, useState } from \"react\";\nimport { useReducedMotion } from \"framer-motion\";\n\n// A made-up pottery's kiln opening. If the date has passed, the same day next year is used.\nconst EVENT = \"Winter kiln opening\";\nconst TARGET = \"2026-12-05T10:00:00\";\nconst INTRO_MS = 1300; // how long the first count-up from zero takes\n\nconst two = new Intl.NumberFormat(\"en-US\", { minimumIntegerDigits: 2 });\nconst plain = new Intl.NumberFormat(\"en-US\");\nconst dateLabel = new Intl.DateTimeFormat(\"en-US\", { weekday: \"long\", day: \"numeric\", month: \"long\", hour: \"numeric\", minute: \"2-digit\" });\nconst easeOut = (t: number) => 1 - Math.pow(1 - t, 3);\n\nfunction targetDate(now: number) {\n  const d = new Date(TARGET);\n  while (d.getTime() < now) d.setFullYear(d.getFullYear() + 1);\n  return d;\n}\nfunction remaining(now: number) {\n  const ms = Math.max(0, targetDate(now).getTime() - now);\n  const mins = Math.floor(ms / 60000);\n  return { days: Math.floor(mins / 1440), hours: Math.floor((mins % 1440) / 60), minutes: mins % 60, sec: (ms % 60000) / 1000 };\n}\n\nexport default function CalmCountdownTimer() {\n  const reduce = useReducedMotion();\n  const rootRef = useRef<HTMLElement>(null);\n  const [now, setNow] = useState(() => Date.now());\n  const [intro, setIntro] = useState(1);\n  const [paused, setPaused] = useState(false);\n\n  useEffect(() => {\n    const el = rootRef.current;\n    if (!el) return;\n    let raf = 0;\n    let timer = 0;\n    let introDone = !!reduce;\n    let started = 0;\n    if (!reduce) setIntro(0);\n    const step = (t: number) => {\n      if (!started) started = t;\n      const p = Math.min(1, (t - started) / INTRO_MS);\n      setIntro(p);\n      if (p < 1) raf = requestAnimationFrame(step);\n      else introDone = true;\n    };\n    // Ticks once a minute, lined up with the clock, and only while on screen.\n    const schedule = () => {\n      setNow(Date.now());\n      timer = window.setTimeout(schedule, 60000 - (Date.now() % 60000) + 20);\n    };\n    const io = new IntersectionObserver(\n      ([entry]) => {\n        window.clearTimeout(timer);\n        cancelAnimationFrame(raf);\n        setPaused(!entry.isIntersecting);\n        if (!entry.isIntersecting) return;\n        schedule();\n        if (!introDone) {\n          started = 0;\n          raf = requestAnimationFrame(step);\n        }\n      },\n      { threshold: 0.3 },\n    );\n    io.observe(el);\n    return () => {\n      io.disconnect();\n      window.clearTimeout(timer);\n      cancelAnimationFrame(raf);\n    };\n  }, [reduce]);\n\n  const r = remaining(now);\n  const e = easeOut(intro);\n  const units = [\n    { label: \"Days\", value: r.days, show: plain.format(Math.round(r.days * e)), size: plain.format(r.days) },\n    { label: \"Hours\", value: r.hours, show: two.format(Math.round(r.hours * e)), size: \"00\" },\n    { label: \"Minutes\", value: r.minutes, show: two.format(Math.round(r.minutes * e)), size: \"00\" },\n  ];\n  const when = targetDate(now);\n  const sentence = `${EVENT} in ${plain.format(r.days)} days, ${r.hours} hours and ${r.minutes} minutes.`;\n\n  return (\n    <section\n      ref={rootRef}\n      data-paused={paused}\n      className=\"relative overflow-hidden px-5 py-16 text-[#3a2a22] sm:py-20\"\n      style={{ background: \"linear-gradient(170deg, #f8ecdd 0%, #f4dccb 55%, #eecdbd 100%)\" }}\n    >\n      <style>{`\n        @keyframes cdn-fill { from { transform: scaleX(0); } to { transform: scaleX(1); } }\n        .cdn-bar { transform: scaleX(0); animation: cdn-fill 60s linear infinite; }\n        [data-paused=\"true\"] .cdn-bar { animation-play-state: paused; }\n        @media (prefers-reduced-motion: reduce) { .cdn-bar { animation: none; transform: scaleX(0); } }\n      `}</style>\n      <div\n        aria-hidden=\"true\"\n        className=\"pointer-events-none absolute -top-40 left-1/2 h-[420px] w-[720px] -translate-x-1/2 rounded-full\"\n        style={{ background: \"radial-gradient(closest-side, rgba(255,246,228,.9), rgba(255,246,228,0))\" }}\n      />\n      <div className=\"relative mx-auto max-w-3xl text-center\">\n        <p className=\"text-sm font-medium uppercase tracking-[0.2em] text-[#94512f]\">Linden Pottery</p>\n        <h2 className=\"mt-3 font-serif text-3xl leading-tight sm:text-4xl\">{EVENT}</h2>\n        <p className=\"mt-3 text-base text-[#5d463a]\">\n          <time dateTime={when.toISOString()}>{dateLabel.format(when)}</time>, at the studio\n        </p>\n        <p className=\"sr-only\">{sentence}</p>\n        <div aria-hidden=\"true\" className=\"mt-10 flex items-start justify-center gap-3 sm:gap-6\">\n          {units.map((u, i) => (\n            <div key={u.label} className=\"flex items-start gap-3 sm:gap-6\">\n              {i > 0 && <span className=\"pt-2 text-4xl font-extralight text-[#3a2a22]/30 sm:pt-5 sm:text-6xl\">:</span>}\n              <div className=\"flex flex-col items-center\">\n                <span className=\"grid text-[clamp(3rem,13vw,6.5rem)] font-extralight leading-none tracking-tight tabular-nums\">\n                  <span className=\"invisible col-start-1 row-start-1\">{u.size}</span>\n                  <span className=\"col-start-1 row-start-1 text-center\">{u.show}</span>\n                </span>\n                <span className=\"mt-3 text-xs font-medium uppercase tracking-[0.2em] text-[#6b5245]\">{u.label}</span>\n                {u.label === \"Minutes\" && (\n                  <span className=\"mt-3 h-px w-full overflow-hidden bg-[#3a2a22]/15\">\n                    <span\n                      key={now}\n                      className=\"cdn-bar block h-full origin-left bg-[#94512f]\"\n                      style={{ animationDelay: `-${r.sec.toFixed(2)}s` }}\n                    />\n                  </span>\n                )}\n              </div>\n            </div>\n          ))}\n        </div>\n        <p className=\"mx-auto mt-10 max-w-md text-sm leading-relaxed text-[#5d463a]\">\n          The figures change once a minute and only a hairline creeps under the minutes, so it can sit beside your booking button without nagging. The studio and date are made up for this demo.\n        </p>\n      </div>\n    </section>\n  );\n}\n"}]}
