{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "upsell-cross-sell-block",
  "title": "Upsell / Cross-sell Block",
  "description": "A post-add offer screen: choose between your pick and a highlighted upgrade with a side-by-side spec table that follows the choice, add accessories at a bundle discount with struck-through prices, and watch the total and savings update before adding everything to the cart.",
  "dependencies": [
    "motion",
    "clsx",
    "tailwind-merge"
  ],
  "files": [
    {
      "path": "registry/new-york/upsell-cross-sell-block/upsell-cross-sell-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 UpsellProduct {\n  id: string;\n  title: string;\n  price: number;\n  image?: string;\n  specs: Record<string, string>;\n  tag?: string;\n}\n\nexport interface CrossSellProduct {\n  id: string;\n  title: string;\n  note: string;\n  price: number;\n  image?: string;\n}\n\nexport interface UpsellCrossSellBlockProps {\n  current?: UpsellProduct;\n  upgrade?: UpsellProduct;\n  crossSells?: CrossSellProduct[];\n  /** Percent off the cross-sells when bought together with the main product. */\n  bundleDiscount?: number;\n  currency?: string;\n  onAddToCart?: (items: { id: string; price: number }[]) => void | Promise<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\", cardOn: \"#1C1C1C\", photo: \"#F1F1F1\", buttonBg: \"#FFFFFF\", buttonText: \"#111111\", accent: \"#7CDE6A\", 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\", cardOn: \"#FFFFFF\", photo: \"#F1F1F1\", buttonBg: \"#111111\", buttonText: \"#FFFFFF\", accent: \"#2F9E44\", ring: \"rgba(0,0,0,0.45)\" },\n};\nconst CURRENT: UpsellProduct = { id: \"earbuds\", title: \"Wireless Earbuds\", price: 129, image: \"/demo/wireless-earbuds.webp\", specs: { \"Noise cancelling\": \"Active\", Battery: \"8h (30h case)\", Comfort: \"In-ear\", Sound: \"10 mm drivers\", Calls: \"3 mics\" } };\nconst UPGRADE: UpsellProduct = { id: \"headphones\", title: \"Noise Cancelling Headphones\", price: 199, image: \"/demo/blocks/product-list/headphones.webp\", tag: \"Most chosen upgrade\", specs: { \"Noise cancelling\": \"Adaptive\", Battery: \"40h\", Comfort: \"Over-ear memory foam\", Sound: \"40 mm drivers\", Calls: \"6 mics\" } };\nconst CROSS: CrossSellProduct[] = [\n  { id: \"case\", title: \"Silicone case cover\", note: \"Drop protection for the charging case\", price: 15, image: \"/demo/wireless-earbuds.webp\" },\n  { id: \"stand\", title: \"Laptop Stand\", note: \"Raise your screen for calls\", price: 49, image: \"/demo/laptop-stand.webp\" },\n  { id: \"bottle\", title: \"Insulated Water Bottle\", note: \"For long work or gym sessions\", price: 39, image: \"/demo/insulated-water-bottle.webp\" },\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.45, 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 UpsellCrossSellBlock({ current = CURRENT, upgrade = UPGRADE, crossSells = CROSS, bundleDiscount = 15, currency = \"$\", onAddToCart, theme = \"dark\", className }: UpsellCrossSellBlockProps) {\n  const p = PALETTES[theme];\n  const reduce = !!useReducedMotion();\n  const uid = React.useId();\n  const [choice, setChoice] = React.useState<\"current\" | \"upgrade\">(\"current\");\n  const [extras, setExtras] = React.useState<string[]>([crossSells[0]?.id].filter(Boolean) as string[]);\n  const [state, setState] = React.useState<\"idle\" | \"loading\" | \"added\">(\"idle\");\n  const main = choice === \"upgrade\" ? upgrade : current;\n  const picked = crossSells.filter((c) => extras.includes(c.id));\n  const factor = 1 - bundleDiscount / 100;\n  const extrasTotal = picked.reduce((a, c) => a + c.price * factor, 0);\n  const total = main.price + extrasTotal;\n  const saved = picked.reduce((a, c) => a + c.price * (1 - factor), 0);\n  const money = (n: number) => `${currency}${n.toFixed(2)}`;\n  const specKeys = Object.keys(current.specs);\n\n  React.useEffect(() => {\n    if (state !== \"added\") return;\n    const t = setTimeout(() => setState(\"idle\"), 2400);\n    return () => clearTimeout(t);\n  }, [state]);\n\n  const add = async () => {\n    if (state === \"loading\") return;\n    setState(\"loading\");\n    try {\n      const items = [{ id: main.id, price: main.price }, ...picked.map((c) => ({ id: c.id, price: +(c.price * factor).toFixed(2) }))];\n      await (onAddToCart ? onAddToCart(items) : new Promise((r) => setTimeout(r, reduce ? 0 : 700)));\n      setState(\"added\");\n    } catch {\n      setState(\"idle\");\n    }\n  };\n\n  const ring = { [\"--tw-ring-color\" as string]: p.ring, [\"--tw-ring-offset-color\" as string]: p.bg } as React.CSSProperties;\n  const focus = \"outline-none focus-visible:ring-2 focus-visible:ring-offset-2\";\n  const option = (key: \"current\" | \"upgrade\", prod: UpsellProduct) => {\n    const on = choice === key;\n    return (\n      <label className=\"relative flex cursor-pointer flex-col rounded-2xl border-2 p-3 transition-colors has-[:focus-visible]:ring-2\" style={{ borderColor: on ? p.title : p.line, background: on ? p.cardOn : p.card, ...ring }}>\n        <input type=\"radio\" name={`${uid}-main`} checked={on} onChange={() => setChoice(key)} className=\"sr-only\" />\n        {prod.tag && (\n          <span className=\"absolute -top-3 left-3 rounded-full px-2.5 py-1 text-[11px] font-semibold text-[#0A0A0A]\" style={{ background: p.accent }}>\n            {prod.tag}\n          </span>\n        )}\n        <span className=\"relative aspect-[4/3] overflow-hidden rounded-xl\" style={{ background: p.photo }}>\n          {/* eslint-disable-next-line @next/next/no-img-element */}\n          {prod.image && <img src={prod.image} alt=\"\" className=\"h-full w-full object-cover\" />}\n          <motion.span className=\"absolute top-2 right-2 grid h-6 w-6 place-items-center rounded-full border-2\" animate={{ backgroundColor: on ? \"#111111\" : \"#FFFFFF\", borderColor: on ? \"#111111\" : \"rgba(17,17,17,0.25)\" }}>\n            {on && (\n              <motion.svg width=\"11\" height=\"11\" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"#FFFFFF\" strokeWidth=\"3.5\" strokeLinecap=\"round\" strokeLinejoin=\"round\" initial={reduce ? false : { scale: 0 }} animate={{ scale: 1 }} aria-hidden=\"true\">\n                <path d=\"m5 12.5 4.5 4.5L19 7.5\" />\n              </motion.svg>\n            )}\n          </motion.span>\n        </span>\n        <span className=\"mt-3 text-[13px] leading-tight font-medium\">{prod.title}</span>\n        <span className=\"mt-1 flex items-baseline gap-1.5\">\n          <span className=\"text-[16px] font-semibold tabular-nums\">{money(prod.price)}</span>\n          {key === \"upgrade\" && (\n            <span className=\"text-[12px] tabular-nums\" style={{ color: p.muted }}>\n              +{money(upgrade.price - current.price)}\n            </span>\n          )}\n        </span>\n      </label>\n    );\n  };\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-6xl px-4 py-12 @[640px]:px-8\">\n        <div className=\"flex flex-wrap items-center gap-3\">\n          <span className=\"grid h-7 w-7 place-items-center rounded-full text-[13px] font-bold text-[#0A0A0A]\" style={{ background: p.accent }} aria-hidden=\"true\">\n            ✓\n          </span>\n          <h2 className=\"m-0 text-[24px] font-semibold tracking-[-0.02em] @[640px]:text-[30px]\">Great pick. Make it even better?</h2>\n        </div>\n\n        <div className=\"mt-7 grid grid-cols-1 gap-6 @[900px]:grid-cols-[minmax(0,1.15fr)_minmax(0,1fr)]\">\n          {/* Upsell */}\n          <div className=\"rounded-3xl border p-5\" style={{ borderColor: p.line }}>\n            <p className=\"m-0 text-[12px] font-medium tracking-[0.08em] uppercase\" style={{ color: p.muted }}>\n              Upgrade\n            </p>\n            <div role=\"radiogroup\" aria-label=\"Choose your product\" className=\"mt-4 grid grid-cols-2 gap-3\">\n              {option(\"current\", current)}\n              {option(\"upgrade\", upgrade)}\n            </div>\n            <table className=\"mt-5 w-full border-collapse text-[12.5px]\">\n              <caption className=\"sr-only\">Compare {current.title} and {upgrade.title}</caption>\n              <tbody>\n                {specKeys.map((k) => (\n                  <tr key={k} className=\"border-t\" style={{ borderColor: p.line }}>\n                    <th scope=\"row\" className=\"w-[30%] py-2 pr-2 text-left font-normal\" style={{ color: p.muted }}>\n                      {k}\n                    </th>\n                    <td className=\"py-2 pr-2 transition-colors\" style={{ color: choice === \"current\" ? p.title : p.muted }}>\n                      {current.specs[k]}\n                    </td>\n                    <td className=\"py-2 transition-colors\" style={{ color: choice === \"upgrade\" ? p.title : p.muted, fontWeight: choice === \"upgrade\" ? 600 : 400 }}>\n                      {upgrade.specs[k]}\n                    </td>\n                  </tr>\n                ))}\n              </tbody>\n            </table>\n          </div>\n\n          {/* Cross-sell */}\n          <div className=\"flex flex-col rounded-3xl border p-5\" style={{ borderColor: p.line }}>\n            <div className=\"flex items-center justify-between\">\n              <p className=\"m-0 text-[12px] font-medium tracking-[0.08em] uppercase\" style={{ color: p.muted }}>\n                Complete it\n              </p>\n              <span className=\"rounded-full px-2.5 py-1 text-[11px] font-semibold\" style={{ background: `${p.accent}1F`, color: p.accent }}>\n                {bundleDiscount}% off together\n              </span>\n            </div>\n            <ul className=\"m-0 mt-3 flex list-none flex-col gap-2 p-0\">\n              {crossSells.map((c) => {\n                const on = extras.includes(c.id);\n                return (\n                  <li key={c.id}>\n                    <label className=\"flex cursor-pointer items-center gap-3 rounded-2xl border p-2.5 transition-colors has-[:focus-visible]:ring-2\" style={{ borderColor: on ? p.title : p.line, background: on ? p.card : \"transparent\", ...ring }}>\n                      <input type=\"checkbox\" checked={on} onChange={() => setExtras((x) => (on ? x.filter((i) => i !== c.id) : [...x, c.id]))} className=\"h-4 w-4 shrink-0 cursor-pointer\" style={{ accentColor: p.title }} />\n                      <span className=\"h-12 w-12 shrink-0 overflow-hidden rounded-xl\" style={{ background: p.photo }}>\n                        {/* eslint-disable-next-line @next/next/no-img-element */}\n                        {c.image && <img src={c.image} alt=\"\" className=\"h-full w-full object-cover\" />}\n                      </span>\n                      <span className=\"min-w-0 flex-1\">\n                        <span className=\"block truncate text-[13.5px] font-medium\">{c.title}</span>\n                        <span className=\"block truncate text-[12px]\" style={{ color: p.muted }}>\n                          {c.note}\n                        </span>\n                      </span>\n                      <span className=\"shrink-0 text-right text-[13px] tabular-nums\">\n                        <span className=\"block font-semibold\">{money(c.price * factor)}</span>\n                        <span className=\"block text-[11.5px] line-through\" style={{ color: p.muted }}>\n                          {money(c.price)}\n                        </span>\n                      </span>\n                    </label>\n                  </li>\n                );\n              })}\n            </ul>\n\n            <div className=\"mt-auto pt-5\">\n              <div className=\"flex items-baseline justify-between border-t pt-4\" style={{ borderColor: p.line }}>\n                <span className=\"text-[13.5px]\" style={{ color: p.body }}>\n                  {1 + picked.length} {picked.length ? \"items\" : \"item\"}\n                </span>\n                <span className=\"text-[24px] font-semibold tracking-[-0.02em] tabular-nums\">\n                  <Money value={total} currency={currency} reduce={reduce} />\n                </span>\n              </div>\n              <AnimatePresence initial={false}>\n                {saved > 0 && (\n                  <motion.p key=\"s\" initial={reduce ? false : { opacity: 0, height: 0 }} animate={{ opacity: 1, height: \"auto\" }} exit={reduce ? undefined : { opacity: 0, height: 0 }} className=\"m-0 overflow-hidden text-right text-[12.5px]\" style={{ color: p.accent }} aria-live=\"polite\">\n                    Bundle saves you {money(saved)}\n                  </motion.p>\n                )}\n              </AnimatePresence>\n              <button type=\"button\" onClick={add} aria-live=\"polite\" className={cn(\"mt-4 flex h-12 w-full cursor-pointer items-center justify-center gap-2 rounded-full border-0 text-[15px] font-semibold\", focus)} style={{ background: state === \"added\" ? p.accent : p.buttonBg, color: state === \"added\" ? \"#0A0A0A\" : p.buttonText, transition: \"background-color .25s\", ...ring }}>\n                {state === \"loading\" ? <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=\"Adding\" /> : state === \"added\" ? \"✓ Added to cart\" : `Add ${1 + picked.length} to cart · ${money(total)}`}\n              </button>\n              <button type=\"button\" className={cn(\"mt-2 h-10 w-full cursor-pointer rounded-full border-0 bg-transparent text-[13px]\", focus)} style={{ color: p.body, ...ring }}>\n                No thanks, continue with {current.title}\n              </button>\n            </div>\n          </div>\n        </div>\n      </div>\n    </section>\n  );\n}\n\nexport default UpsellCrossSellBlock;\n",
      "type": "registry:component"
    }
  ],
  "type": "registry:component"
}