{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "recently-viewed-products",
  "title": "Recently Viewed Products",
  "description": "A browsing history strip grouped into Today, Yesterday and This week: snap-scrolling product cards with viewed times, price drop, back in stock and low stock badges, a quick add button that expands to Added, per-item removal and Clear history with an undo toast, and an empty state.",
  "dependencies": [
    "motion",
    "clsx",
    "tailwind-merge"
  ],
  "files": [
    {
      "path": "registry/new-york/recently-viewed-products/recently-viewed-products.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 ViewedProduct {\n  id: string;\n  name: string;\n  price: number;\n  was?: number;\n  image: string;\n  /** Group label, e.g. Today, Yesterday, This week. */\n  group: string;\n  /** When it was viewed, already formatted (e.g. \"12 min ago\"). */\n  viewed: string;\n  badge?: { kind: \"drop\" | \"back\" | \"low\"; label: string };\n  zoom?: number;\n}\nexport interface RecentlyViewedProductsProps {\n  items?: ViewedProduct[];\n  currency?: string;\n  title?: string;\n  onAdd?: (item: ViewedProduct) => void;\n  onRemove?: (item: ViewedProduct) => void;\n  onClear?: () => 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    amber: \"#F5B44A\",\n    amberText: \"#2A1A00\",\n    buttonBg: \"#FFFFFF\",\n    buttonText: \"#111111\",\n    ghost: \"rgba(255,255,255,0.07)\",\n    toast: \"#1C1C1C\",\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    amber: \"#F0A020\",\n    amberText: \"#2A1A00\",\n    buttonBg: \"#111111\",\n    buttonText: \"#FFFFFF\",\n    ghost: \"rgba(0,0,0,0.05)\",\n    toast: \"#111111\",\n  },\n};\ntype Palette = (typeof PALETTES)[\"dark\"];\n\nconst L = \"/demo/blocks/product-list\";\nconst ITEMS: ViewedProduct[] = [\n  { id: \"shoes\", name: \"Running Shoes\", price: 99, was: 149, image: \"/demo/running-shoes.webp\", group: \"Today\", viewed: \"12 min ago\", badge: { kind: \"drop\", label: \"Price dropped $50\" }, zoom: 1.2 },\n  { id: \"watch\", name: \"Minimal Watch\", price: 79, was: 129, image: \"/demo/minimal-watch.webp\", group: \"Today\", viewed: \"1 h ago\", zoom: 1.5 },\n  { id: \"earbuds\", name: \"Wireless Earbuds\", price: 129, image: \"/demo/wireless-earbuds.webp\", group: \"Today\", viewed: \"3 h ago\", zoom: 1.4 },\n  { id: \"headphones\", name: \"Headphones\", price: 199, image: `${L}/headphones.webp`, group: \"Yesterday\", viewed: \"Yesterday, 21:40\", badge: { kind: \"back\", label: \"Back in stock\" } },\n  { id: \"bottle\", name: \"Insulated Bottle\", price: 39, image: \"/demo/insulated-water-bottle.webp\", group: \"Yesterday\", viewed: \"Yesterday, 18:05\", zoom: 1.5 },\n  { id: \"backpack\", name: \"Backpack\", price: 89, image: `${L}/backpack.webp`, group: \"This week\", viewed: \"Mon\", badge: { kind: \"low\", label: \"Only 3 left\" } },\n  { id: \"notebook\", name: \"Notebook\", price: 24, image: \"/demo/notebook.webp\", group: \"This week\", viewed: \"Mon\", zoom: 1.4 },\n  { id: \"lamp\", name: \"Desk Lamp\", price: 59, image: `${L}/lamp.webp`, group: \"This week\", viewed: \"Sun\" },\n];\n\nconst money = (n: number, c: string) => `${c}${n.toFixed(2)}`;\n\nfunction Badge({ kind, label, p }: { kind: \"drop\" | \"back\" | \"low\"; label: string; p: Palette }) {\n  const style = kind === \"drop\" ? { background: p.accent, color: p.accentText } : kind === \"low\" ? { background: p.amber, color: p.amberText } : { background: \"#111111\", color: \"#FFFFFF\" };\n  return (\n    <span className=\"inline-flex items-center gap-1 rounded-full px-2 py-0.5 text-[11px] font-semibold whitespace-nowrap\" style={style}>\n      {kind === \"drop\" ? (\n        <svg width=\"10\" height=\"10\" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" strokeWidth=\"3\" strokeLinecap=\"round\" strokeLinejoin=\"round\" aria-hidden=\"true\">\n          <path d=\"M12 5v14M6 13l6 6 6-6\" />\n        </svg>\n      ) : null}\n      {label}\n    </span>\n  );\n}\n\nexport function RecentlyViewedProducts({ items = ITEMS, currency = \"$\", title = \"Recently viewed\", onAdd, onRemove, onClear, theme = \"dark\", className }: RecentlyViewedProductsProps) {\n  const p = PALETTES[theme];\n  const reduce = !!useReducedMotion();\n  const [list, setList] = React.useState(items);\n  const [added, setAdded] = React.useState<string[]>([]);\n  const [toast, setToast] = React.useState<{ id: number; text: string; restore: ViewedProduct[] } | null>(null);\n  const [edge, setEdge] = React.useState({ start: true, end: false });\n  const scroller = React.useRef<HTMLDivElement>(null);\n  const toastTimer = React.useRef<ReturnType<typeof setTimeout> | null>(null);\n  const toastSeq = React.useRef(0);\n  const anim = React.useRef<{ stop: () => void } | null>(null);\n\n  const groups = list.reduce<{ label: string; items: ViewedProduct[] }[]>((acc, it) => {\n    const g = acc.find((x) => x.label === it.group);\n    if (g) g.items.push(it);\n    else acc.push({ label: it.group, items: [it] });\n    return acc;\n  }, []);\n\n  const syncEdges = React.useCallback(() => {\n    const el = scroller.current;\n    if (!el) return;\n    setEdge({ start: el.scrollLeft < 4, end: el.scrollLeft + el.clientWidth > el.scrollWidth - 4 });\n  }, []);\n  React.useEffect(() => {\n    syncEdges();\n    const el = scroller.current;\n    if (!el) return;\n    const ro = new ResizeObserver(syncEdges);\n    ro.observe(el);\n    return () => ro.disconnect();\n  }, [syncEdges, list.length]);\n  React.useEffect(\n    () => () => {\n      if (toastTimer.current) clearTimeout(toastTimer.current);\n      anim.current?.stop();\n    },\n    [],\n  );\n\n  const showToast = (text: string, restore: ViewedProduct[]) => {\n    if (toastTimer.current) clearTimeout(toastTimer.current);\n    toastSeq.current += 1;\n    setToast({ id: toastSeq.current, text, restore });\n    toastTimer.current = setTimeout(() => setToast(null), 5000);\n  };\n  const remove = (it: ViewedProduct) => {\n    setList((cur) => cur.filter((x) => x.id !== it.id));\n    onRemove?.(it);\n    showToast(`Removed ${it.name}`, list);\n  };\n  const clear = () => {\n    setList([]);\n    onClear?.();\n    showToast(\"History cleared\", list);\n  };\n  const undo = () => {\n    if (!toast) return;\n    setList(toast.restore);\n    setToast(null);\n  };\n  const quickAdd = (it: ViewedProduct) => {\n    setAdded((cur) => (cur.includes(it.id) ? cur : [...cur, it.id]));\n    onAdd?.(it);\n  };\n  const page = (dir: 1 | -1) => {\n    const el = scroller.current;\n    if (!el) return;\n    const to = Math.max(0, Math.min(el.scrollWidth - el.clientWidth, el.scrollLeft + dir * el.clientWidth * 0.8));\n    anim.current?.stop();\n    if (reduce) {\n      el.scrollLeft = to;\n      return;\n    }\n    // Snap fights a programmatic tween, so switch it off while the arrows scroll.\n    el.style.scrollSnapType = \"none\";\n    anim.current = animate(el.scrollLeft, to, { duration: 0.55, ease: [0.22, 1, 0.36, 1], onUpdate: (v) => (el.scrollLeft = v), onComplete: () => (el.style.scrollSnapType = \"\") });\n  };\n\n  const arrow = (dir: 1 | -1, disabled: boolean) => (\n    <button\n      type=\"button\"\n      onClick={() => page(dir)}\n      disabled={disabled}\n      aria-label={dir === 1 ? \"Scroll right\" : \"Scroll left\"}\n      className=\"grid size-9 place-items-center rounded-full border transition-opacity disabled:opacity-30\"\n      style={{ borderColor: p.line, color: p.title }}\n    >\n      <svg width=\"14\" height=\"14\" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" strokeWidth=\"2.4\" strokeLinecap=\"round\" strokeLinejoin=\"round\" aria-hidden=\"true\">\n        <path d={dir === 1 ? \"m9 6 6 6-6 6\" : \"m15 6-6 6 6 6\"} />\n      </svg>\n    </button>\n  );\n\n  return (\n    <section className={cn(\"relative w-full overflow-hidden pt-12 pb-20 sm:pt-16 sm:pb-20\", className)} style={{ background: p.bg, color: p.body }}>\n      <div className=\"mx-auto w-full max-w-[1120px]\">\n        <div className=\"flex flex-wrap items-end justify-between gap-3 px-4 sm:px-8\">\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]\" aria-live=\"polite\">\n              {list.length ? `${list.length} product${list.length === 1 ? \"\" : \"s\"} from the last 7 days` : \"Your browsing history is empty\"}\n            </p>\n          </div>\n          <div className=\"flex items-center gap-2\">\n            {list.length ? (\n              <button type=\"button\" onClick={clear} className=\"mr-1 rounded-full px-3 py-2 text-[13px] font-medium underline-offset-4 hover:underline\" style={{ color: p.body }}>\n                Clear history\n              </button>\n            ) : null}\n            <div className=\"hidden gap-2 sm:flex\">\n              {arrow(-1, edge.start || !list.length)}\n              {arrow(1, edge.end || !list.length)}\n            </div>\n          </div>\n        </div>\n\n        <div className=\"relative mt-6\">\n          <AnimatePresence mode=\"wait\" initial={false}>\n            {list.length ? (\n              <motion.div key=\"list\" initial={reduce ? false : { opacity: 0 }} animate={{ opacity: 1 }} exit={{ opacity: 0 }}>\n                <div ref={scroller} onScroll={syncEdges} className=\"flex snap-x snap-mandatory scroll-px-4 gap-3 overflow-x-auto px-4 pb-2 [scrollbar-width:none] sm:scroll-px-8 sm:px-8 [&::-webkit-scrollbar]:hidden\" role=\"list\" aria-label={title}>\n                  <AnimatePresence initial={false}>\n                    {groups.map((g) => (\n                      <motion.div key={g.label} layout={!reduce} exit={{ opacity: 0 }} className=\"flex shrink-0 gap-3\" role=\"presentation\">\n                        <div className=\"flex w-5 shrink-0 snap-start items-start justify-center pt-2 sm:w-6\" aria-hidden=\"true\">\n                          <span className=\"text-[11px] font-semibold tracking-[0.14em] whitespace-nowrap uppercase [writing-mode:vertical-rl] rotate-180\" style={{ color: p.muted }}>\n                            {g.label}\n                          </span>\n                        </div>\n                        <AnimatePresence initial={false} mode=\"popLayout\">\n                          {g.items.map((it) => {\n                            const isAdded = added.includes(it.id);\n                            return (\n                              <motion.article\n                                key={it.id}\n                                role=\"listitem\"\n                                aria-label={`${it.name}, ${money(it.price, currency)}, viewed ${it.viewed}`}\n                                layout={!reduce}\n                                initial={reduce ? false : { opacity: 0, scale: 0.9 }}\n                                animate={{ opacity: 1, scale: 1 }}\n                                exit={reduce ? { opacity: 0 } : { opacity: 0, scale: 0.85, transition: { duration: 0.2 } }}\n                                transition={{ type: \"spring\", stiffness: 420, damping: 36 }}\n                                className=\"group relative w-[150px] shrink-0 snap-start sm:w-[190px]\"\n                              >\n                                <div className=\"relative aspect-square overflow-hidden rounded-2xl\" style={{ background: p.tile }}>\n                                  <a href=\"#\" className=\"block h-full w-full transition-transform duration-500 group-hover:scale-[1.04]\" aria-label={`View ${it.name}`}>\n                                    {/* eslint-disable-next-line @next/next/no-img-element -- plain img keeps the block framework-agnostic */}\n                                    <img src={it.image} alt=\"\" width={400} height={400} className=\"h-full w-full object-cover\" style={{ transform: `scale(${it.zoom ?? 1})` }} />\n                                  </a>\n                                  {it.badge ? (\n                                    <span className=\"pointer-events-none absolute top-2 left-2\">\n                                      <Badge kind={it.badge.kind} label={it.badge.label} p={p} />\n                                    </span>\n                                  ) : null}\n                                  <button\n                                    type=\"button\"\n                                    onClick={() => remove(it)}\n                                    aria-label={`Remove ${it.name} from history`}\n                                    className=\"absolute top-2 right-2 grid size-7 place-items-center rounded-full bg-black/60 text-white opacity-100 backdrop-blur transition-opacity focus-visible:opacity-100 sm:opacity-0 sm:group-hover:opacity-100\"\n                                  >\n                                    <svg width=\"10\" height=\"10\" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" strokeWidth=\"3\" strokeLinecap=\"round\" aria-hidden=\"true\">\n                                      <path d=\"M6 6l12 12M18 6 6 18\" />\n                                    </svg>\n                                  </button>\n                                  <motion.button\n                                    type=\"button\"\n                                    onClick={() => quickAdd(it)}\n                                    aria-label={isAdded ? `${it.name} added to cart` : `Add ${it.name} to cart`}\n                                    whileTap={reduce ? undefined : { scale: 0.9 }}\n                                    animate={{ background: isAdded ? p.accent : \"#FFFFFF\", color: isAdded ? p.accentText : \"#111111\", width: isAdded ? 76 : 32 }}\n                                    transition={{ type: \"spring\", stiffness: 420, damping: 30 }}\n                                    className=\"absolute right-2 bottom-2 flex h-8 items-center justify-center gap-1 overflow-hidden rounded-full text-[12px] font-semibold whitespace-nowrap shadow-[0_4px_14px_rgba(0,0,0,0.18)]\"\n                                  >\n                                    {isAdded ? (\n                                      <>\n                                        <svg width=\"12\" height=\"12\" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" strokeWidth=\"3.2\" strokeLinecap=\"round\" strokeLinejoin=\"round\" aria-hidden=\"true\">\n                                          <path d=\"M5 12.5 10 17l9-10\" />\n                                        </svg>\n                                        Added\n                                      </>\n                                    ) : (\n                                      <svg width=\"13\" height=\"13\" 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                                    )}\n                                  </motion.button>\n                                </div>\n                                <div className=\"px-0.5 pt-2.5\">\n                                  <p className=\"truncate text-[14px] font-medium\" style={{ color: p.title }}>\n                                    {it.name}\n                                  </p>\n                                  <p className=\"mt-0.5 flex items-baseline gap-2 text-[13px] 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                                  <p className=\"mt-1 flex items-center gap-1 text-[11px]\" style={{ color: p.muted }}>\n                                    <svg width=\"11\" height=\"11\" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" strokeWidth=\"2\" strokeLinecap=\"round\" aria-hidden=\"true\">\n                                      <circle cx=\"12\" cy=\"12\" r=\"8.5\" />\n                                      <path d=\"M12 7.5V12l3 2\" />\n                                    </svg>\n                                    {it.viewed}\n                                  </p>\n                                </div>\n                              </motion.article>\n                            );\n                          })}\n                        </AnimatePresence>\n                      </motion.div>\n                    ))}\n                  </AnimatePresence>\n                </div>\n              </motion.div>\n            ) : (\n              <motion.div\n                key=\"empty\"\n                initial={reduce ? false : { opacity: 0, y: 10 }}\n                animate={{ opacity: 1, y: 0 }}\n                exit={{ opacity: 0 }}\n                className=\"mx-4 flex flex-col items-center rounded-2xl border border-dashed px-6 py-12 text-center sm:mx-8\"\n                style={{ borderColor: p.line }}\n              >\n                <span className=\"grid size-12 place-items-center rounded-full\" style={{ background: p.ghost, color: p.title }}>\n                  <svg width=\"20\" height=\"20\" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" strokeWidth=\"1.8\" strokeLinecap=\"round\" strokeLinejoin=\"round\" aria-hidden=\"true\">\n                    <path d=\"M2.5 12S6 5.5 12 5.5 21.5 12 21.5 12 18 18.5 12 18.5 2.5 12 2.5 12Z\" />\n                    <circle cx=\"12\" cy=\"12\" r=\"3\" />\n                  </svg>\n                </span>\n                <p className=\"mt-4 text-[16px] font-medium\" style={{ color: p.title }}>\n                  Nothing here yet\n                </p>\n                <p className=\"mt-1 max-w-[320px] text-[14px]\">Products you look at show up here, so you can find them again without searching.</p>\n                <a href=\"#\" className=\"mt-5 rounded-full px-5 py-2.5 text-[14px] font-semibold\" style={{ background: p.buttonBg, color: p.buttonText }}>\n                  Start browsing\n                </a>\n              </motion.div>\n            )}\n          </AnimatePresence>\n        </div>\n      </div>\n\n      {/* Undo toast */}\n      <div className=\"pointer-events-none absolute inset-x-0 bottom-3 flex justify-center px-4\" aria-live=\"polite\">\n        <AnimatePresence>\n          {toast ? (\n            <motion.div\n              key={toast.id}\n              initial={reduce ? { opacity: 0 } : { opacity: 0, y: 16, scale: 0.96 }}\n              animate={{ opacity: 1, y: 0, scale: 1 }}\n              exit={reduce ? { opacity: 0 } : { opacity: 0, y: 12, scale: 0.96 }}\n              transition={{ type: \"spring\", stiffness: 420, damping: 32 }}\n              className=\"pointer-events-auto flex items-center gap-4 rounded-full py-2 pr-2 pl-4 text-[13px] text-white shadow-[0_12px_30px_rgba(0,0,0,0.35)]\"\n              style={{ background: p.toast }}\n            >\n              <span>{toast.text}</span>\n              <button type=\"button\" onClick={undo} className=\"rounded-full bg-white px-3 py-1 text-[12px] font-semibold text-[#111111]\">\n                Undo\n              </button>\n            </motion.div>\n          ) : null}\n        </AnimatePresence>\n      </div>\n    </section>\n  );\n}\n\nexport default RecentlyViewedProducts;\n",
      "type": "registry:component"
    }
  ],
  "type": "registry:component"
}