Skip to content

Delta

A signed change, with polarity and a redundant non-color cue.
Implemented
Status
Implemented
Family
1 of 13
Registry
@lopifi/delta
npx shadcn@latest add @lopifi/theme @lopifi/delta

The component, rendering

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

Preview

The four-layer spec

A signed change. Never colored without a redundant cue.

<Delta value={{ amount: 12400, currency: "USD" }} percent={5.3} polarity="standard" />
// ↑ +$12,400.00 (+5.3%)
<Delta value={{ amount: 8200, currency: "USD" }} polarity="inverted" />
// ↑ +$8,200.00   ← colored NEGATIVE, because up is bad for this metric
interface DeltaProps {
  value: MoneyValue | null;
  percent?: PercentChange;              // { kind: "points" | "relative"; value: number }
  polarity?: "standard" | "inverted" | "neutral";
  showArrow?: boolean;                  // default true
  showPercent?: boolean;
  basis?: string;                       // "vs. Aug" — never omit the comparison basis
}

The polarity contract is the whole point. A -4.2% change in burn is good. Defaulting everything to standard is how dashboards report cheerful green on a cost overrun.

Structure: [arrow] [sign] [value] [(percent)] [basis]. Sign glyph slot reserved so a column of deltas aligns even when some are unchanged.

Zero delta: — with --neutral-value, no arrow. Not +0.0%.

Motion: none. Value flash when live.

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/delta.tsx

Lopifi dependencies · 6
cnformat-moneymoneymoney.typespercenttooltip
npm packages
@base-ui-components/reactclass-variance-authorityclsxtailwind-merge
Registry entry
/r/delta.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/delta.tsx
import * as React from "react";
import { cn } from "@/lib/ledger/cn";
import { isZero } from "@/lib/ledger/format-money";
import type { MoneyValue, PercentChange, Polarity } from "@/lib/ledger/money.types";
import { Money } from "@/components/ledger/money";
import { Percent } from "@/components/ledger/percent";
import { Tooltip } from "@/components/ledger/tooltip";

export interface DeltaProps extends Omit<React.ComponentPropsWithoutRef<"span">, "children"> {
  /** Never omit the comparison basis. A bare "+5.3%" answers nothing. */
  basis?: string;
  /**
   * The basis, carried on hover instead of inline. For dense surfaces — a card
   * meta row, a KPI tile — where the visible basis is what turns a two-item
   * row into a wrapping sentence. Ignored when `basis` is also given, because
   * saying it twice is worse than either.
   */
  basisTooltip?: string;
  percent?: PercentChange;
  /** Percent-only delta, for metrics with no monetary magnitude. */
  percentOnly?: boolean;
  /** Required in practice: up is not assumed good. */
  polarity?: Polarity;
  showArrow?: boolean;
  size?: "xs" | "sm" | "md";
  value: MoneyValue | null;
}

/**
 * A signed change, never coloured without a redundant cue.
 *
 * The sign glyph slot is reserved so a column of deltas aligns even when
 * some are unchanged, and the arrow is a second non-chromatic signal so
 * the whole thing survives greyscale.
 */
export const Delta = React.forwardRef<HTMLSpanElement, DeltaProps>(
  (
    {
      className,
      value,
      percent,
      polarity = "standard",
      showArrow = true,
      basis,
      basisTooltip,
      size = "sm",
      percentOnly = false,
      ...props
    },
    ref,
  ) => {
    const magnitude = percentOnly ? (percent?.value ?? 0) : numberOf(value);
    const unchanged = value == null ? percent == null || percent.value === 0 : isZero(value);

    const hint = basis ? undefined : basisTooltip;

    if (unchanged) {
      return (
        <Tooltip content={hint}>
          <span
            className={cn("numeric relative inline-flex items-baseline gap-1", sizeClass(size), className)}
            ref={ref}
            style={{ color: "var(--neutral-value)" }}
            {...props}
          >
            <span aria-hidden="true">—</span>
            <span className="sr-only select-none">{hint ? `unchanged, ${hint}` : "unchanged"}</span>
            {basis ? <BasisText>{basis}</BasisText> : null}
          </span>
        </Tooltip>
      );
    }

    const up = magnitude > 0;
    const toneClass =
      polarity === "neutral"
        ? "text-text-secondary"
        : (polarity === "standard") === up
          ? "text-positive"
          : "text-negative";

    return (
      <Tooltip content={hint}>
        <span
          aria-description={hint}
          className={cn(
            "numeric inline-flex flex-wrap items-baseline gap-1",
            sizeClass(size),
            toneClass,
            className,
          )}
          ref={ref}
          {...props}
        >
          {/* The signed run stays one unbreakable unit; only a trailing basis is
            allowed to fall to the next line. Previously `nowrap` sat on the
            whole span, so a basis clause forced the entire delta to overflow. */}
          <span className="inline-flex items-baseline gap-1 whitespace-nowrap">
            {showArrow ? (
              <span aria-hidden="true" className="text-[0.85em] leading-none">
                {up ? "↑" : "↓"}
              </span>
            ) : null}

            {percentOnly ? null : (
              <Money
                className="font-inherit"
                size={size === "md" ? "md" : size === "sm" ? "sm" : "xs"}
                tone="inherit"
                value={value}
              />
            )}

            {percent ? (
              <span className={percentOnly ? undefined : "text-[0.95em]"}>
                {percentOnly ? null : <span aria-hidden="true">(</span>}
                <Percent className="text-inherit" size={size === "md" ? "md" : "xs"} value={percent} />
                {percentOnly ? null : <span aria-hidden="true">)</span>}
              </span>
            ) : null}
          </span>

          {basis ? <BasisText>{basis}</BasisText> : null}
        </span>
      </Tooltip>
    );
  },
);
Delta.displayName = "Delta";

function BasisText({ children }: { children: React.ReactNode }) {
  return <span className="font-normal text-caption text-text-tertiary">{children}</span>;
}

function sizeClass(size: "xs" | "sm" | "md") {
  return size === "md" ? "text-figure-md font-medium" : size === "sm" ? "text-figure-sm" : "text-figure-xs";
}

function numberOf(value: MoneyValue | null): number {
  if (!value) return 0;
  return typeof value.amount === "bigint" ? Number(value.amount) : value.amount;
}

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 Delta 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.