{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"tax-basis","title":"Tax basis","description":"Whether a price includes tax, as a badge the price sits inside: filled when the tax is in the figure, hollow when it is left out. Hover explains the tax (GST, VAT, GST/HST, sales tax), its rate and the price on the other basis. Show, hide, or print both bases.","registryDependencies":["tooltip","https://ui.twelfth.ai/r/retail-format.json"],"files":[{"path":"src/registry/catman/ui/tax-basis.tsx","content":"\"use client\"\n\nimport type { ReactNode } from \"react\"\n\nimport { Tooltip, TooltipContent, TooltipTrigger } from \"@/components/ui/tooltip\"\nimport { cn } from \"@/lib/utils\"\nimport { formatMoney, type RetailLocale } from \"@/registry/catman/lib/retail-format\"\n\n/**\n * Whether a price includes tax, as a badge the price sits inside, so the figure\n * and its basis read as one object. Filled means the tax is in the figure;\n * hollow means it is left out. The shape tells inc from ex at a glance in a\n * mixed column, and the word in it still says which, so neither colour nor\n * shape alone carries meaning. Hover (or focus) explains the tax: its full\n * name, the rate and the same price on the other basis, when those are known.\n *\n * Works for any tax label: GST, VAT, GST/HST and SST print as they are; longer\n * labels (\"Sales tax\", \"Consumption tax\") print as \"tax\" in the mark and in\n * full in its title and screen-reader text.\n *\n * Part of the twelfth catman registry (`@twelfth/tax-basis`).\n */\nexport type TaxBasis = \"inclusive\" | \"exclusive\"\n\n/** How a figure carries its basis. `show`: the figure and its mark. `hide`:\n * the figure alone, for a column whose header already names the basis.\n * `both`: the figure, then the same price on the other basis, each marked. */\nexport type TaxBasisDisplay = \"show\" | \"hide\" | \"both\"\n\n/** \"including GST\" / \"excluding VAT\": what a screen reader hears. */\nexport function taxBasisWords(basis: TaxBasis, taxLabel = \"GST\"): string {\n  return `${basis === \"inclusive\" ? \"including\" : \"excluding\"} ${taxLabel}`\n}\n\n/** The label as the mark prints it: a short single-word label as given, any\n * other as \"tax\". The full label stays in the title and spoken text. */\nfunction markLabel(taxLabel: string): string {\n  return taxLabel.length <= 7 && !/\\s/.test(taxLabel) ? taxLabel : \"tax\"\n}\n\n/**\n * The same price on the other basis, at the given rate. Null when the basis or\n * the rate is unknown, or the rate is not a plausible tax rate: a converted\n * figure at a guessed rate is worse than no figure.\n */\nexport function otherBasisPrice(value: number | null, basis: TaxBasis | null | undefined, taxRatePct: number | null | undefined): number | null {\n  if (value === null || !Number.isFinite(value) || !basis) return null\n  if (taxRatePct === null || taxRatePct === undefined || !Number.isFinite(taxRatePct) || taxRatePct < 0 || taxRatePct > 30) return null\n  const factor = 1 + taxRatePct / 100\n  return basis === \"exclusive\" ? value * factor : value / factor\n}\n\n/** The full name behind a short label, for the hover. Labels that are already\n * words (\"Sales tax\") explain themselves. */\nconst TAX_NAMES: Record<string, string> = {\n  GST: \"Goods and Services Tax\",\n  VAT: \"Value Added Tax\",\n  \"GST/HST\": \"Goods and Services Tax / Harmonized Sales Tax\",\n  HST: \"Harmonized Sales Tax\",\n  SST: \"Sales and Service Tax\",\n}\n\n/** How the badge explains itself. `focus`: a tooltip on hover and keyboard\n * focus. `hover`: pointer only, for a badge inside a link or button, which\n * must not hold a second focus stop. `false`: none, for a badge already inside\n * a tooltip. The spoken text names the basis in every case. */\nexport type TaxBasisExplain = \"focus\" | \"hover\" | false\n\nconst BADGE = \"inline-flex shrink-0 items-center gap-1 rounded-[4px] border border-current/35 whitespace-nowrap\"\nconst TAG = \"font-mono text-[9.5px] font-medium uppercase leading-[13px] tracking-[0.04em]\"\n/** Both tones derive from the text colour around them, so the badge reads on a\n * card, in a header and inside a tooltip without a variant. */\nconst TONE: Record<TaxBasis, string> = {\n  inclusive: \"bg-current/12\",\n  exclusive: \"\",\n}\n\nfunction Explanation({ basis, taxLabel, taxRatePct, other, note }: { basis: TaxBasis; taxLabel: string; taxRatePct?: number | null; other?: string | null; note?: ReactNode }) {\n  const name = TAX_NAMES[taxLabel]\n  const rate = taxRatePct != null && Number.isFinite(taxRatePct) && taxRatePct >= 0 && taxRatePct <= 30 ? taxRatePct : null\n  return (\n    <span className=\"flex flex-col gap-0.5\">\n      <span className=\"font-medium\">{basis === \"inclusive\" ? `Includes ${taxLabel}` : `Excludes ${taxLabel}`}</span>\n      {name ? <span className=\"opacity-80\">{taxLabel}: {name}</span> : null}\n      {rate !== null ? <span className=\"opacity-80 tabular-nums\">Rate {rate}%</span> : null}\n      {other ? <span className=\"tabular-nums\">{basis === \"inclusive\" ? \"Ex\" : \"Inc\"} {taxLabel}: {other}</span> : null}\n      {note ? <span className=\"opacity-80\">{note}</span> : null}\n    </span>\n  )\n}\n\n/**\n * The badge. With `children` (the figure) it holds the price and its basis\n * together; without, it is the basis alone, for a column header or a heading\n * that names it once for every figure under it.\n */\nexport function TaxBasisMark({\n  basis,\n  taxLabel = \"GST\",\n  short = false,\n  taxRatePct,\n  other,\n  note,\n  explain = \"focus\",\n  children,\n  className,\n}: {\n  basis: TaxBasis\n  /** The workspace's word for the tax: \"GST\", \"VAT\", \"GST/HST\", \"Sales tax\"… */\n  taxLabel?: string\n  /** \"inc\" / \"ex\" only, where width is tight. The hover and spoken text keep the label. */\n  short?: boolean\n  /** Shown in the hover when known; never assumed. */\n  taxRatePct?: number | null\n  /** The same price on the other basis, formatted, for the hover. */\n  other?: string | null\n  /** A further line for the hover, e.g. how the figure was converted. */\n  note?: ReactNode\n  explain?: TaxBasisExplain\n  /** The figure the badge holds. */\n  children?: ReactNode\n  className?: string\n}) {\n  const words = taxBasisWords(basis, taxLabel)\n  const face = (\n    <>\n      {children !== undefined ? <span className=\"tabular-nums\">{children}</span> : null}\n      <span aria-hidden className={cn(TAG, children !== undefined && \"opacity-75\")}>{basis === \"inclusive\" ? \"inc\" : \"ex\"}{short ? null : ` ${markLabel(taxLabel)}`}</span>\n      <span className=\"sr-only\">{words}</span>\n    </>\n  )\n  const badge = cn(BADGE, TONE[basis], children !== undefined ? \"px-1\" : \"px-[3px]\", className)\n  if (!explain) return <span data-tax-basis={basis} className={badge}>{face}</span>\n  return (\n    <Tooltip>\n      <TooltipTrigger\n        delay={150}\n        render={<span data-tax-basis={basis} tabIndex={explain === \"focus\" ? 0 : undefined} />}\n        className={cn(badge, \"cursor-help outline-none focus-visible:ring-2 focus-visible:ring-ring\")}\n      >\n        {face}\n      </TooltipTrigger>\n      <TooltipContent side=\"top\" className=\"max-w-64 text-xs normal-case tracking-normal\">\n        <Explanation basis={basis} taxLabel={taxLabel} taxRatePct={taxRatePct} other={other} note={note} />\n      </TooltipContent>\n    </Tooltip>\n  )\n}\n\n/**\n * A money figure inside its tax-basis badge. Pass the basis the figure is on;\n * with `taxBasis=\"both\"` and a rate, the same price on the other basis follows\n * in its own badge, muted, so an ex-tax figure can be checked against a shelf\n * that shows inc. Without a rate, \"both\" shows the one figure; without a basis,\n * the figure stands alone with no badge.\n */\nexport function PriceFigure({\n  value,\n  basis,\n  taxLabel = \"GST\",\n  taxRatePct = null,\n  taxBasis = \"show\",\n  short = false,\n  stacked = false,\n  explain = \"focus\",\n  note,\n  format,\n  locale,\n  currency,\n  className,\n}: {\n  /** The price, on `basis`. null prints \"—\". */\n  value: number | null\n  /** The basis `value` is on. null or undefined when the source did not say. */\n  basis: TaxBasis | null | undefined\n  taxLabel?: string\n  /** The rate behind `taxBasis=\"both\"` and the hover, in percent (10 for 10%). */\n  taxRatePct?: number | null\n  taxBasis?: TaxBasisDisplay\n  /** Badges print \"inc\" / \"ex\" without the label. */\n  short?: boolean\n  /** With \"both\", the second figure goes under the first instead of beside it. */\n  stacked?: boolean\n  explain?: TaxBasisExplain\n  /** A further line for the hover. */\n  note?: ReactNode\n  /** Formats each figure; defaults to `formatMoney` with `locale` and `currency`. */\n  format?: (value: number) => string\n  className?: string\n} & RetailLocale) {\n  if (value === null || !Number.isFinite(value)) return <span className={cn(\"tabular-nums\", className)}>—</span>\n  const print = format ?? ((amount: number) => formatMoney(amount, { locale, currency }))\n  const marked = taxBasis !== \"hide\" && (basis === \"inclusive\" || basis === \"exclusive\") ? basis : null\n  if (!marked) return <span className={cn(\"tabular-nums\", className)}>{print(value)}</span>\n  const other = otherBasisPrice(value, marked, taxRatePct)\n  const otherBasis: TaxBasis = marked === \"exclusive\" ? \"inclusive\" : \"exclusive\"\n  const shared = { taxLabel, short, taxRatePct, explain }\n  const first = <TaxBasisMark basis={marked} other={other === null ? null : print(other)} note={note} {...shared}>{print(value)}</TaxBasisMark>\n  if (taxBasis !== \"both\" || other === null) return <span className={cn(\"inline-flex\", className)}>{first}</span>\n  return (\n    <span className={cn(\"inline-flex\", stacked ? \"flex-col items-end gap-0.5\" : \"flex-wrap items-center gap-x-1.5 gap-y-0.5\", className)}>\n      {first}\n      <TaxBasisMark basis={otherBasis} other={print(value)} className=\"text-[0.92em] font-normal opacity-75\" {...shared}>{print(other)}</TaxBasisMark>\n    </span>\n  )\n}\n","type":"registry:ui"}],"type":"registry:ui"}