{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "frequently-bought-together",
  "title": "Frequently Bought Together",
  "description": "A bundle builder for product pages: the current product plus add-ons joined by plus signs, each toggled with a check, greyed out when skipped, with bundle discount tiers, a counting total, savings and an Add all to cart button that confirms the items added.",
  "dependencies": [
    "motion",
    "clsx",
    "tailwind-merge"
  ],
  "files": [
    {
      "path": "registry/new-york/frequently-bought-together/frequently-bought-together.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\nimport { animate, AnimatePresence, motion, useReducedMotion } 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 BundleItem {\n  id: string;\n  name: string;\n  price: number;\n  was?: number;\n  image: string;\n  rating?: number;\n  /** Image zoom inside its tile (1 = as is). */\n  zoom?: number;\n}\nexport interface BundleTier {\n  /** Number of items needed. */\n  count: number;\n  /** Discount in percent. */\n  off: number;\n}\nexport interface FrequentlyBoughtTogetherProps {\n  /** The first item is the product being viewed and is always included. */\n  items?: BundleItem[];\n  tiers?: BundleTier[];\n  currency?: string;\n  title?: string;\n  onAdd?: (items: BundleItem[], total: number) => void | Promise<void>;\n  theme?: \"dark\" | \"light\";\n  className?: string;\n}\n\nconst PALETTES = {\n  dark: {\n    bg: \"#0A0A0A\",\n    card: \"#141414\",\n    title: \"#FFFFFF\",\n    muted: \"rgba(255,255,255,0.45)\",\n    body: \"rgba(255,255,255,0.62)\",\n    line: \"rgba(255,255,255,0.12)\",\n    lineHi: \"rgba(255,255,255,0.35)\",\n    tile: \"#F1F1F1\",\n    accent: \"#7CDE6A\",\n    accentText: \"#0B1A08\",\n    accentSoft: \"rgba(124,222,106,0.12)\",\n    star: \"#F5C451\",\n    buttonBg: \"#FFFFFF\",\n    buttonText: \"#111111\",\n    ghost: \"rgba(255,255,255,0.07)\",\n  },\n  light: {\n    bg: \"#FFFFFF\",\n    card: \"#F7F7F7\",\n    title: \"#111111\",\n    muted: \"rgba(0,0,0,0.45)\",\n    body: \"rgba(0,0,0,0.62)\",\n    line: \"rgba(0,0,0,0.1)\",\n    lineHi: \"rgba(0,0,0,0.3)\",\n    tile: \"#F1F1F1\",\n    accent: \"#2F9E44\",\n    accentText: \"#FFFFFF\",\n    accentSoft: \"rgba(47,158,68,0.1)\",\n    star: \"#E0A100\",\n    buttonBg: \"#111111\",\n    buttonText: \"#FFFFFF\",\n    ghost: \"rgba(0,0,0,0.05)\",\n  },\n};\ntype Palette = (typeof PALETTES)[\"dark\"];\n\nconst ITEMS: BundleItem[] = [\n  { id: \"stand\", name: \"Laptop Stand\", price: 49, was: 89, image: \"/demo/laptop-stand.webp\", rating: 4.8 },\n  { id: \"lamp\", name: \"Desk Lamp\", price: 59, image: \"/demo/blocks/product-list/lamp.webp\", rating: 4.6, zoom: 1.1 },\n  { id: \"notebook\", name: \"Notebook\", price: 24, image: \"/demo/notebook.webp\", rating: 4.9, zoom: 1.3 },\n  { id: \"earbuds\", name: \"Wireless Earbuds\", price: 129, image: \"/demo/wireless-earbuds.webp\", rating: 4.7, zoom: 1.35 },\n];\nconst TIERS: BundleTier[] = [\n  { count: 2, off: 5 },\n  { count: 3, off: 10 },\n  { count: 4, off: 15 },\n];\n\nconst money = (n: number, c: string) => `${c}${n.toFixed(2)}`;\n\n/** Tweens a money value by writing to the DOM, so counting doesn't re-render the block. */\nfunction Amount({ value, currency, className, style }: { value: number; currency: string; className?: string; style?: React.CSSProperties }) {\n  const ref = React.useRef<HTMLSpanElement>(null);\n  const prev = React.useRef(value);\n  const reduce = useReducedMotion();\n  React.useEffect(() => {\n    const el = ref.current;\n    if (!el) return;\n    if (reduce) {\n      el.textContent = money(value, currency);\n      prev.current = value;\n      return;\n    }\n    const c = animate(prev.current, value, { duration: 0.5, ease: [0.22, 1, 0.36, 1], onUpdate: (v) => (el.textContent = money(v, currency)) });\n    prev.current = value;\n    return () => c.stop();\n  }, [value, currency, reduce]);\n  return (\n    <span ref={ref} className={className} style={style}>\n      {money(value, currency)}\n    </span>\n  );\n}\n\nfunction Check({ on, p }: { on: boolean; p: Palette }) {\n  return (\n    <span className=\"grid size-6 place-items-center rounded-full border-2 transition-colors\" style={{ borderColor: on ? p.accent : p.lineHi, background: on ? p.accent : \"transparent\" }}>\n      <motion.svg width=\"12\" height=\"12\" viewBox=\"0 0 24 24\" fill=\"none\" stroke={p.accentText} strokeWidth=\"3.4\" strokeLinecap=\"round\" strokeLinejoin=\"round\" aria-hidden=\"true\">\n        <motion.path d=\"M5 12.5 10 17l9-10\" initial={false} animate={{ pathLength: on ? 1 : 0, opacity: on ? 1 : 0 }} transition={{ duration: 0.22 }} />\n      </motion.svg>\n    </span>\n  );\n}\n\nexport function FrequentlyBoughtTogether({ items = ITEMS, tiers = TIERS, currency = \"$\", title = \"Frequently bought together\", onAdd, theme = \"dark\", className }: FrequentlyBoughtTogetherProps) {\n  const p = PALETTES[theme];\n  const reduce = !!useReducedMotion();\n  const [picked, setPicked] = React.useState<string[]>(() => items.slice(0, 3).map((i) => i.id));\n  const [state, setState] = React.useState<\"idle\" | \"adding\" | \"added\">(\"idle\");\n  const timer = React.useRef<ReturnType<typeof setTimeout> | null>(null);\n  React.useEffect(() => () => void (timer.current && clearTimeout(timer.current)), []);\n\n  const main = items[0];\n  const chosen = items.filter((i) => i.id === main.id || picked.includes(i.id));\n  const n = chosen.length;\n  const tier = [...tiers].reverse().find((t) => n >= t.count) ?? null;\n  const next = tiers.find((t) => t.count > n) ?? null;\n  const subtotal = chosen.reduce((s, i) => s + i.price, 0);\n  const off = tier ? Math.round(subtotal * tier.off) / 100 : 0;\n  const total = subtotal - off;\n  const listSave = chosen.reduce((s, i) => s + (i.was ? i.was - i.price : 0), 0) + off;\n\n  const toggle = (id: string) => {\n    if (id === main.id) return;\n    setPicked((cur) => (cur.includes(id) ? cur.filter((x) => x !== id) : [...cur, id]));\n    setState(\"idle\");\n  };\n  const add = async () => {\n    if (state === \"adding\") return;\n    setState(\"adding\");\n    await Promise.all([onAdd?.(chosen, total), new Promise((r) => setTimeout(r, reduce ? 0 : 800))]);\n    setState(\"added\");\n    if (timer.current) clearTimeout(timer.current);\n    timer.current = setTimeout(() => setState(\"idle\"), 2600);\n  };\n\n  return (\n    <section className={cn(\"w-full px-4 py-12 sm:px-8 sm:py-16\", className)} style={{ background: p.bg, color: p.body }}>\n      <div className=\"mx-auto w-full max-w-[1040px]\">\n        <div className=\"flex flex-wrap items-end justify-between gap-3\">\n          <div>\n            <h2 className=\"text-[26px] font-semibold tracking-tight sm:text-[30px]\" style={{ color: p.title }}>\n              {title}\n            </h2>\n            <p className=\"mt-1 text-[14px]\">Customers who bought the {main.name} often added these.</p>\n          </div>\n          <p className=\"text-[13px]\" style={{ color: p.muted }}>\n            {n} of {items.length} selected\n          </p>\n        </div>\n\n        {/* Items */}\n        <ul className=\"mt-7 grid grid-cols-2 gap-3 md:flex md:items-stretch md:gap-0\">\n          {items.map((it, i) => {\n            const isMain = it.id === main.id;\n            const on = isMain || picked.includes(it.id);\n            return (\n              <React.Fragment key={it.id}>\n                {i > 0 ? (\n                  <li aria-hidden=\"true\" className=\"hidden w-9 shrink-0 place-items-center md:grid\">\n                    <motion.span animate={{ opacity: on ? 1 : 0.3, scale: on ? 1 : 0.8 }} className=\"grid size-7 place-items-center rounded-full\" style={{ background: p.ghost, color: p.title }}>\n                      <svg width=\"12\" height=\"12\" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" strokeWidth=\"2.8\" strokeLinecap=\"round\" aria-hidden=\"true\">\n                        <path d=\"M12 5v14M5 12h14\" />\n                      </svg>\n                    </motion.span>\n                  </li>\n                ) : null}\n                <li className=\"min-w-0 md:flex-1\">\n                  <motion.div\n                    role={isMain ? undefined : \"checkbox\"}\n                    aria-checked={isMain ? undefined : on}\n                    aria-label={isMain ? undefined : `${it.name}, ${money(it.price, currency)}`}\n                    tabIndex={isMain ? undefined : 0}\n                    onClick={() => toggle(it.id)}\n                    onKeyDown={(e) => !isMain && (e.key === \" \" || e.key === \"Enter\") && (e.preventDefault(), toggle(it.id))}\n                    animate={{ opacity: on ? 1 : 0.55 }}\n                    whileTap={isMain || reduce ? undefined : { scale: 0.97 }}\n                    className={cn(\"relative flex h-full flex-col rounded-2xl border p-2.5 outline-none focus-visible:ring-2\", !isMain && \"cursor-pointer\")}\n                    style={{ borderColor: on ? (isMain ? p.lineHi : p.accent) : p.line, background: p.card, [\"--tw-ring-color\" as string]: p.lineHi }}\n                  >\n                    <div className=\"relative aspect-square overflow-hidden rounded-xl\" style={{ background: p.tile }}>\n                      {/* eslint-disable-next-line @next/next/no-img-element -- plain img keeps the block framework-agnostic */}\n                      <img src={it.image} alt={isMain ? it.name : \"\"} width={600} height={600} className=\"h-full w-full object-cover transition-[filter] duration-300\" style={{ transform: `scale(${it.zoom ?? 1})`, filter: on ? \"none\" : \"grayscale(1)\" }} />\n                      <span className=\"absolute top-2 right-2\">\n                        {isMain ? (\n                          <span className=\"rounded-full px-2 py-0.5 text-[11px] font-semibold\" style={{ background: \"#111111\", color: \"#FFFFFF\" }}>\n                            This item\n                          </span>\n                        ) : (\n                          <Check on={on} p={p} />\n                        )}\n                      </span>\n                    </div>\n                    <div className=\"flex flex-1 flex-col px-1 pt-3 pb-1\">\n                      <p className=\"truncate text-[14px] font-medium\" style={{ color: p.title }}>\n                        {it.name}\n                      </p>\n                      {it.rating ? (\n                        <p className=\"mt-0.5 flex items-center gap-1 text-[12px]\" style={{ color: p.muted }}>\n                          <svg width=\"11\" height=\"11\" viewBox=\"0 0 24 24\" fill={p.star} aria-hidden=\"true\">\n                            <path d=\"m12 2.8 2.8 5.8 6.3.9-4.6 4.4 1.1 6.3L12 17.2l-5.6 3 1.1-6.3-4.6-4.4 6.3-.9z\" />\n                          </svg>\n                          {it.rating}\n                        </p>\n                      ) : null}\n                      <p className=\"mt-auto flex items-baseline gap-2 pt-2 text-[14px] tabular-nums\">\n                        <span className=\"font-semibold\" style={{ color: p.title }}>\n                          {money(it.price, currency)}\n                        </span>\n                        {it.was ? (\n                          <span className=\"text-[12px] line-through\" style={{ color: p.muted }}>\n                            {money(it.was, currency)}\n                          </span>\n                        ) : null}\n                      </p>\n                    </div>\n                  </motion.div>\n                </li>\n              </React.Fragment>\n            );\n          })}\n        </ul>\n\n        {/* Summary */}\n        <div className=\"mt-4 grid gap-5 rounded-2xl border p-5 md:grid-cols-[minmax(0,1fr)_auto] md:items-center md:gap-8 md:p-6\" style={{ borderColor: p.line, background: p.card }}>\n          <div className=\"min-w-0\">\n            <div className=\"flex flex-wrap gap-2\" aria-label=\"Bundle discounts\">\n              {tiers.map((t) => {\n                const active = tier?.count === t.count;\n                return (\n                  <motion.span\n                    key={t.count}\n                    animate={{ background: active ? p.accent : p.ghost, color: active ? p.accentText : n >= t.count ? p.title : p.muted, scale: active && !reduce ? [1, 1.08, 1] : 1 }}\n                    transition={{ duration: 0.3 }}\n                    className=\"rounded-full px-3 py-1 text-[12px] font-semibold tabular-nums\"\n                    aria-current={active || undefined}\n                  >\n                    {t.count} items · {t.off}% off\n                  </motion.span>\n                );\n              })}\n            </div>\n            <div className=\"relative mt-3 h-5 text-[14px]\" aria-live=\"polite\">\n              <AnimatePresence mode=\"popLayout\" initial={false}>\n                <motion.p key={`${n}`} initial={reduce ? false : { opacity: 0, y: 8 }} animate={{ opacity: 1, y: 0 }} exit={{ opacity: 0, y: -8 }} transition={{ duration: 0.2 }} className=\"absolute inset-0 truncate\">\n                  {next ? (\n                    <>\n                      Add {next.count - n} more to get <span style={{ color: p.title }}>{next.off}% off</span> the bundle.\n                    </>\n                  ) : (\n                    <span style={{ color: p.accent }}>Best bundle price unlocked.</span>\n                  )}\n                </motion.p>\n              </AnimatePresence>\n            </div>\n          </div>\n\n          <div className=\"flex flex-col gap-4 sm:flex-row sm:items-center md:gap-6\">\n            <div className=\"sm:text-right\">\n              <p className=\"text-[12px]\" style={{ color: p.muted }}>\n                Total for {n} item{n === 1 ? \"\" : \"s\"}\n              </p>\n              <p className=\"flex items-baseline gap-2 sm:justify-end\">\n                <Amount value={total} currency={currency} className=\"text-[26px] font-semibold tracking-tight tabular-nums\" style={{ color: p.title }} />\n                {off ? <Amount value={subtotal} currency={currency} className=\"text-[14px] tabular-nums line-through\" style={{ color: p.muted }} /> : null}\n              </p>\n              <AnimatePresence initial={false}>\n                {listSave > 0 ? (\n                  <motion.p key=\"save\" initial={{ opacity: 0, height: 0 }} animate={{ opacity: 1, height: \"auto\" }} exit={{ opacity: 0, height: 0 }} className=\"overflow-hidden text-[12px] font-medium\" style={{ color: p.accent }}>\n                    You save <Amount value={listSave} currency={currency} />\n                  </motion.p>\n                ) : null}\n              </AnimatePresence>\n            </div>\n            <motion.button\n              type=\"button\"\n              onClick={add}\n              disabled={state === \"adding\"}\n              whileTap={reduce ? undefined : { scale: 0.97 }}\n              animate={{ background: state === \"added\" ? p.accent : p.buttonBg, color: state === \"added\" ? p.accentText : p.buttonText }}\n              className=\"relative h-12 min-w-[210px] overflow-hidden rounded-full px-6 text-[15px] font-semibold whitespace-nowrap\"\n            >\n              <AnimatePresence mode=\"popLayout\" initial={false}>\n                <motion.span\n                  key={state === \"idle\" ? `idle-${n}` : state}\n                  initial={reduce ? false : { y: 16, opacity: 0 }}\n                  animate={{ y: 0, opacity: 1 }}\n                  exit={{ y: -16, opacity: 0 }}\n                  transition={{ duration: 0.2 }}\n                  className=\"inline-flex items-center justify-center gap-2\"\n                >\n                  {state === \"adding\" ? (\n                    <>\n                      <motion.span className=\"size-4 rounded-full border-2 border-current border-t-transparent\" animate={reduce ? undefined : { rotate: 360 }} transition={{ repeat: Infinity, duration: 0.8, ease: \"linear\" }} aria-hidden=\"true\" />\n                      Adding…\n                    </>\n                  ) : state === \"added\" ? (\n                    <>\n                      <svg width=\"15\" height=\"15\" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" strokeWidth=\"3\" strokeLinecap=\"round\" strokeLinejoin=\"round\" aria-hidden=\"true\">\n                        <path d=\"M5 12.5 10 17l9-10\" />\n                      </svg>\n                      {n} items added\n                    </>\n                  ) : n === 1 ? (\n                    \"Add to cart\"\n                  ) : (\n                    `Add all ${n} to cart`\n                  )}\n                </motion.span>\n              </AnimatePresence>\n            </motion.button>\n          </div>\n        </div>\n      </div>\n    </section>\n  );\n}\n\nexport default FrequentlyBoughtTogether;\n",
      "type": "registry:component"
    }
  ],
  "type": "registry:component"
}