{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "delta-badge",
  "title": "Delta badge",
  "description": "\"+12.4%\" change badge whose digits roll, arrow rotates to the new direction and background pulses green or red, with an inverted mode for metrics where down is good.",
  "dependencies": [
    "lucide-react",
    "motion"
  ],
  "registryDependencies": [
    "https://goodui.dev/r/goodui-motion.json",
    "https://goodui.dev/r/locale.json",
    "https://goodui.dev/r/number-flow.json"
  ],
  "files": [
    {
      "path": "registry/components/numbers/delta-badge.tsx",
      "content": "\"use client\"\n\nimport * as React from \"react\"\nimport { AnimatePresence, motion, useAnimate, useReducedMotion } from \"motion/react\"\nimport { ArrowUp } from \"lucide-react\"\nimport { cn } from \"@/lib/utils\"\nimport { useLabels } from \"@/lib/goodui-locale\"\nimport { NumberFlow } from \"@/components/goodui/number-flow\"\nimport { spring } from \"@/lib/goodui-motion\"\n\nexport type DeltaBadgeProps = Omit<React.ComponentProps<\"span\">, \"children\"> & {\n  /** The change, e.g. 12.4 for +12.4%. */\n  value: number\n  /** Decimals shown. */\n  decimals?: number\n  /** Text after the number; use \"\" or \" pts\" for non-percent changes. */\n  suffix?: string\n  /** Which direction is good. Use \"down\" for churn, no-shows or costs. */\n  goodWhen?: \"up\" | \"down\"\n  /** soft: tinted background, solid: filled green or red with white text, outline: colored border and text. */\n  variant?: \"soft\" | \"solid\" | \"outline\"\n  /** sm is 20px tall with 11px text, md is 24px with 12px text. */\n  size?: \"sm\" | \"md\"\n  /** Hide the arrow icon. */\n  hideIcon?: boolean\n  /** Text for built-in controls and screen readers; pass translations here. */\n  labels?: Partial<DeltaBadgeLabels>\n}\n\n/** Screen-reader wording of the change; `amount` is the formatted absolute value with suffix, e.g. \"12.4%\". */\nexport type DeltaBadgeLabels = { up: (amount: string) => string; down: (amount: string) => string; flat: (amount: string) => string }\nconst DEFAULT_LABELS: DeltaBadgeLabels = {\n  up: (amount) => `Up ${amount}`,\n  down: (amount) => `Down ${amount}`,\n  flat: (amount) => `No change, ${amount}`,\n}\n\ntype Tone = \"good\" | \"bad\" | \"flat\"\n\nconst TONES: Record<DeltaBadgeProps[\"variant\"] & string, Record<Tone, string>> = {\n  soft: {\n    good: \"bg-emerald-500/12 text-emerald-700 dark:bg-emerald-400/15 dark:text-emerald-300\",\n    bad: \"bg-rose-500/12 text-rose-700 dark:bg-rose-400/15 dark:text-rose-300\",\n    flat: \"bg-muted text-foreground/75\",\n  },\n  // Solid fills use the kit's success and danger tones so white text keeps 4.5:1 in light and dark.\n  solid: {\n    good: \"bg-[oklch(0.53_0.13_155)] text-white\",\n    bad: \"bg-[oklch(0.56_0.21_27)] text-white\",\n    flat: \"bg-muted-foreground text-background\",\n  },\n  outline: {\n    good: \"border-emerald-500/40 text-emerald-700 dark:text-emerald-300\",\n    bad: \"border-rose-500/40 text-rose-700 dark:text-rose-300\",\n    flat: \"border-border text-muted-foreground\",\n  },\n}\nconst PULSE: Record<Tone, string> = { good: \"bg-emerald-500/35\", bad: \"bg-rose-500/35\", flat: \"bg-foreground/15\" }\n\n/** Percent-change badge: digits roll, the arrow turns to the new direction and the badge pulses green or red. */\nexport function DeltaBadge({\n  value,\n  decimals = 1,\n  suffix = \"%\",\n  goodWhen = \"up\",\n  variant = \"soft\",\n  size = \"md\",\n  hideIcon = false,\n  labels,\n  className,\n  ...props\n}: DeltaBadgeProps) {\n  const l = useLabels(\"delta-badge\", DEFAULT_LABELS, labels)\n  const reduce = useReducedMotion() ?? false\n  const rounded = Number(value.toFixed(decimals))\n  const dir = rounded > 0 ? 1 : rounded < 0 ? -1 : 0\n  const tone: Tone = dir === 0 ? \"flat\" : (dir > 0) === (goodWhen === \"up\") ? \"good\" : \"bad\"\n\n  // Count changes during render (React's \"derived state\" pattern) so the pulse starts in the same frame.\n  const [prev, setPrev] = React.useState(rounded)\n  const [pulse, setPulse] = React.useState(0)\n  if (prev !== rounded) {\n    setPrev(rounded)\n    setPulse((p) => p + 1)\n  }\n\n  const format = React.useMemo<Intl.NumberFormatOptions>(\n    () => ({ minimumFractionDigits: decimals, maximumFractionDigits: decimals }),\n    [decimals],\n  )\n  const [scope, animate] = useAnimate<HTMLSpanElement>()\n  React.useEffect(() => {\n    if (pulse === 0 || reduce || !scope.current) return\n    const controls = animate(scope.current, { scale: [1, 1.12, 1] }, { duration: 0.35, ease: \"easeOut\" })\n    return () => controls.stop()\n  }, [pulse, reduce, animate, scope])\n\n  const sign = dir > 0 ? \"+\" : dir < 0 ? \"−\" : \"\"\n  const amount = `${Math.abs(rounded).toFixed(decimals)}${suffix}`\n  const spoken = dir > 0 ? l.up(amount) : dir < 0 ? l.down(amount) : l.flat(amount)\n\n  return (\n    <span\n      className={cn(\n        \"relative inline-flex shrink-0 items-center gap-0.5 rounded-full border border-transparent font-medium tabular-nums transition-colors duration-300\",\n        size === \"sm\" ? \"h-5 px-1.5 text-[11px]\" : \"h-6 px-2 text-xs\",\n        TONES[variant][tone],\n        className,\n      )}\n      {...props}\n    >\n      <span className=\"sr-only\">{spoken}</span>\n      <AnimatePresence>\n        {pulse > 0 && !reduce && (\n          <motion.span\n            key={pulse}\n            aria-hidden=\"true\"\n            className={cn(\"pointer-events-none absolute inset-0 rounded-full\", PULSE[tone])}\n            initial={{ opacity: 1, scale: 1 }}\n            animate={{ opacity: 0, scale: 1.35 }}\n            exit={{ opacity: 0 }}\n            transition={{ duration: 0.6, ease: \"easeOut\" }}\n          />\n        )}\n      </AnimatePresence>\n      {!hideIcon && (\n        // The outer span mirrors under RTL, so the flat arrow points toward the inline end.\n        <span aria-hidden=\"true\" className=\"relative inline-flex rtl:-scale-x-100\">\n          <motion.span\n            className=\"inline-flex\"\n            initial={false}\n            animate={{ rotate: dir > 0 ? 0 : dir < 0 ? 180 : 90 }}\n            transition={reduce ? { duration: 0 } : spring.play}\n          >\n            <ArrowUp className={size === \"sm\" ? \"size-3\" : \"size-3.5\"} strokeWidth={2.5} />\n          </motion.span>\n        </span>\n      )}\n      <span ref={scope} aria-hidden=\"true\" className=\"relative inline-flex\">\n        <span className=\"inline-block w-[1ch] text-center\">{sign}</span>\n        <NumberFlow value={Math.abs(rounded)} format={format} suffix={suffix} />\n      </span>\n    </span>\n  )\n}\n",
      "type": "registry:component",
      "target": "components/goodui/delta-badge.tsx"
    }
  ],
  "categories": [
    "components",
    "numbers"
  ],
  "type": "registry:component"
}
