Skip to content

Percent

Percent vs. percentage points, explicitly discriminated.
Implemented
Status
Implemented
Family
1 of 13
Registry
@lopifi/percent
npx shadcn@latest add @lopifi/theme @lopifi/percent

The component, rendering

Real data, the shipped component. Switch the theme to check the claims the spec makes below.

Preview

The four-layer spec

Percent and percentage points are different numbers. This component makes conflating them impossible.

<Percent value={{ kind: "relative", value: 25.0 }} />   // +25.0%
<Percent value={{ kind: "points", value: 5.0 }} />      // +5.0pp
<Percent value={{ kind: "bps", value: 25 }} />          // +25 bps

No default kind. The discriminator is required. Division by zero → n/a, never Infinity.

Precision by kind: relative/points 1dp · bps 0dp · rate 3dp.

Lifted verbatim from Primitives. The doc and this page are the same paragraphs, so they cannot disagree.

What lands in your repository

One component is never one file. Everything below is copied into your project, where you own it.

Files

components/ledger/percent.tsx

Lopifi dependencies · 3
cnformat-moneymoney.types
npm packages
clsxtailwind-merge
Registry entry
/r/percent.json

The whole component

No compiled bundle, no black box. This is the file the registry copies, and the file you will edit.

components/ledger/percent.tsx
import * as React from "react";
import { cn } from "@/lib/ledger/cn";
import { formatPercent } from "@/lib/ledger/format-money";
import type { PercentChange } from "@/lib/ledger/money.types";

export interface PercentProps extends Omit<React.ComponentPropsWithoutRef<"span">, "children"> {
  signed?: boolean;
  size?: "xs" | "sm" | "md";
  /** No default kind. Conflating percent with percentage points is the bug. */
  value: PercentChange | null;
}

const SIZES = {
  md: "text-figure-md font-medium",
  sm: "text-figure-sm",
  xs: "text-figure-xs",
} as const;

/**
 * A margin moving 20% → 25% rose 5 percentage points and 25% relative.
 * Both are correct; they are different numbers. The discriminator is required.
 *
 * Division by zero renders `n/a` — never Infinity, never NaN, never 100%.
 */
export const Percent = React.forwardRef<HTMLSpanElement, PercentProps>(
  ({ className, value, signed = true, size = "sm", ...props }, ref) => {
    if (value == null || !Number.isFinite(value.value)) {
      return (
        <span
          className={cn("numeric relative text-text-tertiary", SIZES[size], className)}
          ref={ref}
          {...props}
        >
          <span aria-hidden="true">n/a</span>
          <span className="sr-only select-none">not available</span>
        </span>
      );
    }

    const text = formatPercent(value, { signed });
    const spoken =
      value.kind === "points"
        ? `${text.replace("pp", " percentage points")}`
        : value.kind === "bps"
          ? text.replace(" bps", " basis points")
          : text.replace("%", " percent");

    return (
      <span className={cn("numeric relative whitespace-nowrap", SIZES[size], className)} ref={ref} {...props}>
        <span aria-hidden="true">{text}</span>
        {/* Spoken text, not an aria-label: a label on a role-less span is
            prohibited ARIA. select-none keeps it out of a copied selection. */}
        <span className="sr-only select-none">{spoken.replace("−", "negative ")}</span>
      </span>
    );
  },
);
Percent.displayName = "Percent";

Also in Primitives

See also

A component is specified in one place, defined in another, and running in a third. All three, from here.

Elsewhere in the system

Where Percent sits in the rest of the system — the live section it renders in, the calculators that compute what it displays, and the ledger payloads it is fed from.

Components are copied into your repository and meant to be edited — rounding policy, fiscal calendar and compliance copy differ per company.

Told when Primitives changes

Components are copied into your repo, so a fix here does not reach you on its own. One email when a component in this family changes in a way that needs a migration — and nothing when it does not.

One address, no list rental, unsubscribe in one click.

Works in your QuickBooks Online or Xero file

Get your books closed every month

Lopifi is building AI bookkeeping that keeps your books closed on US GAAP as transactions happen, with a CPA review every quarter, for $200 a month. We’re not open yet.

$200/month when we open. No card now.