{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "coupon-code-block",
  "title": "Coupon Code Block",
  "description": "A discount code card: type a code (validated with a spinner, shaking with a clear message when invalid or below its minimum spend) or pick one of the available coupon tickets, with a 'Best value' tag, locked coupons that say how much more to add, an applied ticket with a confetti pop, and totals that count down.",
  "dependencies": [
    "motion",
    "clsx",
    "tailwind-merge"
  ],
  "files": [
    {
      "path": "registry/new-york/coupon-code-block/coupon-code-block.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\nimport { AnimatePresence, animate, motion, useMotionValue, useReducedMotion, useTransform } from \"motion/react\";\nimport { clsx, type ClassValue } from \"clsx\";\nimport { twMerge } from \"tailwind-merge\";\n\nfunction cn(...inputs: ClassValue[]) {\n  return twMerge(clsx(inputs));\n}\n\nexport interface Coupon {\n  code: string;\n  label: string;\n  kind: \"percent\" | \"fixed\" | \"shipping\";\n  value: number;\n  minSpend?: number;\n  expires?: string;\n}\n\nexport interface CouponCodeBlockProps {\n  subtotal?: number;\n  shipping?: number;\n  coupons?: Coupon[];\n  currency?: string;\n  /** Validate a typed code. Return the coupon or null. Defaults to looking it up in coupons. */\n  onValidate?: (code: string) => Coupon | null | Promise<Coupon | null>;\n  onApply?: (coupon: Coupon | null) => void;\n  theme?: \"dark\" | \"light\";\n  className?: string;\n}\n\nconst PALETTES = {\n  dark: { bg: \"#0A0A0A\", title: \"#FFFFFF\", muted: \"rgba(255,255,255,0.45)\", body: \"rgba(255,255,255,0.62)\", line: \"rgba(255,255,255,0.12)\", card: \"#141414\", ticket: \"#1B1B1B\", input: \"#0F0F0F\", buttonBg: \"#FFFFFF\", buttonText: \"#111111\", accent: \"#7CDE6A\", error: \"#FF6B57\", ring: \"rgba(255,255,255,0.5)\" },\n  light: { bg: \"#FFFFFF\", title: \"#111111\", muted: \"rgba(0,0,0,0.45)\", body: \"rgba(0,0,0,0.62)\", line: \"rgba(0,0,0,0.1)\", card: \"#F7F7F6\", ticket: \"#FFFFFF\", input: \"#FFFFFF\", buttonBg: \"#111111\", buttonText: \"#FFFFFF\", accent: \"#2F9E44\", error: \"#D9480F\", ring: \"rgba(0,0,0,0.45)\" },\n};\nconst COUPONS: Coupon[] = [\n  { code: \"WELCOME10\", label: \"10% off your first order\", kind: \"percent\", value: 10, expires: \"Dec 31\" },\n  { code: \"SAVE25\", label: \"$25 off orders over $200\", kind: \"fixed\", value: 25, minSpend: 200, expires: \"Nov 30\" },\n  { code: \"FREESHIP\", label: \"Free express shipping\", kind: \"shipping\", value: 0, minSpend: 75 },\n];\n\nfunction Money({ value, currency, reduce }: { value: number; currency: string; reduce: boolean }) {\n  const mv = useMotionValue(value);\n  const text = useTransform(mv, (v) => `${currency}${v.toFixed(2)}`);\n  React.useEffect(() => {\n    if (reduce) {\n      mv.set(value);\n      return;\n    }\n    const c = animate(mv, value, { duration: 0.5, ease: [0.22, 1, 0.36, 1] });\n    return () => c.stop();\n  }, [value, reduce, mv]);\n  return <motion.span>{text}</motion.span>;\n}\n\nexport function CouponCodeBlock({ subtotal = 168, shipping = 12, coupons = COUPONS, currency = \"$\", onValidate, onApply, theme = \"dark\", className }: CouponCodeBlockProps) {\n  const p = PALETTES[theme];\n  const reduce = !!useReducedMotion();\n  const uid = React.useId();\n  const [input, setInput] = React.useState(\"\");\n  const [applied, setApplied] = React.useState<Coupon | null>(null);\n  const [error, setError] = React.useState<string | null>(null);\n  const [checking, setChecking] = React.useState(false);\n  const [shake, setShake] = React.useState(0);\n  const [burst, setBurst] = React.useState(0);\n\n  const money = (n: number) => `${currency}${n.toFixed(2)}`;\n  const discountOf = (c: Coupon | null) => (!c ? 0 : c.kind === \"percent\" ? subtotal * (c.value / 100) : c.kind === \"fixed\" ? Math.min(c.value, subtotal) : 0);\n  const shipOf = (c: Coupon | null) => (c?.kind === \"shipping\" ? 0 : shipping);\n  const discount = discountOf(applied);\n  const ship = shipOf(applied);\n  const total = subtotal - discount + ship;\n  const best = coupons.filter((c) => !c.minSpend || subtotal >= c.minSpend).sort((a, b) => discountOf(b) + (shipping - shipOf(b)) - (discountOf(a) + (shipping - shipOf(a))))[0];\n\n  const apply = async (raw: string) => {\n    const code = raw.trim().toUpperCase();\n    if (!code || checking) return;\n    setChecking(true);\n    setError(null);\n    try {\n      const c = onValidate ? await onValidate(code) : await new Promise<Coupon | null>((r) => setTimeout(() => r(coupons.find((x) => x.code === code) ?? null), reduce ? 0 : 600));\n      if (!c) {\n        setError(`“${code}” isn't a valid code`);\n        setShake((s) => s + 1);\n      } else if (c.minSpend && subtotal < c.minSpend) {\n        setError(`${c.code} needs ${money(c.minSpend)} in your bag. Add ${money(c.minSpend - subtotal)} more.`);\n        setShake((s) => s + 1);\n      } else {\n        setApplied(c);\n        setInput(\"\");\n        setBurst((b) => b + 1);\n        onApply?.(c);\n      }\n    } finally {\n      setChecking(false);\n    }\n  };\n  const remove = () => {\n    setApplied(null);\n    onApply?.(null);\n  };\n\n  const ring = { [\"--tw-ring-color\" as string]: p.ring, [\"--tw-ring-offset-color\" as string]: p.card } as React.CSSProperties;\n  const focus = \"outline-none focus-visible:ring-2 focus-visible:ring-offset-2\";\n\n  return (\n    <section className={cn(\"@container w-full\", className)} style={{ background: p.bg, color: p.title }}>\n      <div className=\"mx-auto max-w-[520px] px-4 py-10\">\n        <div className=\"rounded-3xl border p-5\" style={{ background: p.card, borderColor: p.line }}>\n          <h2 className=\"m-0 text-[17px] font-semibold\">Discount code</h2>\n\n          <AnimatePresence mode=\"popLayout\" initial={false}>\n            {applied ? (\n              <motion.div key=\"applied\" initial={reduce ? false : { opacity: 0, scale: 0.94, y: 6 }} animate={{ opacity: 1, scale: 1, y: 0 }} exit={reduce ? undefined : { opacity: 0, scale: 0.96 }} transition={{ type: \"spring\", stiffness: 380, damping: 26 }} className=\"relative mt-3\" role=\"status\">\n                <div className=\"relative flex items-center gap-3 overflow-hidden rounded-2xl border border-dashed px-4 py-3.5\" style={{ borderColor: p.accent, background: `${p.accent}14` }}>\n                  <span className=\"absolute top-1/2 -left-2 h-4 w-4 -translate-y-1/2 rounded-full\" style={{ background: p.card }} aria-hidden=\"true\" />\n                  <span className=\"absolute top-1/2 -right-2 h-4 w-4 -translate-y-1/2 rounded-full\" style={{ background: p.card }} aria-hidden=\"true\" />\n                  <span className=\"grid h-9 w-9 shrink-0 place-items-center rounded-full text-[15px] font-bold text-[#0A0A0A]\" style={{ background: p.accent }} aria-hidden=\"true\">\n                    ✓\n                  </span>\n                  <span className=\"min-w-0 flex-1\">\n                    <span className=\"block font-mono text-[14px] font-semibold tracking-[0.06em]\">{applied.code}</span>\n                    <span className=\"block text-[12.5px]\" style={{ color: p.body }}>\n                      {applied.label} · {applied.kind === \"shipping\" ? `saves ${money(shipping)}` : `−${money(discount)}`}\n                    </span>\n                  </span>\n                  <button type=\"button\" onClick={remove} className={cn(\"cursor-pointer border-0 bg-transparent p-0 text-[12.5px] underline-offset-2 hover:underline\", focus)} style={{ color: p.body, ...ring }}>\n                    Remove\n                  </button>\n                </div>\n                {!reduce && (\n                  <span key={burst} className=\"pointer-events-none absolute top-1/2 left-8\" aria-hidden=\"true\">\n                    {Array.from({ length: 12 }, (_, k) => {\n                      const a = (k / 12) * Math.PI * 2;\n                      return <motion.span key={k} className=\"absolute h-1.5 w-1.5 rounded-sm\" style={{ background: k % 3 ? p.accent : \"#FBBF24\" }} initial={{ x: 0, y: 0, opacity: 1, rotate: 0 }} animate={{ x: Math.cos(a) * 60, y: Math.sin(a) * 36, opacity: 0, rotate: 180 }} transition={{ duration: 0.8, ease: \"easeOut\" }} />;\n                    })}\n                  </span>\n                )}\n              </motion.div>\n            ) : (\n              <motion.form\n                key=\"form\"\n                initial={reduce ? false : { opacity: 0 }}\n                animate={{ opacity: 1, x: shake && !reduce ? [0, -8, 8, -5, 5, 0] : 0 }}\n                transition={{ duration: 0.4 }}\n                exit={reduce ? undefined : { opacity: 0 }}\n                className=\"mt-3\"\n                onSubmit={(e) => {\n                  e.preventDefault();\n                  apply(input);\n                }}\n              >\n                <div className=\"flex gap-2\">\n                  <label htmlFor={`${uid}-code`} className=\"sr-only\">\n                    Discount code\n                  </label>\n                  <input\n                    id={`${uid}-code`}\n                    value={input}\n                    onChange={(e) => {\n                      setInput(e.target.value.toUpperCase().replace(/\\s/g, \"\").slice(0, 20));\n                      setError(null);\n                    }}\n                    placeholder=\"Enter code\"\n                    autoComplete=\"off\"\n                    aria-invalid={!!error || undefined}\n                    aria-describedby={error ? `${uid}-err` : undefined}\n                    className=\"h-12 min-w-0 flex-1 rounded-xl border px-4 font-mono text-[14px] tracking-[0.06em] outline-none placeholder:font-sans placeholder:tracking-normal focus-visible:ring-2\"\n                    style={{ background: p.input, borderColor: error ? p.error : p.line, color: p.title, ...ring }}\n                  />\n                  <button type=\"submit\" disabled={!input.trim() || checking} className={cn(\"flex h-12 w-[96px] shrink-0 cursor-pointer items-center justify-center rounded-xl border-0 text-[14px] font-semibold disabled:cursor-not-allowed disabled:opacity-40\", focus)} style={{ background: p.buttonBg, color: p.buttonText, ...ring }}>\n                    {checking ? <motion.span className=\"h-4 w-4 rounded-full border-2 border-current border-t-transparent\" animate={reduce ? undefined : { rotate: 360 }} transition={{ repeat: Infinity, duration: 0.7, ease: \"linear\" }} aria-label=\"Checking\" /> : \"Apply\"}\n                  </button>\n                </div>\n                <AnimatePresence initial={false}>\n                  {error && (\n                    <motion.p key={error} id={`${uid}-err`} role=\"alert\" initial={reduce ? false : { opacity: 0, height: 0 }} animate={{ opacity: 1, height: \"auto\" }} exit={reduce ? undefined : { opacity: 0, height: 0 }} className=\"m-0 overflow-hidden pt-2 text-[12.5px]\" style={{ color: p.error }}>\n                      {error}\n                    </motion.p>\n                  )}\n                </AnimatePresence>\n              </motion.form>\n            )}\n          </AnimatePresence>\n\n          <div className=\"mt-5\">\n            <p className=\"m-0 text-[12px] font-medium tracking-[0.08em] uppercase\" style={{ color: p.muted }}>\n              Available for you\n            </p>\n            <ul className=\"m-0 mt-2.5 flex list-none flex-col gap-2 p-0\">\n              {coupons.map((c) => {\n                const locked = !!c.minSpend && subtotal < c.minSpend;\n                const isApplied = applied?.code === c.code;\n                const isBest = best?.code === c.code && !applied;\n                return (\n                  <li key={c.code}>\n                    <div className=\"relative flex items-stretch overflow-hidden rounded-2xl border\" style={{ borderColor: isApplied ? p.accent : p.line, background: p.ticket, opacity: locked ? 0.6 : 1 }}>\n                      <div className=\"flex w-[84px] shrink-0 flex-col items-center justify-center border-r border-dashed px-2 py-3 text-center\" style={{ borderColor: p.line }}>\n                        <span className=\"text-[19px] leading-none font-semibold tracking-[-0.02em]\">{c.kind === \"percent\" ? `${c.value}%` : c.kind === \"fixed\" ? `${currency}${c.value}` : \"Free\"}</span>\n                        <span className=\"mt-1 text-[10.5px] tracking-[0.08em] uppercase\" style={{ color: p.muted }}>\n                          {c.kind === \"shipping\" ? \"shipping\" : \"off\"}\n                        </span>\n                      </div>\n                      <div className=\"flex min-w-0 flex-1 items-center gap-3 px-3.5 py-3\">\n                        <span className=\"min-w-0 flex-1\">\n                          <span className=\"flex items-center gap-2\">\n                            <span className=\"font-mono text-[13px] font-semibold tracking-[0.06em]\">{c.code}</span>\n                            {isBest && (\n                              <span className=\"rounded-full px-2 py-0.5 text-[10.5px] font-semibold whitespace-nowrap text-[#0A0A0A]\" style={{ background: p.accent }}>\n                                Best value\n                              </span>\n                            )}\n                          </span>\n                          <span className=\"block truncate text-[12.5px]\" style={{ color: p.body }}>\n                            {c.label}\n                          </span>\n                          <span className=\"block text-[11.5px]\" style={{ color: locked ? p.error : p.muted }}>\n                            {locked ? `Add ${money(c.minSpend! - subtotal)} to unlock` : c.expires ? `Ends ${c.expires}` : \"No expiry\"}\n                          </span>\n                        </span>\n                        <button type=\"button\" disabled={locked || isApplied || checking} onClick={() => apply(c.code)} aria-label={isApplied ? `${c.code} applied` : `Apply ${c.code}`} className={cn(\"h-8 shrink-0 cursor-pointer rounded-full border px-3.5 text-[12.5px] font-semibold disabled:cursor-default\", focus)} style={{ borderColor: isApplied ? p.accent : p.line, background: isApplied ? p.accent : \"transparent\", color: isApplied ? \"#0A0A0A\" : p.title, ...ring }}>\n                          {isApplied ? \"Applied\" : locked ? \"Locked\" : \"Apply\"}\n                        </button>\n                      </div>\n                    </div>\n                  </li>\n                );\n              })}\n            </ul>\n          </div>\n\n          <dl className=\"m-0 mt-5 flex flex-col gap-2 border-t pt-4 text-[13.5px]\" style={{ borderColor: p.line }}>\n            <div className=\"flex justify-between\">\n              <dt style={{ color: p.body }}>Subtotal</dt>\n              <dd className=\"m-0 tabular-nums\">{money(subtotal)}</dd>\n            </div>\n            <AnimatePresence initial={false}>\n              {discount > 0 && (\n                <motion.div key=\"d\" initial={reduce ? false : { opacity: 0, height: 0 }} animate={{ opacity: 1, height: \"auto\" }} exit={reduce ? undefined : { opacity: 0, height: 0 }} className=\"flex justify-between overflow-hidden\">\n                  <dt style={{ color: p.body }}>Discount</dt>\n                  <dd className=\"m-0 tabular-nums\" style={{ color: p.accent }}>\n                    −{money(discount)}\n                  </dd>\n                </motion.div>\n              )}\n            </AnimatePresence>\n            <div className=\"flex justify-between\">\n              <dt style={{ color: p.body }}>Shipping</dt>\n              <dd className=\"m-0 tabular-nums\">\n                {ship === 0 && shipping > 0 ? (\n                  <span>\n                    <span className=\"mr-1.5 line-through\" style={{ color: p.muted }}>\n                      {money(shipping)}\n                    </span>\n                    <span style={{ color: p.accent }}>Free</span>\n                  </span>\n                ) : (\n                  money(ship)\n                )}\n              </dd>\n            </div>\n            <div className=\"mt-1 flex items-baseline justify-between border-t pt-3\" style={{ borderColor: p.line }}>\n              <dt className=\"text-[15px] font-semibold\">Total</dt>\n              <dd className=\"m-0 text-[22px] font-semibold tracking-[-0.02em] tabular-nums\">\n                <Money value={total} currency={currency} reduce={reduce} />\n              </dd>\n            </div>\n          </dl>\n        </div>\n      </div>\n    </section>\n  );\n}\n\nexport default CouponCodeBlock;\n",
      "type": "registry:component"
    }
  ],
  "type": "registry:component"
}