Skip to content

DensityToggle

Persisted, synced, phone-clamped.
Implemented
Status
Implemented
Family
8 of 13
Registry
@lopifi/density-toggle
npx shadcn@latest add @lopifi/theme @lopifi/density-toggle

The component, rendering

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

Preview

The four-layer spec

compact / default / comfortable. Persisted per user, synced across devices, clamped to comfortable on phone regardless of the stored preference — 32px rows with 13px text fails both touch targets and arm's-length legibility.

Sets data-density on a container; every list component reads it.

Lifted verbatim from System & states. 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/density-toggle.tsx

Lopifi dependencies · 3
cnmoney.typesuse-media-query
npm packages
clsxtailwind-merge
Registry entry
/r/density-toggle.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/density-toggle.tsx
"use client";

import { useIsPhone } from "@/hooks/ledger/use-media-query";
import { cn } from "@/lib/ledger/cn";
import type { Density } from "@/lib/ledger/money.types";

export interface DensityToggleProps {
  className?: string;
  /** Off when the surface has no rows to set the density of. */
  disabled?: boolean;
  /** Said in the tooltip, so a dead control explains itself rather than sulking. */
  disabledReason?: string;
  onValueChange: (d: Density) => void;
  value: Density;
}

const OPTIONS: { key: Density; label: string }[] = [
  { key: "compact", label: "Compact" },
  { key: "default", label: "Default" },
  { key: "comfortable", label: "Comfortable" },
];

/**
 * Persisted per user, and clamped to `comfortable` on a phone regardless of
 * the stored preference — 32px rows with 13px text fails both touch targets
 * and arm's-length legibility.
 */
export function DensityToggle({
  value,
  onValueChange,
  className,
  disabled,
  disabledReason,
}: DensityToggleProps) {
  const isPhone = useIsPhone();

  return (
    <div
      aria-disabled={disabled || undefined}
      aria-label="Row density"
      className={cn("flex rounded-sm border border-border p-0.5", className)}
      role="radiogroup"
      title={disabled ? disabledReason : undefined}
    >
      {OPTIONS.map((o) => {
        const clamped = disabled || (isPhone && o.key !== "comfortable");
        return (
          <button
            aria-checked={isPhone ? o.key === "comfortable" : value === o.key}
            className={cn(
              "press h-7 rounded-xs px-2.5 text-label font-medium",
              (isPhone ? o.key === "comfortable" : value === o.key)
                ? "bg-surface-selected text-text-primary"
                : "text-text-secondary hover:bg-surface-hover",
              clamped && "cursor-not-allowed opacity-40",
            )}
            disabled={clamped}
            key={o.key}
            onClick={() => onValueChange(o.key)}
            role="radio"
            title={disabled ? disabledReason : clamped ? "Clamped to comfortable on a phone" : undefined}
            type="button"
          >
            {o.label}
          </button>
        );
      })}
    </div>
  );
}

Also in System & states

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 DensityToggle 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 System & states 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.