{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "promo-banner",
  "title": "Promo Banner",
  "description": "Promo banner with a countdown timer for React and shadcn: digits roll down each second, the code copies, and it folds away at zero.",
  "dependencies": [
    "motion"
  ],
  "registryDependencies": [
    "https://eased.dev/r/eased-tokens.json",
    "https://eased.dev/r/eased-motion.json",
    "https://eased.dev/r/eased-number-roll.json",
    "https://eased.dev/r/eased-auto-height.json",
    "https://eased.dev/r/eased-copy.json",
    "https://eased.dev/r/eased-text-swap.json"
  ],
  "files": [
    {
      "path": "src/registry/eased/promo-banner/promo-banner.tsx",
      "content": "\"use client\";\n\nimport { useInView } from \"motion/react\";\nimport { useEffect, useRef, useState, type ReactNode } from \"react\";\nimport { AutoHeight } from \"../_primitives/auto-height/auto-height\";\nimport { CopyIcon, useCopy } from \"../_primitives/copy/copy\";\nimport { NumberRoll } from \"../_primitives/number-roll/number-roll\";\nimport { TextSwap } from \"../_primitives/text-swap/text-swap\";\nimport { useSafeReducedMotion } from \"../lib/motion\";\nimport { pressIcon } from \"../lib/press\";\n\n/* ---------- Countdown -------------------------------------------------------------------- */\n\nexport type CountdownParts = { days: number; hours: number; minutes: number; seconds: number; total: number };\n\nconst split = (ms: number): CountdownParts => {\n  const t = Math.max(0, Math.ceil(ms / 1000));\n  return { days: Math.floor(t / 86_400), hours: Math.floor(t / 3600) % 24, minutes: Math.floor(t / 60) % 60, seconds: t % 60, total: t };\n};\n\n/**\n * Seconds left until `endsAt`. `now` pins the first render (server and client agree), and the\n * clock then runs on real elapsed time from there. It ticks on the second boundary while the\n * element is on screen and catches up when it comes back, so an off-screen banner costs nothing.\n */\nexport function useCountdown(endsAt: number, { now, target }: { now?: number; target?: React.RefObject<Element | null> } = {}) {\n  const [left, setLeft] = useState<number | null>(now === undefined ? null : endsAt - now);\n  const none = useRef<Element | null>(null);\n  const inView = useInView(target ?? none, { margin: \"64px\" });\n  const active = target ? inView : true;\n  // Offset between the pinned now and the machine clock, fixed on the first client tick.\n  const offset = useRef<number | null>(null);\n  const pinned = useRef(now);\n  useEffect(() => {\n    if (!active) return;\n    if (offset.current === null) offset.current = pinned.current === undefined ? 0 : pinned.current - Date.now();\n    const skew = offset.current;\n    let timer = 0;\n    const tick = () => {\n      const ms = endsAt - (Date.now() + skew);\n      setLeft(ms);\n      if (ms > 0) timer = window.setTimeout(tick, ((ms % 1000) + 1000) % 1000 || 1000);\n    };\n    tick();\n    return () => window.clearTimeout(timer);\n  }, [endsAt, active]);\n  return left === null ? null : split(left);\n}\n\n/* ---------- Banner ----------------------------------------------------------------------- */\n\nexport type PromoBannerProps = {\n  /** When the offer ends, epoch ms. */\n  endsAt: number;\n  /** Pin \"now\" for the first render (SSR, demos). Default: the machine clock after hydration. */\n  now?: number;\n  headline: ReactNode;\n  detail?: ReactNode;\n  /** A discount code, copyable from the banner. */\n  code?: string;\n  /** A trailing action, e.g. a \"Shop the sale\" Button or link. */\n  action?: ReactNode;\n  /** `bar`: a one-line announcement bar. `card`: a hero card with digit tiles. Default \"bar\". */\n  variant?: \"bar\" | \"card\";\n  /** The bar's fill. `neutral` (ink on the canvas colour) is calmer when the page already has a brand CTA. Default \"brand\". */\n  tone?: \"brand\" | \"neutral\";\n  /** Collapse the banner when the clock reaches zero. Default true. */\n  collapseOnExpire?: boolean;\n  /** Fires once at zero. */\n  onExpire?: () => void;\n  /** Fires when the collapse has settled, to unmount or swap in the next offer. */\n  onCollapsed?: () => void;\n  locale?: Intl.LocalesArgument;\n  className?: string;\n};\n\nconst UNITS = [\n  { key: \"days\", short: \"d\", long: \"Days\" },\n  { key: \"hours\", short: \"h\", long: \"Hours\" },\n  { key: \"minutes\", short: \"m\", long: \"Min\" },\n  { key: \"seconds\", short: \"s\", long: \"Sec\" },\n] as const;\n\nconst spokenLeft = (p: CountdownParts) => {\n  const bits = [p.days && `${p.days} day${p.days === 1 ? \"\" : \"s\"}`, p.hours && `${p.hours} hour${p.hours === 1 ? \"\" : \"s\"}`, `${p.minutes} minute${p.minutes === 1 ? \"\" : \"s\"}`].filter(Boolean);\n  return p.total < 60 ? \"less than a minute\" : bits.join(\" \");\n};\n\n/**\n * A time-boxed offer: digits roll down each second, and at zero the banner folds away on a\n * height spring so the page below glides up instead of jumping. The code copies with a morph\n * to a check. Screen readers get the time left to the minute, not a tick every second.\n */\nexport function PromoBanner({\n  endsAt,\n  now,\n  headline,\n  detail,\n  code,\n  action,\n  variant = \"bar\",\n  tone = \"brand\",\n  collapseOnExpire = true,\n  onExpire,\n  onCollapsed,\n  locale,\n  className = \"\",\n}: PromoBannerProps) {\n  const root = useRef<HTMLDivElement>(null);\n  const reduced = useSafeReducedMotion();\n  const parts = useCountdown(endsAt, { now, target: root });\n  const expired = parts !== null && parts.total === 0;\n  const fired = useRef(false);\n  const latest = useRef(onExpire);\n  useEffect(() => {\n    latest.current = onExpire;\n  });\n  useEffect(() => {\n    if (expired && !fired.current) {\n      fired.current = true;\n      latest.current?.();\n    }\n    if (!expired) fired.current = false;\n  }, [expired]);\n\n  const units = UNITS.filter((u) => u.key !== \"days\" || (parts?.days ?? 0) > 0);\n  const two = { minimumIntegerDigits: 2, useGrouping: false } as const;\n  const digit = (v: number | undefined, i: number) =>\n    v === undefined ? \"––\" : <NumberRoll value={v} locale={locale} format={i === 0 && units[0].key === \"days\" ? { useGrouping: false } : two} direction=\"down\" animated={!reduced} aria-hidden />;\n\n  const timer = (\n    <span role=\"timer\" aria-live=\"off\" className=\"sr-only\">\n      {parts ? (expired ? \"Offer ended\" : `Ends in ${spokenLeft(parts)}`) : \"\"}\n    </span>\n  );\n\n  return (\n    <AutoHeight open={!(collapseOnExpire && expired)} onOpenChangeComplete={(open) => !open && onCollapsed?.()} className={className}>\n      <div ref={root}>\n      {variant === \"bar\" ? (\n        <div className={`flex min-h-10 flex-wrap items-center justify-center gap-x-3 gap-y-1 px-(--pad,16px) py-2 text-ui-sm ${tone === \"neutral\" ? \"bg-text text-bg\" : \"bg-brand text-text-on-brand\"}`}>\n          {timer}\n          <p className=\"min-w-0 text-center text-pretty\">\n            <span className=\"font-medium\">{headline}</span>\n            {detail && <span className=\"opacity-80\"> · {detail}</span>}\n          </p>\n          <span aria-hidden className=\"inline-flex items-center gap-1.5 font-medium tabular-nums\">\n            <span className=\"opacity-80\">Ends in</span>\n            <span className=\"inline-flex\">\n              {units.map((u, i) => (\n                <span key={u.key} className=\"inline-flex\">\n                  {i > 0 && (u.key === \"hours\" && units[0].key === \"days\" ? \" \" : \":\")}\n                  {digit(parts?.[u.key], i)}\n                  {u.key === \"days\" && \"d\"}\n                </span>\n              ))}\n            </span>\n          </span>\n          {code && <CodeChip code={code} tone=\"bar\" />}\n          {action}\n        </div>\n      ) : (\n        <div className=\"flex flex-col gap-4 rounded-panel border border-line-subtle bg-surface p-(--card-pad,20px) [--card-pad:20px] [--pad:var(--card-pad)] [--radius-outer:var(--eased-radius-panel)]\">\n          {timer}\n          <div className=\"flex flex-col gap-1\">\n            <p className=\"text-ui-md font-medium text-balance text-text\">{headline}</p>\n            {detail && <p className=\"text-ui-sm text-pretty text-text-secondary\">{detail}</p>}\n          </div>\n          <div aria-hidden className=\"grid gap-2\" style={{ gridTemplateColumns: `repeat(${units.length}, minmax(0, 1fr))` }}>\n            {units.map((u, i) => (\n              <div key={u.key} className=\"flex flex-col items-center gap-0.5 rounded-control-sm bg-surface-sunken py-2.5 inset-shadow-well\">\n                <span className=\"text-2xl font-medium text-text tabular-nums\">{digit(parts?.[u.key], i)}</span>\n                <span className=\"text-ui-2xs tracking-caps text-text-muted uppercase\">{u.long}</span>\n              </div>\n            ))}\n          </div>\n          {(code || action) && (\n            <div className=\"flex flex-wrap items-center justify-between gap-3\">\n              {code && <CodeChip code={code} tone=\"card\" />}\n              {action}\n            </div>\n          )}\n        </div>\n      )}\n      </div>\n    </AutoHeight>\n  );\n}\n\n/** The code as a chip: press copies it; the label swaps to \"Copied\" and the icon draws a check. */\nfunction CodeChip({ code, tone }: { code: string; tone: \"bar\" | \"card\" }) {\n  const { copied, copy } = useCopy(code);\n  return (\n    <button\n      type=\"button\"\n      onClick={() => void copy()}\n      aria-label={`Copy code ${code}`}\n      // On the card it shares a row with the action, so it takes the action's 32px height (rule 52).\n      className={`inline-flex shrink-0 cursor-pointer items-center gap-1.5 border border-dashed font-mono text-ui-xs font-medium slashed-zero ${pressIcon} ${\n        tone === \"bar\" ? \"h-control-xs rounded-control-xs border-current/40 px-2 hover-fine:bg-current/10\" : \"h-control-sm rounded-control-sm border-line-strong px-3 text-text hover-fine:bg-surface-hover\"\n      }`}\n    >\n      <TextSwap>{copied ? \"Copied\" : code}</TextSwap>\n      <CopyIcon copied={copied} className=\"size-3.5! [&_svg]:size-3.5\" />\n      <span aria-live=\"polite\" className=\"sr-only\">\n        {copied ? `Code ${code} copied` : \"\"}\n      </span>\n    </button>\n  );\n}\n",
      "type": "registry:ui",
      "target": "@components/eased/promo-banner/promo-banner.tsx"
    }
  ],
  "meta": {
    "details": "A time-boxed offer as a bar or a card: the countdown's digits roll down each second, the code copies with a check, and at zero the banner folds away so the page glides up.",
    "industries": [
      "ecommerce"
    ],
    "releaseAt": "2026-12-06"
  },
  "docs": "Import PromoBanner from components/eased/promo-banner/promo-banner. Pass endsAt (epoch ms), headline, detail and code; pin now for SSR; variant is bar or card.",
  "type": "registry:ui"
}