{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "checkout-summary",
  "type": "registry:block",
  "title": "Checkout Summary",
  "description": "A checkout with generic express-pay buttons, delivery options that slide a selection ring, quantity steppers, a promo field that validates, and totals whose digits roll as anything changes.",
  "categories": [
    "block",
    "Apps"
  ],
  "dependencies": [
    "lucide-react",
    "motion"
  ],
  "registryDependencies": [
    "utils",
    "https://coss.com/ui/r/button.json",
    "https://coss.com/ui/r/input.json"
  ],
  "files": [
    {
      "path": "registry/devl/checkout-summary.tsx",
      "type": "registry:component",
      "content": "\"use client\";\n\nimport {\n  CalendarClockIcon,\n  CheckIcon,\n  LandmarkIcon,\n  LockIcon,\n  type LucideIcon,\n  MinusIcon,\n  PlusIcon,\n  RotateCcwIcon,\n  ShieldCheckIcon,\n  TagIcon,\n  TruckIcon,\n  WalletIcon,\n  XIcon,\n} from \"lucide-react\";\nimport { AnimatePresence, motion, useReducedMotion } from \"motion/react\";\nimport { type FormEvent, useId, useState } from \"react\";\nimport { Button } from \"@/components/ui/button\";\nimport { Input } from \"@/components/ui/input\";\nimport { cn } from \"@/lib/utils\";\n\nexport interface CheckoutItem {\n  id: string;\n  name: string;\n  variant?: string;\n  price: number;\n  quantity: number;\n  icon: LucideIcon;\n  tone?: string;\n}\n\nexport interface CheckoutShipping {\n  id: string;\n  label: string;\n  eta: string;\n  price: number;\n}\n\nexport interface CheckoutPromo {\n  code: string;\n  percent: number;\n}\n\nexport interface CheckoutExpress {\n  id: string;\n  label: string;\n  icon: LucideIcon;\n}\n\nexport interface CheckoutSummaryProps {\n  items: CheckoutItem[];\n  shipping: CheckoutShipping[];\n  express?: CheckoutExpress[];\n  taxRate?: number;\n  currency?: string;\n  email?: string;\n  validatePromo?: (code: string) => Promise<CheckoutPromo | null>;\n  onPay?: (total: number) => Promise<void>;\n  onExpress?: (method: CheckoutExpress) => void;\n  className?: string;\n}\n\nconst ease = [0.23, 1, 0.32, 1] as const;\nconst press = \"transition-[box-shadow,scale,background-color] duration-150 active:scale-[0.97]\";\nconst DEFAULT_EXPRESS: CheckoutExpress[] = [\n  { id: \"wallet\", label: \"Wallet\", icon: WalletIcon },\n  { id: \"later\", label: \"Pay later\", icon: CalendarClockIcon },\n  { id: \"bank\", label: \"Bank\", icon: LandmarkIcon },\n];\n\nfunction Digit({ value }: { value: number }) {\n  const reduce = useReducedMotion();\n  return (\n    <span aria-hidden className=\"relative inline-block h-[1.2em] w-[0.6em] overflow-hidden\">\n      <motion.span\n        animate={{ y: `${-value * 1.2}em` }}\n        className=\"absolute inset-x-0 top-0 flex flex-col items-center\"\n        initial={false}\n        transition={reduce ? { duration: 0 } : { type: \"spring\", stiffness: 260, damping: 30 }}\n      >\n        {Array.from({ length: 10 }, (_, n) => (\n          <span className=\"block h-[1.2em] leading-[1.2em]\" key={n}>\n            {n}\n          </span>\n        ))}\n      </motion.span>\n    </span>\n  );\n}\n\nfunction Money({ value, currency, className, decimals = 2 }: { value: number; currency: string; className?: string; decimals?: number }) {\n  const text = `${currency}${value.toLocaleString(\"en-US\", { minimumFractionDigits: decimals, maximumFractionDigits: decimals })}`;\n  const chars = text.split(\"\");\n  return (\n    <span className={cn(\"inline-flex items-start tabular-nums\", className)}>\n      <span className=\"sr-only\">{text}</span>\n      {chars.map((ch, i) => {\n        const key = chars.length - i;\n        return /\\d/.test(ch) ? (\n          <Digit key={`d${key}`} value={Number(ch)} />\n        ) : (\n          <span aria-hidden className=\"inline-block h-[1.2em] leading-[1.2em]\" key={`s${key}`}>\n            {ch}\n          </span>\n        );\n      })}\n    </span>\n  );\n}\n\nfunction plain(n: number, currency: string) {\n  return `${currency}${n.toLocaleString(\"en-US\", { minimumFractionDigits: 2, maximumFractionDigits: 2 })}`;\n}\n\nexport function CheckoutSummary({\n  items: initialItems,\n  shipping,\n  express = DEFAULT_EXPRESS,\n  taxRate = 0.0825,\n  currency = \"$\",\n  email = \"\",\n  validatePromo = async () => null,\n  onPay = async () => {},\n  onExpress,\n  className,\n}: CheckoutSummaryProps) {\n  const reduce = useReducedMotion();\n  const uid = useId();\n  const shipLayout = useId();\n  const [items, setItems] = useState(initialItems);\n  const [method, setMethod] = useState(shipping[0]?.id);\n  const [promoOpen, setPromoOpen] = useState(false);\n  const [code, setCode] = useState(\"\");\n  const [promo, setPromo] = useState<CheckoutPromo | null>(null);\n  const [promoError, setPromoError] = useState<string>();\n  const [checking, setChecking] = useState(false);\n  const [contact, setContact] = useState(email);\n  const [phase, setPhase] = useState<\"idle\" | \"paying\" | \"done\">(\"idle\");\n\n  const subtotal = items.reduce((s, i) => s + i.price * i.quantity, 0);\n  const discount = promo ? Math.round(subtotal * promo.percent * 100) / 100 : 0;\n  const ship = shipping.find((s) => s.id === method)?.price ?? 0;\n  const tax = Math.round((subtotal - discount) * taxRate * 100) / 100;\n  const total = Math.max(0, subtotal - discount + ship + tax);\n  const count = items.reduce((s, i) => s + i.quantity, 0);\n\n  const setQty = (id: string, q: number) =>\n    setItems((list) => list.flatMap((i) => (i.id === id ? (q <= 0 ? [] : [{ ...i, quantity: Math.min(9, q) }]) : [i])));\n\n  const apply = async (e: FormEvent) => {\n    e.preventDefault();\n    if (!code.trim()) return;\n    setChecking(true);\n    const found = await validatePromo(code.trim().toUpperCase());\n    setChecking(false);\n    if (found) {\n      setPromo(found);\n      setPromoOpen(false);\n      setCode(\"\");\n      setPromoError(undefined);\n    } else {\n      setPromoError(\"That code isn’t valid or has expired.\");\n    }\n  };\n\n  const pay = async () => {\n    setPhase(\"paying\");\n    await onPay(total);\n    setPhase(\"done\");\n  };\n\n  return (\n    <section className={cn(\"mx-auto grid w-full max-w-[1000px] items-start gap-5 md:grid-cols-[1fr_400px] md:gap-6\", className)}>\n      <div className=\"order-2 space-y-5 rounded-2xl border bg-card p-5 shadow-xs/5 sm:p-6 md:order-1\">\n        <div>\n          <h2 className=\"font-semibold text-base tracking-tight\">Express checkout</h2>\n          <div className=\"mt-3 grid grid-cols-3 gap-2\">\n            {express.map((m) => (\n              <Button className={cn(\"h-11 sm:h-10\", press)} key={m.id} onClick={() => onExpress?.(m)} variant=\"outline\">\n                <m.icon />\n                {m.label}\n              </Button>\n            ))}\n          </div>\n          <div className=\"mt-5 flex items-center gap-3 text-muted-foreground text-xs\">\n            <span className=\"h-px flex-1 bg-border\" />\n            or check out below\n            <span className=\"h-px flex-1 bg-border\" />\n          </div>\n        </div>\n\n        <div>\n          <label className=\"font-medium text-sm\" htmlFor={`${uid}-email`}>\n            Email for receipts\n          </label>\n          <Input\n            autoComplete=\"email\"\n            className=\"mt-2\"\n            id={`${uid}-email`}\n            onChange={(e) => setContact(e.target.value)}\n            placeholder=\"you@example.com\"\n            type=\"email\"\n            value={contact}\n          />\n        </div>\n\n        <fieldset>\n          <legend className=\"font-medium text-sm\">Delivery</legend>\n          <div className=\"mt-2 space-y-2\" role=\"radiogroup\">\n            {shipping.map((s) => {\n              const on = s.id === method;\n              return (\n                <label\n                  className={cn(\n                    \"relative flex cursor-pointer items-center gap-3 rounded-xl border px-3.5 py-3 transition-colors duration-150 has-focus-visible:ring-2 has-focus-visible:ring-ring\",\n                    on ? \"border-transparent\" : \"hover:bg-accent/40\",\n                  )}\n                  key={s.id}\n                >\n                  {on && (\n                    <motion.span\n                      aria-hidden\n                      className=\"absolute inset-0 rounded-xl border-2 border-foreground/80 bg-accent/40\"\n                      layoutId={shipLayout}\n                      transition={reduce ? { duration: 0 } : { type: \"spring\", stiffness: 500, damping: 40 }}\n                    />\n                  )}\n                  <input checked={on} className=\"sr-only\" name={`${uid}-ship`} onChange={() => setMethod(s.id)} type=\"radio\" value={s.id} />\n                  <span\n                    className={cn(\n                      \"relative flex size-4 shrink-0 items-center justify-center rounded-full border transition-colors duration-150\",\n                      on ? \"border-primary bg-primary\" : \"border-input\",\n                    )}\n                  >\n                    <motion.span\n                      animate={{ scale: on ? 1 : 0 }}\n                      className=\"size-1.5 rounded-full bg-primary-foreground\"\n                      initial={false}\n                      transition={{ duration: 0.15, ease }}\n                    />\n                  </span>\n                  <span className=\"relative min-w-0 flex-1\">\n                    <span className=\"block font-medium text-sm\">{s.label}</span>\n                    <span className=\"block text-muted-foreground text-xs\">{s.eta}</span>\n                  </span>\n                  <span className=\"relative font-medium text-sm tabular-nums\">{s.price === 0 ? \"Free\" : plain(s.price, currency)}</span>\n                </label>\n              );\n            })}\n          </div>\n        </fieldset>\n      </div>\n\n      <aside className=\"order-1 overflow-hidden rounded-2xl border bg-card shadow-xs/5 md:order-2\">\n        <div className=\"flex items-baseline justify-between px-5 pt-5 sm:px-6\">\n          <h2 className=\"font-semibold text-base tracking-tight\">Order summary</h2>\n          <span className=\"text-muted-foreground text-xs tabular-nums\">\n            {count} {count === 1 ? \"item\" : \"items\"}\n          </span>\n        </div>\n\n        <ul className=\"mt-3 px-5 sm:px-6\">\n          <AnimatePresence initial={false}>\n            {items.map((item) => (\n              <motion.li\n                animate={{ opacity: 1, height: \"auto\" }}\n                className=\"overflow-hidden\"\n                exit={{ opacity: 0, height: 0 }}\n                initial={false}\n                key={item.id}\n                transition={{ duration: reduce ? 0 : 0.22, ease }}\n              >\n                <div className=\"flex gap-3 border-b py-3\">\n                  <span\n                    className={cn(\n                      \"relative flex size-14 shrink-0 items-center justify-center rounded-xl\",\n                      item.tone ?? \"bg-muted text-foreground\",\n                    )}\n                  >\n                    <item.icon className=\"size-6\" strokeWidth={1.5} />\n                  </span>\n                  <div className=\"min-w-0 flex-1\">\n                    <div className=\"flex items-start justify-between gap-2\">\n                      <div className=\"min-w-0\">\n                        <p className=\"truncate font-medium text-sm\">{item.name}</p>\n                        {item.variant && <p className=\"truncate text-muted-foreground text-xs\">{item.variant}</p>}\n                      </div>\n                      <button\n                        aria-label={`Remove ${item.name}`}\n                        className=\"-me-1.5 -mt-1 flex size-7 shrink-0 cursor-pointer items-center justify-center rounded-md text-muted-foreground outline-none hover:bg-accent hover:text-foreground focus-visible:ring-2 focus-visible:ring-ring\"\n                        onClick={() => setQty(item.id, 0)}\n                        type=\"button\"\n                      >\n                        <XIcon className=\"size-3.5\" />\n                      </button>\n                    </div>\n                    <div className=\"mt-2 flex items-center justify-between\">\n                      <div className=\"flex items-center rounded-lg border\">\n                        <button\n                          aria-label={`One fewer ${item.name}`}\n                          className=\"flex size-7 cursor-pointer items-center justify-center rounded-s-lg text-muted-foreground outline-none hover:bg-accent hover:text-foreground focus-visible:ring-2 focus-visible:ring-ring active:scale-90\"\n                          onClick={() => setQty(item.id, item.quantity - 1)}\n                          type=\"button\"\n                        >\n                          <MinusIcon className=\"size-3\" />\n                        </button>\n                        <span aria-live=\"polite\" className=\"w-6 text-center text-sm\">\n                          <Money className=\"justify-center\" currency=\"\" decimals={0} value={item.quantity} />\n                        </span>\n                        <button\n                          aria-label={`One more ${item.name}`}\n                          className=\"flex size-7 cursor-pointer items-center justify-center rounded-e-lg text-muted-foreground outline-none hover:bg-accent hover:text-foreground focus-visible:ring-2 focus-visible:ring-ring active:scale-90 disabled:opacity-40\"\n                          disabled={item.quantity >= 9}\n                          onClick={() => setQty(item.id, item.quantity + 1)}\n                          type=\"button\"\n                        >\n                          <PlusIcon className=\"size-3\" />\n                        </button>\n                      </div>\n                      <Money className=\"font-medium text-sm\" currency={currency} value={item.price * item.quantity} />\n                    </div>\n                  </div>\n                </div>\n              </motion.li>\n            ))}\n          </AnimatePresence>\n        </ul>\n\n        {items.length === 0 && <p className=\"px-6 py-8 text-center text-muted-foreground text-sm\">Your bag is empty.</p>}\n\n        <div className=\"px-5 py-4 sm:px-6\">\n          <AnimatePresence initial={false} mode=\"popLayout\">\n            {promo ? (\n              <motion.div\n                animate={{ opacity: 1, scale: 1 }}\n                className=\"flex items-center justify-between gap-2\"\n                initial={{ opacity: 0, scale: 0.96 }}\n                key=\"applied\"\n                transition={{ duration: 0.18, ease }}\n              >\n                <span className=\"inline-flex items-center gap-1.5 rounded-full bg-success/10 py-1 ps-2 pe-1 font-medium font-mono text-success-foreground text-xs\">\n                  <TagIcon className=\"size-3\" />\n                  {promo.code}\n                  <button\n                    aria-label={`Remove code ${promo.code}`}\n                    className=\"flex size-5 cursor-pointer items-center justify-center rounded-full outline-none hover:bg-success/15 focus-visible:ring-2 focus-visible:ring-ring\"\n                    onClick={() => setPromo(null)}\n                    type=\"button\"\n                  >\n                    <XIcon className=\"size-3\" />\n                  </button>\n                </span>\n                <span className=\"text-muted-foreground text-xs\">{Math.round(promo.percent * 100)}% off your order</span>\n              </motion.div>\n            ) : promoOpen ? (\n              <motion.form\n                animate={{ opacity: 1 }}\n                initial={{ opacity: 0 }}\n                key=\"form\"\n                noValidate\n                onSubmit={apply}\n                transition={{ duration: 0.16 }}\n              >\n                <label className=\"sr-only\" htmlFor={`${uid}-promo`}>\n                  Promo code\n                </label>\n                <div className=\"flex gap-2\">\n                  <Input\n                    aria-describedby={promoError ? `${uid}-promo-err` : undefined}\n                    aria-invalid={promoError ? true : undefined}\n                    autoFocus\n                    className=\"flex-1 font-mono\"\n                    id={`${uid}-promo`}\n                    onChange={(e) => {\n                      setCode(e.target.value);\n                      setPromoError(undefined);\n                    }}\n                    placeholder=\"Promo code\"\n                    value={code}\n                  />\n                  <Button className={press} disabled={!code.trim()} loading={checking} type=\"submit\" variant=\"outline\">\n                    Apply\n                  </Button>\n                </div>\n                <AnimatePresence initial={false}>\n                  {promoError && (\n                    <motion.p\n                      animate={{ height: \"auto\", opacity: 1 }}\n                      className=\"overflow-hidden text-destructive-foreground text-xs\"\n                      exit={{ height: 0, opacity: 0 }}\n                      id={`${uid}-promo-err`}\n                      initial={{ height: 0, opacity: 0 }}\n                      transition={{ duration: 0.18, ease }}\n                    >\n                      <span className=\"block pt-1.5\">{promoError}</span>\n                    </motion.p>\n                  )}\n                </AnimatePresence>\n              </motion.form>\n            ) : (\n              <motion.button\n                animate={{ opacity: 1 }}\n                className=\"flex cursor-pointer items-center gap-1.5 rounded text-muted-foreground text-sm outline-none hover:text-foreground focus-visible:ring-2 focus-visible:ring-ring\"\n                initial={{ opacity: 0 }}\n                key=\"link\"\n                onClick={() => setPromoOpen(true)}\n                type=\"button\"\n              >\n                <TagIcon className=\"size-3.5\" />\n                Add a promo code\n              </motion.button>\n            )}\n          </AnimatePresence>\n        </div>\n\n        <dl className=\"space-y-2 border-t bg-muted/30 px-5 py-4 text-sm sm:px-6\">\n          <div className=\"flex justify-between\">\n            <dt className=\"text-muted-foreground\">Subtotal</dt>\n            <dd>\n              <Money currency={currency} value={subtotal} />\n            </dd>\n          </div>\n          <AnimatePresence initial={false}>\n            {promo && (\n              <motion.div\n                animate={{ height: \"auto\", opacity: 1 }}\n                className=\"overflow-hidden\"\n                exit={{ height: 0, opacity: 0 }}\n                initial={{ height: 0, opacity: 0 }}\n                transition={{ duration: reduce ? 0 : 0.2, ease }}\n              >\n                <div className=\"flex justify-between text-success-foreground\">\n                  <dt>Discount</dt>\n                  <dd>\n                    <Money currency={`−${currency}`} value={discount} />\n                  </dd>\n                </div>\n              </motion.div>\n            )}\n          </AnimatePresence>\n          <div className=\"flex justify-between\">\n            <dt className=\"text-muted-foreground\">Shipping</dt>\n            <dd>{ship === 0 ? <span className=\"font-medium text-success-foreground\">Free</span> : <Money currency={currency} value={ship} />}</dd>\n          </div>\n          <div className=\"flex justify-between\">\n            <dt className=\"text-muted-foreground\">Estimated tax</dt>\n            <dd>\n              <Money currency={currency} value={tax} />\n            </dd>\n          </div>\n          <div className=\"flex items-baseline justify-between border-t pt-3\">\n            <dt className=\"font-medium\">Total</dt>\n            <dd className=\"font-semibold text-xl tracking-tight\">\n              <Money currency={currency} value={total} />\n            </dd>\n          </div>\n        </dl>\n\n        <div className=\"px-5 pt-1 pb-5 sm:px-6\">\n          <Button\n            className={cn(\"w-full\", press, phase === \"done\" && \"disabled:opacity-100\")}\n            disabled={!items.length || phase !== \"idle\"}\n            loading={phase === \"paying\"}\n            onClick={pay}\n            size=\"lg\"\n          >\n            {phase === \"done\" ? (\n              <>\n                <CheckIcon />\n                Order placed\n              </>\n            ) : (\n              <>\n                <LockIcon />\n                Pay {plain(total, currency)}\n              </>\n            )}\n          </Button>\n          <ul className=\"mt-4 grid grid-cols-3 gap-2 text-center text-[0.6875rem] text-muted-foreground\">\n            {[\n              { icon: ShieldCheckIcon, label: \"Encrypted checkout\" },\n              { icon: RotateCcwIcon, label: \"30-day returns\" },\n              { icon: TruckIcon, label: \"Tracked delivery\" },\n            ].map(({ icon: Icon, label }) => (\n              <li className=\"flex flex-col items-center gap-1\" key={label}>\n                <Icon className=\"size-4\" />\n                {label}\n              </li>\n            ))}\n          </ul>\n        </div>\n      </aside>\n    </section>\n  );\n}\n"
    }
  ]
}