{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "low-stock-urgency-badge",
  "title": "Low Stock Urgency Badge",
  "description": "Honest stock urgency for product pages and cards: badges that switch between In stock, Selling fast, Only n left (pulsing amber), Last one! (red with a shake) and Back on a date, plus a segmented stock meter that highlights the units you're about to take and drops when you add to cart.",
  "dependencies": [
    "motion",
    "clsx",
    "tailwind-merge"
  ],
  "files": [
    {
      "path": "registry/new-york/low-stock-urgency-badge/low-stock-urgency-badge.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\nimport { 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 StockVariant {\n  id: string;\n  label: string;\n  swatch: string;\n  stock: number;\n}\nexport interface UrgencyCard {\n  id: string;\n  name: string;\n  price: number;\n  image: string;\n  stock: number;\n  /** Units sold in the last 24 hours, from your analytics. */\n  sold24h?: number;\n  restock?: string;\n  zoom?: number;\n}\nexport interface LowStockUrgencyBadgeProps {\n  product?: { name: string; price: number; image: string; variants: StockVariant[] };\n  cards?: UrgencyCard[];\n  /** Stock at or below this shows the low-stock state. */\n  lowAt?: number;\n  /** Sold in 24h at or above this shows Selling fast (when stock isn't low). */\n  hotAt?: number;\n  /** Segments in the stock meter. */\n  meterMax?: number;\n  currency?: string;\n  onAdd?: (variant: StockVariant, qty: number) => 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    tile: \"#F1F1F1\",\n    accent: \"#7CDE6A\",\n    accentText: \"#0B1A08\",\n    amber: \"#F5B44A\",\n    amberText: \"#2A1A00\",\n    red: \"#FF6B5E\",\n    redText: \"#2B0703\",\n    buttonBg: \"#FFFFFF\",\n    buttonText: \"#111111\",\n    ghost: \"rgba(255,255,255,0.07)\",\n    seg: \"rgba(255,255,255,0.1)\",\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    tile: \"#F1F1F1\",\n    accent: \"#2F9E44\",\n    accentText: \"#FFFFFF\",\n    amber: \"#E59400\",\n    amberText: \"#2A1A00\",\n    red: \"#E03E2D\",\n    redText: \"#FFFFFF\",\n    buttonBg: \"#111111\",\n    buttonText: \"#FFFFFF\",\n    ghost: \"rgba(0,0,0,0.05)\",\n    seg: \"rgba(0,0,0,0.08)\",\n  },\n};\ntype Palette = (typeof PALETTES)[\"dark\"];\n\nconst PRODUCT = {\n  name: \"Minimal Watch\",\n  price: 79,\n  image: \"/demo/blocks/product-detail/watch-1.webp\",\n  variants: [\n    { id: \"black\", label: \"Black leather\", swatch: \"#1C1C1C\", stock: 3 },\n    { id: \"tan\", label: \"Tan leather\", swatch: \"#A86F43\", stock: 14 },\n    { id: \"canvas\", label: \"Olive canvas\", swatch: \"#6E7350\", stock: 1 },\n    { id: \"mesh\", label: \"Steel mesh\", swatch: \"#B9BCC0\", stock: 0 },\n  ],\n};\nconst L = \"/demo/blocks/product-list\";\nconst CARDS: UrgencyCard[] = [\n  { id: \"shoes\", name: \"Running Shoes\", price: 99, image: \"/demo/running-shoes.webp\", stock: 4, zoom: 1.15 },\n  { id: \"earbuds\", name: \"Wireless Earbuds\", price: 129, image: \"/demo/wireless-earbuds.webp\", stock: 40, sold24h: 86, zoom: 1.35 },\n  { id: \"lamp\", name: \"Desk Lamp\", price: 59, image: `${L}/lamp.webp`, stock: 1 },\n  { id: \"backpack\", name: \"Backpack\", price: 89, image: `${L}/backpack.webp`, stock: 0, restock: \"Dec 12\" },\n];\n\ntype Level = \"out\" | \"last\" | \"low\" | \"hot\" | \"ok\";\nfunction levelOf(stock: number, lowAt: number, sold24h = 0, hotAt = Infinity): Level {\n  if (stock <= 0) return \"out\";\n  if (stock === 1) return \"last\";\n  if (stock <= lowAt) return \"low\";\n  if (sold24h >= hotAt) return \"hot\";\n  return \"ok\";\n}\n\nfunction Badge({ level, stock, sold24h, restock, p, size = \"md\" }: { level: Level; stock: number; sold24h?: number; restock?: string; p: Palette; size?: \"sm\" | \"md\" }) {\n  const reduce = useReducedMotion();\n  const tone = level === \"last\" ? { bg: p.red, fg: p.redText } : level === \"low\" ? { bg: p.amber, fg: p.amberText } : level === \"hot\" ? { bg: p.accent, fg: p.accentText } : level === \"out\" ? { bg: \"#2A2A2A\", fg: \"#FFFFFF\" } : { bg: \"#111111\", fg: \"#FFFFFF\" };\n  const text = level === \"out\" ? (restock ? `Back ${restock}` : \"Sold out\") : level === \"last\" ? \"Last one!\" : level === \"low\" ? `Only ${stock} left` : level === \"hot\" ? `${sold24h} sold today` : \"In stock\";\n  return (\n    <motion.span\n      key={`${level}-${stock}`}\n      initial={reduce ? false : { opacity: 0, y: 6, scale: 0.92 }}\n      animate={reduce ? { opacity: 1 } : level === \"last\" ? { opacity: 1, y: 0, scale: 1, rotate: [0, -4, 4, -3, 2, 0] } : { opacity: 1, y: 0, scale: 1 }}\n      exit={reduce ? { opacity: 0 } : { opacity: 0, y: -6, scale: 0.92 }}\n      transition={{ duration: level === \"last\" ? 0.6 : 0.25 }}\n      className={cn(\"inline-flex items-center gap-1.5 rounded-full font-semibold whitespace-nowrap\", size === \"sm\" ? \"px-2 py-0.5 text-[11px]\" : \"px-3 py-1 text-[13px]\")}\n      style={{ background: tone.bg, color: tone.fg }}\n    >\n      {level === \"low\" || level === \"last\" ? (\n        <span className=\"relative flex size-2\" aria-hidden=\"true\">\n          {!reduce ? <motion.span className=\"absolute inset-0 rounded-full\" style={{ background: tone.fg }} animate={{ scale: [1, 2.2], opacity: [0.5, 0] }} transition={{ duration: 1.4, repeat: Infinity, ease: \"easeOut\" }} /> : null}\n          <span className=\"relative size-2 rounded-full\" style={{ background: tone.fg }} />\n        </span>\n      ) : level === \"hot\" ? (\n        <svg width={size === \"sm\" ? 11 : 13} height={size === \"sm\" ? 11 : 13} viewBox=\"0 0 24 24\" fill=\"currentColor\" aria-hidden=\"true\">\n          <path d=\"M12.5 2.5c.6 3.2-1.4 4.8-2.9 6.6C8.3 10.7 7 12.4 7 15a5 5 0 0 0 10 0c0-2-.9-3.6-1.8-4.6-.3 1.4-1.1 2.3-2.2 2.6.6-2.9.3-6.3-.5-10.5Z\" />\n        </svg>\n      ) : level === \"ok\" ? (\n        <span className=\"size-2 rounded-full\" style={{ background: p.accent }} aria-hidden=\"true\" />\n      ) : level === \"out\" ? (\n        <svg width=\"12\" height=\"12\" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" strokeWidth=\"2.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      ) : null}\n      {text}\n    </motion.span>\n  );\n}\n\nfunction Meter({ stock, taking, max, level, p }: { stock: number; taking: number; max: number; level: Level; p: Palette }) {\n  const reduce = useReducedMotion();\n  const color = level === \"last\" ? p.red : level === \"low\" ? p.amber : p.accent;\n  const shown = Math.min(stock, max);\n  return (\n    <div className=\"flex gap-1\" aria-hidden=\"true\">\n      {Array.from({ length: max }, (_, i) => {\n        const filled = i < shown;\n        const mine = filled && i >= shown - taking;\n        return (\n          <motion.span\n            key={i}\n            className=\"h-2 flex-1 rounded-full\"\n            initial={false}\n            animate={{ background: filled ? color : p.seg, opacity: taking > 0 && filled && !mine ? 0.45 : 1, scaleY: mine && !reduce ? 1.5 : 1 }}\n            transition={{ duration: 0.3, delay: reduce ? 0 : i * 0.025 }}\n          />\n        );\n      })}\n    </div>\n  );\n}\n\nexport function LowStockUrgencyBadge({ product = PRODUCT, cards = CARDS, lowAt = 5, hotAt = 50, meterMax = 10, currency = \"$\", onAdd, theme = \"dark\", className }: LowStockUrgencyBadgeProps) {\n  const p = PALETTES[theme];\n  const reduce = !!useReducedMotion();\n  const uid = React.useId();\n  const [stocks, setStocks] = React.useState<Record<string, number>>(() => Object.fromEntries(product.variants.map((v) => [v.id, v.stock])));\n  const [vid, setVid] = React.useState(product.variants[0].id);\n  const [qty, setQty] = React.useState(1);\n  const [added, setAdded] = React.useState<string | null>(null);\n\n  const v = product.variants.find((x) => x.id === vid) ?? product.variants[0];\n  const stock = stocks[v.id] ?? 0;\n  const level = levelOf(stock, lowAt);\n  const take = Math.min(qty, stock);\n\n  const pick = (id: string) => {\n    setVid(id);\n    setQty(1);\n    setAdded(null);\n  };\n  const add = () => {\n    if (!stock) return;\n    setStocks((s) => ({ ...s, [v.id]: Math.max(0, (s[v.id] ?? 0) - take) }));\n    setAdded(`${take} × ${v.label}`);\n    setQty(1);\n    onAdd?.(v, take);\n  };\n\n  const stockLine =\n    level === \"out\"\n      ? \"This strap is sold out. We restock every few weeks.\"\n      : level === \"last\"\n        ? \"This is the last one in this strap.\"\n        : level === \"low\"\n          ? take > 1\n            ? `You’re taking ${take} of the last ${stock}.`\n            : `Only ${stock} left in this strap. Order soon.`\n          : `In stock · ${stock} available`;\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-[1000px]\">\n        {/* Featured buy box */}\n        <div className=\"grid gap-6 rounded-3xl border p-4 sm:p-5 md:grid-cols-[260px_minmax(0,1fr)] md:gap-8\" style={{ borderColor: p.line, background: p.card }}>\n          <div className=\"relative aspect-[4/3] overflow-hidden rounded-2xl md:aspect-square\" style={{ background: p.tile }}>\n            {/* eslint-disable-next-line @next/next/no-img-element -- plain img keeps the block framework-agnostic */}\n            <img src={product.image} alt={product.name} width={900} height={900} className=\"h-full w-full object-cover\" style={{ filter: level === \"out\" ? \"grayscale(1) opacity(0.6)\" : undefined, transition: \"filter 300ms\" }} />\n            <span className=\"absolute top-3 left-3\">\n              <AnimatePresence mode=\"wait\" initial={false}>\n                <Badge key={`${level}-${stock}`} level={level} stock={stock} p={p} />\n              </AnimatePresence>\n            </span>\n          </div>\n          <div className=\"flex min-w-0 flex-col\">\n            <div className=\"flex items-baseline justify-between gap-3\">\n              <h2 className=\"text-[24px] font-semibold tracking-tight sm:text-[28px]\" style={{ color: p.title }}>\n                {product.name}\n              </h2>\n              <span className=\"text-[18px] font-semibold tabular-nums\" style={{ color: p.title }}>\n                {currency}\n                {product.price.toFixed(2)}\n              </span>\n            </div>\n            <p id={`${uid}-strap`} className=\"mt-4 mb-2 text-[13px]\" style={{ color: p.muted }}>\n              Strap <span style={{ color: p.title }}>{v.label}</span>\n            </p>\n            <div role=\"radiogroup\" aria-labelledby={`${uid}-strap`} className=\"flex flex-wrap gap-2\">\n              {product.variants.map((x) => {\n                const on = x.id === vid;\n                const s = stocks[x.id] ?? 0;\n                return (\n                  <button\n                    key={x.id}\n                    type=\"button\"\n                    role=\"radio\"\n                    aria-checked={on}\n                    aria-label={`${x.label}, ${s === 0 ? \"sold out\" : s === 1 ? \"last one\" : `${s} left`}`}\n                    onClick={() => pick(x.id)}\n                    className=\"inline-flex items-center gap-2 rounded-full border py-1.5 pr-3 pl-1.5 text-[13px] font-medium transition-colors\"\n                    style={{ borderColor: on ? p.title : p.line, color: s === 0 ? p.muted : p.title }}\n                  >\n                    <span className=\"size-5 rounded-full\" style={{ background: x.swatch, boxShadow: `inset 0 0 0 1px ${p.line}`, opacity: s === 0 ? 0.4 : 1 }} />\n                    <span className={cn(s === 0 && \"line-through\")}>{x.label}</span>\n                    {s > 0 && s <= lowAt ? <span className=\"size-1.5 rounded-full\" style={{ background: s === 1 ? p.red : p.amber }} aria-hidden=\"true\" /> : null}\n                  </button>\n                );\n              })}\n            </div>\n\n            <div className=\"mt-5\">\n              <Meter stock={stock} taking={level === \"low\" || level === \"last\" ? take : 0} max={meterMax} level={level} p={p} />\n              <div className=\"relative mt-2 h-5 text-[13px]\" aria-live=\"polite\">\n                <AnimatePresence mode=\"popLayout\" initial={false}>\n                  <motion.p\n                    key={stockLine}\n                    initial={reduce ? false : { opacity: 0, y: 6 }}\n                    animate={{ opacity: 1, y: 0 }}\n                    exit={{ opacity: 0, y: -6 }}\n                    transition={{ duration: 0.2 }}\n                    className=\"absolute inset-0 truncate\"\n                    style={{ color: level === \"last\" ? p.red : level === \"low\" ? p.amber : level === \"out\" ? p.muted : p.body }}\n                  >\n                    {stockLine}\n                  </motion.p>\n                </AnimatePresence>\n              </div>\n            </div>\n\n            <div className=\"mt-auto flex flex-wrap items-center gap-3 pt-5\">\n              <div className=\"inline-flex h-11 items-center rounded-full border px-1\" style={{ borderColor: p.line }}>\n                <button type=\"button\" onClick={() => setQty((q) => Math.max(1, q - 1))} disabled={qty <= 1 || !stock} aria-label=\"Decrease quantity\" className=\"grid size-9 place-items-center rounded-full disabled:opacity-30\" style={{ color: p.title }}>\n                  <svg width=\"12\" height=\"12\" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" strokeWidth=\"2.6\" strokeLinecap=\"round\" aria-hidden=\"true\">\n                    <path d=\"M5 12h14\" />\n                  </svg>\n                </button>\n                <span className=\"w-7 text-center text-[14px] font-medium tabular-nums\" style={{ color: p.title }} aria-live=\"polite\" aria-label={`Quantity ${take}`}>\n                  {stock ? take : 0}\n                </span>\n                <button type=\"button\" onClick={() => setQty((q) => Math.min(stock, q + 1))} disabled={qty >= stock} aria-label=\"Increase quantity\" className=\"grid size-9 place-items-center rounded-full disabled:opacity-30\" style={{ color: p.title }}>\n                  <svg width=\"12\" height=\"12\" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" strokeWidth=\"2.6\" strokeLinecap=\"round\" aria-hidden=\"true\">\n                    <path d=\"M12 5v14M5 12h14\" />\n                  </svg>\n                </button>\n              </div>\n              <motion.button\n                type=\"button\"\n                onClick={add}\n                disabled={!stock}\n                whileTap={reduce || !stock ? undefined : { scale: 0.97 }}\n                className=\"h-11 min-w-0 flex-1 rounded-full px-5 text-[14px] font-semibold whitespace-nowrap disabled:opacity-40\"\n                style={{ background: p.buttonBg, color: p.buttonText }}\n              >\n                {stock ? (\n                  <>\n                    Add to cart\n                    <span className=\"hidden sm:inline\">\n                      {\" \"}\n                      · {currency}\n                      {(product.price * take).toFixed(2)}\n                    </span>\n                  </>\n                ) : (\n                  \"Sold out\"\n                )}\n              </motion.button>\n            </div>\n            <AnimatePresence>\n              {added ? (\n                <motion.p key={added + stock} initial={reduce ? false : { opacity: 0, y: 6 }} animate={{ opacity: 1, y: 0 }} exit={{ opacity: 0 }} className=\"mt-2.5 text-[12px]\" style={{ color: p.accent }} role=\"status\">\n                  Added {added} to your cart. Items are held for 15 minutes at checkout.\n                </motion.p>\n              ) : null}\n            </AnimatePresence>\n          </div>\n        </div>\n\n        {/* Badge styles on product cards */}\n        <ul className=\"mt-5 grid grid-cols-2 gap-3 md:grid-cols-4 md:gap-4\">\n          {cards.map((c) => {\n            const lv = levelOf(c.stock, lowAt, c.sold24h, hotAt);\n            return (\n              <li key={c.id} className=\"min-w-0\">\n                <div className=\"relative aspect-square overflow-hidden rounded-2xl\" style={{ background: p.tile }}>\n                  {/* eslint-disable-next-line @next/next/no-img-element -- plain img keeps the block framework-agnostic */}\n                  <img src={c.image} alt={c.name} width={500} height={500} className=\"h-full w-full object-cover\" style={{ transform: `scale(${c.zoom ?? 1})`, filter: lv === \"out\" ? \"grayscale(1) opacity(0.55)\" : undefined }} />\n                  <span className=\"absolute top-2 left-2\">\n                    <Badge level={lv} stock={c.stock} sold24h={c.sold24h} restock={c.restock} p={p} size=\"sm\" />\n                  </span>\n                </div>\n                <p className=\"mt-2 truncate text-[14px] font-medium\" style={{ color: p.title }}>\n                  {c.name}\n                </p>\n                <p className=\"text-[13px] tabular-nums\">\n                  {currency}\n                  {c.price.toFixed(2)}\n                </p>\n              </li>\n            );\n          })}\n        </ul>\n      </div>\n    </section>\n  );\n}\n\nexport default LowStockUrgencyBadge;\n",
      "type": "registry:component"
    }
  ],
  "type": "registry:component"
}