{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "change-readout",
  "title": "Change Readout",
  "description": "Shows a change both ways (₹1,200 → ₹1,500 · +₹300 · +25%), keeps percentage points apart from percent (4% → 6% is +2 pts, +50%), gives no % from a zero base or across zero, divides by |base| for negatives, adds 2.5× when a value at least doubles and marks good or bad.",
  "dependencies": [
    "lucide-react"
  ],
  "files": [
    {
      "path": "registry/default/change-readout/change-readout.tsx",
      "content": "// Bollard UI — change-readout · MIT licence · https://bollard-ui.vercel.app/components/change-readout\r\n\r\nimport * as React from \"react\"\r\nimport { TrendingDown, TrendingUp } from \"lucide-react\"\r\n\r\nimport { cn } from \"@/lib/utils\"\r\n\r\nexport type ChangeSummary = {\r\n  /** to − from, in the values' own unit (percentage points when the values are percentages). */\r\n  delta: number\r\n  /** Relative change as a fraction (0.25 = +25%), or null when a percentage would be meaningless. */\r\n  relative: number | null\r\n  /** Why `relative` is null. */\r\n  reason?: \"zero-base\" | \"crosses-zero\"\r\n  direction: \"up\" | \"down\" | \"same\"\r\n  /** to ÷ from when it at least doubled, since \"+150%\" is often misread as 1.5×. */\r\n  multiple?: number\r\n}\r\n\r\n/**\r\n * Absolute and relative change between two numbers, correct at the edges: a negative base divides\r\n * by |from| so a shrinking loss reads as a rise, a zero base or a sign flip gives no percentage at\r\n * all instead of a misleading one, and float noise (0.1 + 0.2) is rounded off the delta.\r\n */\r\nexport function describeChange(from: number, to: number): ChangeSummary {\r\n  const delta = Number((to - from).toPrecision(12))\r\n  const direction = delta > 0 ? \"up\" : delta < 0 ? \"down\" : \"same\"\r\n  if (delta === 0) return { delta: 0, relative: 0, direction }\r\n  if (from === 0) return { delta, relative: null, reason: \"zero-base\", direction }\r\n  if (to !== 0 && Math.sign(from) !== Math.sign(to)) return { delta, relative: null, reason: \"crosses-zero\", direction }\r\n  const multiple = from > 0 && to / from >= 2 ? to / from : undefined\r\n  return { delta, relative: delta / Math.abs(from), direction, multiple }\r\n}\r\n\r\nconst MINUS = \"\\u2212\"\r\nconst signed = (n: number, text: string) => (n > 0 ? `+${text}` : n < 0 ? `${MINUS}${text}` : text)\r\n\r\n/** \"+25%\", \"−3.4%\", \"<0.1%\": never rounds a real change down to \"0%\". */\r\nexport function formatRelative(fraction: number, locale = \"en-IN\") {\r\n  const pct = Math.abs(fraction * 100)\r\n  if (pct > 0 && pct < 0.1) return signed(fraction, \"<0.1%\")\r\n  const digits = pct < 10 && pct % 1 !== 0 ? 1 : 0\r\n  return signed(fraction, `${new Intl.NumberFormat(locale, { maximumFractionDigits: digits }).format(pct)}%`)\r\n}\r\n\r\nconst tones = {\r\n  good: \"text-emerald-700 dark:text-emerald-400\",\r\n  bad: \"text-red-700 dark:text-red-400\",\r\n  neutral: \"text-foreground\",\r\n}\r\n\r\nexport interface ChangeReadoutProps extends Omit<React.ComponentProps<\"span\">, \"children\"> {\r\n  /** The earlier value. */\r\n  from: number\r\n  /** The later value. */\r\n  to: number\r\n  /** \"percent\" when the values are themselves percentages (4 means 4%): the difference is then shown in points. */\r\n  kind?: \"number\" | \"percent\"\r\n  /** Which way is good news, for colour and the \"better/worse\" wording. \"none\" keeps it neutral. */\r\n  goodDirection?: \"up\" | \"down\" | \"none\"\r\n  /** Shorthand for currency formatting, e.g. \"INR\". Ignored when `format` is set. */\r\n  currency?: string\r\n  /** Locale for number formatting. */\r\n  locale?: string\r\n  /** Formats one magnitude (always called with a value ≥ 0 for the delta). */\r\n  format?: (value: number) => string\r\n  /** Unit label for percentage points, e.g. \"pts\". */\r\n  pointsLabel?: string\r\n  /** Explanation of percentage points, shown as the abbreviation's title. */\r\n  pointsHint?: string\r\n  /** Shown instead of a percentage when the starting value is zero. */\r\n  zeroBaseText?: string\r\n  /** Shown instead of a percentage when the value changes sign. */\r\n  crossesZeroText?: string\r\n  /** Shown when the values are equal. */\r\n  noChangeText?: string\r\n  /** Word for a change in the good direction. */\r\n  betterLabel?: string\r\n  /** Word for a change in the bad direction. */\r\n  worseLabel?: string\r\n  /** Word read between the two values. */\r\n  toLabel?: string\r\n}\r\n\r\n/**\r\n * Shows a change both ways so nobody misreads its size: \"₹1,200 → ₹1,500 · +₹300 · +25%\".\r\n * Percentages get points and percent kept apart (\"4% → 6% · +2 pts · +50%\"), a zero base or a\r\n * sign flip says why there's no percentage, doubling adds \"2.5×\", and colour plus words say\r\n * whether the move is good or bad, because \"up\" isn't always good.\r\n */\r\nexport function ChangeReadout({\r\n  from,\r\n  to,\r\n  kind = \"number\",\r\n  goodDirection = \"up\",\r\n  currency,\r\n  locale = \"en-IN\",\r\n  format,\r\n  pointsLabel = \"pts\",\r\n  pointsHint = \"Percentage points: the plain difference between two percentages.\",\r\n  zeroBaseText = \"no % from 0\",\r\n  crossesZeroText = \"crossed zero, no %\",\r\n  noChangeText = \"no change\",\r\n  betterLabel = \"better\",\r\n  worseLabel = \"worse\",\r\n  toLabel = \"to\",\r\n  className,\r\n  ...props\r\n}: ChangeReadoutProps) {\r\n  const number = new Intl.NumberFormat(locale, currency ? { style: \"currency\", currency, minimumFractionDigits: 0, maximumFractionDigits: 2 } : { maximumFractionDigits: 2 })\r\n  const magnitude = format ?? ((v: number) => number.format(v))\r\n  const value = (v: number) => (v < 0 ? `${MINUS}${magnitude(-v)}` : magnitude(v)) + (kind === \"percent\" && !format ? \"%\" : \"\")\r\n  const change = describeChange(from, to)\r\n  const tone = change.direction === \"same\" || goodDirection === \"none\" ? \"neutral\" : change.direction === goodDirection ? \"good\" : \"bad\"\r\n  const Icon = change.direction === \"up\" ? TrendingUp : TrendingDown\r\n  const deltaText = signed(change.delta, magnitude(Math.abs(change.delta)))\r\n\r\n  return (\r\n    <span data-slot=\"change-readout\" data-direction={change.direction} data-tone={tone} className={cn(\"inline-flex flex-wrap items-baseline gap-x-1.5 text-sm tabular-nums\", className)} {...props}>\r\n      <span className=\"inline-flex items-baseline gap-x-1.5 whitespace-nowrap\">\r\n        <span className=\"text-muted-foreground\">{value(from)}</span>\r\n        <span aria-hidden className=\"text-muted-foreground\">→</span>\r\n        <span className=\"sr-only\">{toLabel}</span>\r\n        <span className=\"font-medium\">{value(to)}</span>\r\n        <span aria-hidden className=\"text-muted-foreground\">·</span>\r\n      </span>\r\n      {change.direction === \"same\" ? (\r\n        <span className=\"text-muted-foreground\">{noChangeText}</span>\r\n      ) : (\r\n        <span className=\"inline-flex flex-wrap items-baseline gap-x-1.5\">\r\n          <span className={cn(\"inline-flex items-baseline gap-1 font-medium whitespace-nowrap\", tones[tone])}>\r\n            <Icon aria-hidden className=\"size-3.5 self-center\" />\r\n            {kind === \"percent\" ? (\r\n              <abbr title={pointsHint} className=\"no-underline\">\r\n                {deltaText} {pointsLabel}\r\n              </abbr>\r\n            ) : (\r\n              deltaText\r\n            )}\r\n            {tone !== \"neutral\" && <span className=\"sr-only\">({tone === \"good\" ? betterLabel : worseLabel})</span>}\r\n            <span aria-hidden className=\"ml-0.5 font-normal text-muted-foreground\">·</span>\r\n          </span>\r\n          {change.relative === null ? (\r\n            <span className=\"whitespace-nowrap text-muted-foreground italic\">{change.reason === \"zero-base\" ? zeroBaseText : crossesZeroText}</span>\r\n          ) : (\r\n            <span className={cn(\"whitespace-nowrap\", tones[tone])}>\r\n              {formatRelative(change.relative, locale)}\r\n              {change.multiple && <span className=\"text-muted-foreground\"> ({new Intl.NumberFormat(locale, { maximumFractionDigits: 1 }).format(change.multiple)}×)</span>}\r\n            </span>\r\n          )}\r\n        </span>\r\n      )}\r\n    </span>\r\n  )\r\n}\r\n",
      "type": "registry:ui",
      "target": "@ui/change-readout.tsx"
    }
  ],
  "meta": {
    "stops": "Percentage points mistaken for percent",
    "mistakes": [
      "percent vs percentage points",
      "growth from zero",
      "percent change wrong",
      "doubled shown as 100%",
      "negative base percent"
    ],
    "aliases": [
      "percent change",
      "delta",
      "kpi",
      "growth",
      "pts",
      "dashboard",
      "metric card",
      "trend"
    ],
    "flows": [
      "forms"
    ],
    "fields": [
      "number",
      "amount"
    ],
    "summary": "Shows a change both as an amount and a percentage, keeping percentage points apart from percent, so changes aren't misread.",
    "catches": [
      "Percentage points confused with percent",
      "Meaningless percentages from a zero base or a sign change",
      "Misleading signs when the base is negative",
      "\"+150%\" misread as 1.5×, by adding the multiple"
    ],
    "limits": [
      "Formats and labels only; it doesn't know whether the underlying numbers are right",
      "Good/bad depends on the goodDirection you set"
    ],
    "a11y": [
      "Arrows and separators are aria-hidden; a text label reads \"to\" between values",
      "Good or bad is stated in words, not only colour",
      "\"pts\" is an <abbr> with an explanatory title"
    ],
    "usage": "import { ChangeReadout } from \"@/components/ui/change-readout\"",
    "runtimeDependencies": [
      "lucide-react"
    ],
    "zeroRuntimeDependencies": false
  },
  "docs": "https://bollard-ui.vercel.app/components/change-readout",
  "categories": [
    "Reading without mistakes"
  ],
  "type": "registry:ui"
}
