{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "metric-value",
  "type": "registry:component",
  "title": "Metric value",
  "description": "Format large totals consistently while keeping the exact figure available on hover.",
  "categories": [
    "metrics"
  ],
  "dependencies": [],
  "registryDependencies": [
    "tooltip"
  ],
  "files": [
    {
      "path": "src/registry/overtrue/metric-value.tsx",
      "type": "registry:component",
      "target": "components/overtrue/metric-value.tsx",
      "content": "/** Adapted from dashboardcn (MIT), copyright 2026 dashboardcn contributors.\n * Source: https://github.com/NoahGdev/dashboardcn/tree/b1ffacbca0f7a0be60135c8665b9e8c267205c48\n *\n * MIT License\n *\n * Copyright (c) 2026 dashboardcn contributors\n *\n * Permission is hereby granted, free of charge, to any person obtaining a copy\n * of this software and associated documentation files (the \"Software\"), to deal\n * in the Software without restriction, including without limitation the rights\n * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n * copies of the Software, and to permit persons to whom the Software is\n * furnished to do so, subject to the following conditions:\n *\n * The above copyright notice and this permission notice shall be included in all\n * copies or substantial portions of the Software.\n *\n * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n * SOFTWARE.\n *\n * ---\n *\n * Portions of this project are derived from shadcn/ui and shadcn/registry-template\n * (https://github.com/shadcn-ui/ui), Copyright (c) 2023 shadcn, licensed under the\n * MIT License. The notice above applies to those portions as well.\n */\n\"use client\";\n\nimport * as React from \"react\";\n\nimport { cn } from \"@/lib/utils\";\nimport { formatNumber, type NumberFormat } from \"./metric-format\";\nimport {\n  Tooltip,\n  TooltipContent,\n  TooltipProvider,\n  TooltipTrigger,\n} from \"@/components/ui/tooltip\";\n\n/** Values at or above this are abbreviated by default. */\nexport const DEFAULT_COMPACT_FROM = 100_000;\n\nexport interface MetricValueProps extends Omit<\n  React.ComponentProps<\"span\">,\n  \"children\"\n> {\n  /** Strings render as-is. */\n  value: number | string;\n  format?: NumberFormat;\n  currency?: string;\n  maximumFractionDigits?: number;\n  /** Values at or above this are abbreviated (e.g. $158K) with the full value in a tooltip. Set to Infinity to always show the full value. */\n  compactFrom?: number;\n  /** Text after the number, e.g. a unit. */\n  suffix?: React.ReactNode;\n}\n\n// Units and currency symbols sit quieter than the digits they qualify, in\n// whatever color the figure has. They never drop below 12px, and a currency\n// symbol rises toward the cap height only once the figure is large.\nconst quiet = \"font-medium tracking-normal text-current/55\";\nconst unitSize = \"text-[length:max(0.6em,0.75rem)]\";\nconst symbolSize =\n  \"mr-[0.04em] text-[length:max(0.62em,0.75rem)] align-[calc((1em_-_0.75rem)*1.1)]\";\n\n/**\n * Split \"$1,234.5\", \"-€12\", \"12.4%\" or \"2.4 days\" into a quiet symbol, the\n * digits, and a quiet unit. Magnitude letters (K, M, B) stay with the digits.\n * Anything else, such as \"Q3 2026\", renders untouched.\n */\nfunction typeset(text: string) {\n  const match =\n    /^([+\\-\\u2212]?)([^\\d\\s+\\-\\u2212]{0,3})(\\d(?:[\\d.,\\u202f\\u00a0]*\\d)?[KkMBT]?)(%|\\s+\\S.*)?$/.exec(\n      text,\n    );\n  if (!match || /[a-z]/i.test(match[2].replace(/[A-Z]{1,2}(?=\\$)/, \"\")))\n    return text;\n  const [, sign, symbol, digits, unit] = match;\n  return (\n    <>\n      {sign}\n      {symbol ? (\n        <span data-slot=\"metric-value-affix\" className={cn(quiet, symbolSize)}>\n          {symbol}\n        </span>\n      ) : null}\n      {digits}\n      {unit ? (\n        <span data-slot=\"metric-value-unit\" className={cn(quiet, unitSize)}>\n          {unit}\n        </span>\n      ) : null}\n    </>\n  );\n}\n\nfunction Unit({ children }: { children: React.ReactNode }) {\n  if (children === null || children === undefined || children === false)\n    return null;\n  return (\n    <span data-slot=\"metric-value-unit\" className={cn(quiet, unitSize)}>\n      {children}\n    </span>\n  );\n}\n\n/** A formatted number. Large values are abbreviated and reveal the full value on hover. */\nfunction MetricValue({\n  value,\n  format,\n  currency,\n  maximumFractionDigits,\n  compactFrom = DEFAULT_COMPACT_FROM,\n  suffix,\n  className,\n  ...props\n}: MetricValueProps) {\n  const classes = cn(\"tabular-nums tracking-[-0.02em]\", className);\n\n  if (typeof value !== \"number\") {\n    return (\n      <span data-slot=\"metric-value\" className={classes} {...props}>\n        {typeset(value)}\n        <Unit>{suffix}</Unit>\n      </span>\n    );\n  }\n\n  const full = formatNumber(value, { format, currency, maximumFractionDigits });\n  const abbreviate = format !== \"percent\" && Math.abs(value) >= compactFrom;\n  if (!abbreviate) {\n    return (\n      <span data-slot=\"metric-value\" className={classes} {...props}>\n        {typeset(full)}\n        <Unit>{suffix}</Unit>\n      </span>\n    );\n  }\n\n  return (\n    <TooltipProvider>\n      <Tooltip>\n        <TooltipTrigger asChild>\n          <span\n            data-slot=\"metric-value\"\n            data-compact=\"\"\n            className={cn(\n              classes,\n              \"cursor-default decoration-current/30 decoration-dotted underline-offset-[0.18em] hover:underline\",\n            )}\n            {...props}\n          >\n            {typeset(formatNumber(value, { format, currency, compact: true }))}\n            <Unit>{suffix}</Unit>\n          </span>\n        </TooltipTrigger>\n        <TooltipContent className=\"font-medium tabular-nums\">\n          {full}\n          {suffix}\n        </TooltipContent>\n      </Tooltip>\n    </TooltipProvider>\n  );\n}\n\nexport { MetricValue };\n"
    },
    {
      "path": "src/registry/overtrue/metric-format.tsx",
      "type": "registry:component",
      "target": "components/overtrue/metric-format.tsx",
      "content": "/** Adapted from dashboardcn (MIT), copyright 2026 dashboardcn contributors.\n * Source: https://github.com/NoahGdev/dashboardcn/tree/b1ffacbca0f7a0be60135c8665b9e8c267205c48\n *\n * MIT License\n *\n * Copyright (c) 2026 dashboardcn contributors\n *\n * Permission is hereby granted, free of charge, to any person obtaining a copy\n * of this software and associated documentation files (the \"Software\"), to deal\n * in the Software without restriction, including without limitation the rights\n * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n * copies of the Software, and to permit persons to whom the Software is\n * furnished to do so, subject to the following conditions:\n *\n * The above copyright notice and this permission notice shall be included in all\n * copies or substantial portions of the Software.\n *\n * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n * SOFTWARE.\n *\n * ---\n *\n * Portions of this project are derived from shadcn/ui and shadcn/registry-template\n * (https://github.com/shadcn-ui/ui), Copyright (c) 2023 shadcn, licensed under the\n * MIT License. The notice above applies to those portions as well.\n */\nexport type NumberFormat = \"number\" | \"compact\" | \"currency\" | \"percent\";\n\nexport interface FormatNumberOptions {\n  format?: NumberFormat;\n  /** ISO 4217 code, used when format is \"currency\". Defaults to USD. */\n  currency?: string;\n  locale?: string;\n  maximumFractionDigits?: number;\n  /** Abbreviate large values, e.g. \"$158K\" instead of \"$158,143\". */\n  compact?: boolean;\n}\n\nconst formatterCache = new Map<string, Intl.NumberFormat>();\n\nfunction getFormatter(locale: string, options: Intl.NumberFormatOptions) {\n  const key = locale + JSON.stringify(options);\n  let formatter = formatterCache.get(key);\n  if (!formatter) {\n    formatter = new Intl.NumberFormat(locale, options);\n    formatterCache.set(key, formatter);\n  }\n  return formatter;\n}\n\n/**\n * Format a metric value for display.\n *\n * formatNumber(1234567)                          -> \"1,234,567\"\n * formatNumber(1234567, { format: \"compact\" })   -> \"1.2M\"\n * formatNumber(48.2, { format: \"currency\" })     -> \"$48.20\"\n * formatNumber(158143, { format: \"currency\", compact: true }) -> \"$158K\"\n * formatNumber(0.124, { format: \"percent\" })     -> \"12.4%\"\n */\nexport function formatNumber(\n  value: number,\n  {\n    format = \"number\",\n    currency = \"USD\",\n    locale = \"en-US\",\n    maximumFractionDigits,\n    compact = false,\n  }: FormatNumberOptions = {},\n): string {\n  if (!Number.isFinite(value)) return \"—\";\n\n  // Compact values keep three significant digits: $158K, $1.23M, 41.2K.\n  const compactOptions: Intl.NumberFormatOptions = compact\n    ? maximumFractionDigits === undefined\n      ? { notation: \"compact\", maximumSignificantDigits: 3 }\n      : { notation: \"compact\", maximumFractionDigits }\n    : {};\n  switch (format) {\n    case \"compact\":\n      return getFormatter(locale, {\n        notation: \"compact\",\n        maximumFractionDigits: maximumFractionDigits ?? 1,\n      }).format(value);\n    case \"currency\":\n      return getFormatter(locale, {\n        style: \"currency\",\n        currency,\n        maximumFractionDigits: maximumFractionDigits ?? 2,\n        ...compactOptions,\n      }).format(value);\n    case \"percent\":\n      return getFormatter(locale, {\n        style: \"percent\",\n        maximumFractionDigits: maximumFractionDigits ?? 1,\n      }).format(value);\n    default:\n      return getFormatter(locale, {\n        maximumFractionDigits: maximumFractionDigits ?? 0,\n        ...compactOptions,\n      }).format(value);\n  }\n}\n\n/**\n * Format a fractional change as a signed percentage.\n *\n * formatDelta(0.124)  -> \"+12.4%\"\n * formatDelta(-0.03)  -> \"-3.0%\"\n * formatDelta(0)      -> \"0.0%\"\n */\nexport function formatDelta(delta: number, locale = \"en-US\"): string {\n  if (!Number.isFinite(delta)) return \"—\";\n  return getFormatter(locale, {\n    style: \"percent\",\n    signDisplay: \"exceptZero\",\n    maximumFractionDigits: 1,\n    minimumFractionDigits: 1,\n  }).format(delta);\n}\n\n/**\n * Fractional change between two values. Returns 0 when there is no baseline.\n */\nexport function computeDelta(current: number, previous: number): number {\n  if (!previous) return 0;\n  return (current - previous) / Math.abs(previous);\n}\n"
    }
  ],
  "docs": "Documentation: https://ui.overtrue.xyz/components/metric-value. Requires an initialized shadcn/ui project with Tailwind CSS v4."
}
