{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"countdown","type":"registry:ui","title":"Countdown","description":"Countdown to an open house or launch with rolling flip digits for days, hours, minutes and seconds.","dependencies":["motion"],"registryDependencies":["utils"],"files":[{"path":"registry/items/countdown/countdown.tsx","type":"registry:ui","target":"components/facade/countdown.tsx","content":"\"use client\";\n\nimport * as React from \"react\";\nimport { AnimatePresence, motion, useReducedMotion } from \"motion/react\";\nimport { cn } from \"@/lib/utils\";\n\nexport interface CountdownProps {\n  /** Target date. Accepts a Date, ISO string or timestamp. Include a UTC offset in strings (\"2026-11-14T10:00:00-05:00\") so the event time is the same for every visitor. */\n  target?: Date | string | number;\n  /** Called once when the countdown reaches zero. */\n  onComplete?: () => void;\n  /** Labels for each unit. */\n  labels?: { days: string; hours: string; minutes: string; seconds: string };\n  /** Hide the seconds column. */\n  hideSeconds?: boolean;\n  /** Rendered when time is up. */\n  completeContent?: React.ReactNode;\n  size?: \"sm\" | \"md\" | \"lg\";\n  className?: string;\n}\n\ntype Parts = { days: number; hours: number; minutes: number; seconds: number };\n\nfunction getParts(ms: number): Parts {\n  const total = Math.max(0, Math.floor(ms / 1000));\n  return {\n    days: Math.floor(total / 86400),\n    hours: Math.floor((total % 86400) / 3600),\n    minutes: Math.floor((total % 3600) / 60),\n    seconds: total % 60,\n  };\n}\n\n// md and lg shrink below the sm breakpoint so four units plus separators fit a 360px viewport.\nconst sizes = {\n  sm: { box: \"h-10 w-7 text-xl rounded-lg\", gap: \"gap-1\", label: \"text-[10px]\", sep: \"text-lg\", sepOffset: \"mt-1\" },\n  md: { box: \"h-11 w-8 text-2xl rounded-lg sm:h-14 sm:w-11 sm:text-[30px] sm:rounded-xl\", gap: \"gap-1\", label: \"text-[11px]\", sep: \"text-lg sm:text-2xl\", sepOffset: \"mt-1.5 sm:mt-2.5\" },\n  lg: { box: \"h-12 w-9 text-2xl rounded-lg sm:h-20 sm:w-14 sm:text-5xl sm:rounded-xl\", gap: \"gap-1 sm:gap-1.5\", label: \"text-[11px] sm:text-xs\", sep: \"text-xl sm:text-4xl\", sepOffset: \"mt-2 sm:mt-4\" },\n};\n\nfunction Digit({ value, size }: { value: string; size: keyof typeof sizes }) {\n  const reduce = useReducedMotion();\n  return (\n    <span\n      className={cn(\n        \"relative inline-flex items-center justify-center overflow-hidden bg-background font-display font-semibold tabular-nums tracking-[-0.02em] text-foreground shadow-sm shadow-foreground/[0.06] dark:bg-foreground/[0.1] dark:shadow-none\",\n        sizes[size].box,\n      )}\n    >\n      <AnimatePresence initial={false} mode=\"popLayout\">\n        <motion.span\n          key={value}\n          initial={reduce ? { opacity: 0 } : { y: \"-70%\", rotateX: 75, opacity: 0, filter: \"blur(2px)\" }}\n          animate={{ y: \"0%\", rotateX: 0, opacity: 1, filter: \"blur(0px)\" }}\n          exit={reduce ? { opacity: 0 } : { y: \"70%\", rotateX: -75, opacity: 0, filter: \"blur(2px)\" }}\n          transition={{ duration: reduce ? 0.15 : 0.5, ease: [0.16, 1, 0.3, 1] }}\n          className=\"inline-block\"\n          style={{ transformPerspective: 300 }}\n        >\n          {value}\n        </motion.span>\n      </AnimatePresence>\n    </span>\n  );\n}\n\nfunction Unit({ value, label, size, pad }: { value: number; label: string; size: keyof typeof sizes; pad: number }) {\n  const str = String(value).padStart(pad, \"0\");\n  return (\n    <div className=\"flex flex-col items-center gap-2\">\n      <div className={cn(\"flex\", sizes[size].gap)}>\n        {str.split(\"\").map((d, i) => (\n          <Digit key={`${str.length}-${i}`} value={d} size={size} />\n        ))}\n      </div>\n      <span className={cn(\"font-medium uppercase tracking-[0.1em] text-muted-foreground\", sizes[size].label)}>{label}</span>\n    </div>\n  );\n}\n\nexport function Countdown({\n  target,\n  onComplete,\n  labels = { days: \"Days\", hours: \"Hours\", minutes: \"Minutes\", seconds: \"Seconds\" },\n  hideSeconds = false,\n  completeContent,\n  size = \"md\",\n  className,\n}: CountdownProps) {\n  const targetMs = React.useMemo(() => {\n    if (target === undefined) return null;\n    return target instanceof Date ? target.getTime() : new Date(target).getTime();\n  }, [target]);\n\n  // Render only after mount to avoid hydration mismatch from Date.now().\n  const [now, setNow] = React.useState<number | null>(null);\n  const [fallbackTarget, setFallbackTarget] = React.useState<number | null>(null);\n  const completedRef = React.useRef(false);\n  const onCompleteRef = React.useRef(onComplete);\n  React.useEffect(() => {\n    onCompleteRef.current = onComplete;\n  });\n\n  React.useEffect(() => {\n    const start = Date.now();\n    if (targetMs === null) setFallbackTarget(start + ((3 * 24 + 7) * 3600 + 42 * 60 + 18) * 1000);\n    setNow(start);\n    const id = window.setInterval(() => setNow(Date.now()), 1000);\n    return () => window.clearInterval(id);\n  }, [targetMs]);\n\n  const end = targetMs ?? fallbackTarget;\n  const remaining = now !== null && end !== null ? end - now : null;\n  const done = remaining !== null && remaining <= 0;\n\n  React.useEffect(() => {\n    if (done && !completedRef.current) {\n      completedRef.current = true;\n      onCompleteRef.current?.();\n    }\n    if (!done) completedRef.current = false;\n  }, [done]);\n\n  const sep = (\n    <span className={cn(\"self-start font-display font-medium text-muted-foreground/50\", sizes[size].sep, sizes[size].sepOffset)} aria-hidden=\"true\">\n      :\n    </span>\n  );\n\n  // Units are grouped in pairs (days + hours, minutes + seconds). Each pair never breaks, but the\n  // pairs may wrap to a 2x2 layout on narrow screens. From `sm` up it is one row, as before.\n  const rootCls = \"inline-flex flex-wrap items-start justify-center gap-x-5 gap-y-4 sm:flex-nowrap sm:gap-3\";\n  const pairCls = \"flex items-start gap-1.5 sm:gap-3\";\n\n  if (remaining === null) {\n    // Placeholder skeleton with stable layout before mount.\n    const skeleton = (\n      <div className=\"flex flex-col items-center gap-2\">\n        <div className={cn(\"flex\", sizes[size].gap)}>\n          <span className={cn(\"bg-foreground/[0.05]\", sizes[size].box)} />\n          <span className={cn(\"bg-foreground/[0.05]\", sizes[size].box)} />\n        </div>\n        <span className={cn(\"invisible\", sizes[size].label)}>00</span>\n      </div>\n    );\n    return (\n      <div className={cn(rootCls, className)} aria-hidden=\"true\">\n        <div className={pairCls}>\n          {skeleton}\n          {sep}\n          {skeleton}\n        </div>\n        <span className=\"hidden sm:contents\">{sep}</span>\n        <div className={pairCls}>\n          {skeleton}\n          {!hideSeconds && sep}\n          {!hideSeconds && skeleton}\n        </div>\n      </div>\n    );\n  }\n\n  if (done && completeContent) return <div className={className}>{completeContent}</div>;\n\n  const p = getParts(remaining);\n  const a11y = `${p.days} days, ${p.hours} hours, ${p.minutes} minutes${hideSeconds ? \"\" : `, ${p.seconds} seconds`} remaining`;\n\n  return (\n    <div className={cn(rootCls, className)} role=\"timer\" aria-label={a11y}>\n      <div className={pairCls}>\n        <Unit value={p.days} label={labels.days} size={size} pad={2} />\n        {sep}\n        <Unit value={p.hours} label={labels.hours} size={size} pad={2} />\n      </div>\n      {/* Middle colon only when the four groups sit in one row. */}\n      <span className=\"hidden sm:contents\">{sep}</span>\n      <div className={pairCls}>\n        <Unit value={p.minutes} label={labels.minutes} size={size} pad={2} />\n        {!hideSeconds && (\n          <>\n            {sep}\n            <Unit value={p.seconds} label={labels.seconds} size={size} pad={2} />\n          </>\n        )}\n      </div>\n    </div>\n  );\n}\n\nexport default Countdown;\n"}],"categories":["data"],"meta":{"tier":"free","demo":"\"use client\";\n\nimport * as React from \"react\";\nimport { Countdown } from \"./countdown\";\n\nexport default function Demo() {\n  // Target is computed after mount so the preview always shows a live countdown.\n  const [target, setTarget] = React.useState<number | undefined>(undefined);\n  React.useEffect(() => {\n    setTarget(Date.now() + ((2 * 24 + 14) * 3600 + 27 * 60 + 45) * 1000);\n  }, []);\n\n  return (\n    <div className=\"w-full max-w-[600px] rounded-[28px] bg-muted px-6 py-10 text-center sm:px-10 sm:py-12\">\n      <p className=\"text-[13px] font-medium uppercase tracking-[0.08em] text-muted-foreground\">Grand opening open house</p>\n      <h3 className=\"mt-2 font-display text-[28px] font-semibold tracking-[-0.03em] text-foreground sm:text-[32px]\">The Residences at Old Town</h3>\n      <p className=\"mt-2 text-[15px] text-muted-foreground\">Saturday, 11 AM to 3 PM. 7135 E Main St, Scottsdale.</p>\n      <div className=\"mt-8 flex justify-center\">\n        <Countdown\n          key={target === undefined ? \"pending\" : \"live\"}\n          target={target}\n          completeContent={<p className=\"font-display text-xl font-semibold text-foreground\">Doors are open. Come on in.</p>}\n        />\n      </div>\n      <a\n        href=\"#rsvp\"\n        className=\"mt-9 inline-flex h-11 items-center justify-center rounded-full bg-brand px-6 text-[15px] font-medium text-brand-foreground transition-colors hover:bg-brand/90 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-muted\"\n      >\n        RSVP for a private preview\n      </a>\n    </div>\n  );\n}\n"}}