{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "price-breakdown",
  "type": "registry:ui",
  "title": "Price Breakdown",
  "description": "What a price is made of, line by line, and the total: labels at the start, amounts in aligned figures at the end.",
  "categories": [
    "ui"
  ],
  "dependencies": [],
  "registryDependencies": [
    "@rivier-ui/theme",
    "@rivier-ui/announce",
    "@rivier-ui/cn"
  ],
  "files": [
    {
      "path": "registry/ui/price-breakdown.tsx",
      "type": "registry:ui",
      "target": "components/ui/price-breakdown.tsx",
      "content": "\"use client\";\n/**\n * @name price-breakdown\n * @category ui\n * @description What a price is made of, line by line, and the total: labels at the start, amounts in aligned figures at the end.\n * @when A quote, a basket summary, a receipt, a booking's cost before paying. Every amount the total includes gets a line, discounts and fees too. For one figure with a label use stat; for many records in rows and columns use table.\n * @example\n * <PriceBreakdown currency=\"EUR\" locale=\"en-IE\" announceTotal={(total) => `Total is now ${total}`}>\n *   <PriceLine label=\"2 adults\" note=\"€46 each\" amount={92} />\n *   <PriceLine label=\"Service fee\" amount={6} />\n *   <PriceLine label=\"Early booking\" amount={-10} discount />\n *   <PriceTotal label=\"Total\" note=\"Taxes included\" amount={88} />\n * </PriceBreakdown>\n */\nimport { createContext, use, useEffect, useMemo, useRef, type ComponentProps, type ReactNode } from \"react\";\nimport { announce } from \"@/lib/announce\";\nimport { cn } from \"@/lib/cn\";\n\ntype PriceContextValue = {\n  format: (amount: number) => string;\n  announceTotal: ((total: string) => string) | undefined;\n};\n\nconst PriceContext = createContext<PriceContextValue | null>(null);\n\nfunction usePrice(part: string): PriceContextValue {\n  const context = use(PriceContext);\n  if (!context) throw new Error(`<${part}> must be used inside <PriceBreakdown>.`);\n  return context;\n}\n\nexport type PriceBreakdownProps = ComponentProps<\"dl\"> & {\n  /** The currency of every amount, as an ISO 4217 code (\"EUR\"). Used with `locale` unless `formatAmount` is given. */\n  currency?: string;\n  /** The locale the amounts are written in (\"en-IE\", \"de-DE\"). Default: the runtime's. */\n  locale?: string;\n  /** Formats an amount yourself: minor units, a currency per line, \"Free\" for zero. */\n  formatAmount?: (amount: number) => string;\n  /**\n   * When given, a change of the total is said to screen-reader users in full, with the sentence this\n   * returns (\"Total is now €88.00\"). It is not said when the breakdown first appears.\n   */\n  announceTotal?: (total: string) => string;\n};\n\n/** A description list: each line is a term (the label) and its amount, so the pair is read together. */\nexport function PriceBreakdown({ className, currency, locale, formatAmount, announceTotal, children, ...props }: PriceBreakdownProps) {\n  const formatter = useMemo(() => new Intl.NumberFormat(locale, currency ? { style: \"currency\", currency } : { minimumFractionDigits: 2, maximumFractionDigits: 2 }), [locale, currency]);\n  const format = formatAmount ?? ((amount: number): string => formatter.format(amount));\n  return (\n    <PriceContext value={{ format, announceTotal }}>\n      <dl data-slot=\"price-breakdown\" className={cn(\"flex w-full min-w-0 flex-col gap-2.5 text-sm\", className)} {...props}>\n        {children}\n      </dl>\n    </PriceContext>\n  );\n}\n\nexport type PriceLineProps = Omit<ComponentProps<\"div\">, \"children\"> & {\n  /** What the amount is for. */\n  label: ReactNode;\n  /** A second line under the label: how it was worked out, a date, a condition. */\n  note?: ReactNode;\n  /** The amount as a number, formatted by the breakdown. A discount is negative. */\n  amount?: number;\n  /** A reduction: the amount is marked as one. Its minus sign (from a negative `amount`) says so without colour. */\n  discount?: boolean;\n  /** Shown in place of the formatted amount: \"Free\", \"Included\", a struck-through former price. */\n  children?: ReactNode;\n};\n\nexport function PriceLine({ label, note, amount, discount = false, className, children, ...props }: PriceLineProps) {\n  const { format } = usePrice(\"PriceLine\");\n  return (\n    <div data-slot=\"price-line\" data-discount={discount ? \"\" : undefined} className={cn(\"flex min-w-0 items-baseline justify-between gap-4\", className)} {...props}>\n      <dt className=\"min-w-0 text-foreground\">\n        <span className=\"block break-words\">{label}</span>\n        {note ? <span className=\"block text-xs text-muted-foreground\">{note}</span> : null}\n      </dt>\n      <dd data-slot=\"price-amount\" className={cn(\"shrink-0 text-end whitespace-nowrap tabular-nums\", discount && \"font-medium text-success\")}>\n        {children ?? (amount === undefined ? null : format(amount))}\n      </dd>\n    </div>\n  );\n}\n\nexport type PriceTotalProps = PriceLineProps;\n\n/** The total: told apart by a rule above it and its weight. Its amount is what `announceTotal` reads out when it changes. */\nexport function PriceTotal({ label, note, amount, className, children, ...props }: PriceTotalProps) {\n  const { format, announceTotal } = usePrice(\"PriceTotal\");\n  const text = amount === undefined ? null : format(amount);\n  const previous = useRef(text);\n  const say = useRef(announceTotal);\n  useEffect(() => {\n    say.current = announceTotal;\n  });\n  useEffect(() => {\n    if (previous.current === text) return;\n    previous.current = text;\n    if (text !== null && say.current) announce(say.current(text));\n  }, [text]);\n\n  return (\n    <div data-slot=\"price-total\" className={cn(\"mt-1 flex min-w-0 items-baseline justify-between gap-4 border-t border-border-strong pt-3 text-base font-medium\", className)} {...props}>\n      <dt className=\"min-w-0 text-foreground\">\n        <span className=\"block break-words\">{label}</span>\n        {note ? <span className=\"block text-xs font-normal text-muted-foreground\">{note}</span> : null}\n      </dt>\n      <dd data-slot=\"price-amount\" className=\"shrink-0 text-end whitespace-nowrap tabular-nums\">\n        {children ?? text}\n      </dd>\n    </div>\n  );\n}\n"
    }
  ],
  "meta": {
    "category": "ui",
    "when": "A quote, a basket summary, a receipt, a booking's cost before paying. Every amount the total includes gets a line, discounts and fees too. For one figure with a label use stat; for many records in rows and columns use table.",
    "example": "<PriceBreakdown currency=\"EUR\" locale=\"en-IE\" announceTotal={(total) => `Total is now ${total}`}>\n  <PriceLine label=\"2 adults\" note=\"€46 each\" amount={92} />\n  <PriceLine label=\"Service fee\" amount={6} />\n  <PriceLine label=\"Early booking\" amount={-10} discount />\n  <PriceTotal label=\"Total\" note=\"Taxes included\" amount={88} />\n</PriceBreakdown>",
    "client": true
  }
}
