# Usage and billing

A workspace billing page: the current plan in a `Card` with its renewal date and a `Modal` to change it, the masked payment method, usage meters for API calls, seats and storage, and a billing history `Table` with invoice downloads.

**Example: Fernhill billing** (`examples/recipes/usage-billing.tsx`)

```tsx
'use client'

import {
  Badge,
  BodySmall,
  Button,
  Card,
  Heading,
  Link,
  Modal,
  Radio,
  RadioGroup,
  Stack,
  Table,
  TableBody,
  TableCell,
  TableContainer,
  TableHead,
  TableHeader,
  TableRow,
  Text,
  toastSuccess,
} from '@blocks/design-system'
import { AlertTriangle, CreditCard, Download } from 'lucide-react'
import { type FormEvent, useId, useState } from 'react'
import styles from './usage-billing.module.css'

const plans = {
  starter: { name: 'Starter', price: 49, limits: { calls: 25_000, seats: 3, storage: 20 } },
  team: { name: 'Team', price: 240, limits: { calls: 100_000, seats: 10, storage: 100 } },
  business: { name: 'Business', price: 690, limits: { calls: 500_000, seats: 30, storage: 500 } },
}

type PlanId = keyof typeof plans
type Resource = keyof (typeof plans)[PlanId]['limits']

const seatsInUse = 9

const usage: { id: Resource; label: string; used: number; unit: string }[] = [
  { id: 'calls', label: 'API calls', used: 84_210, unit: '' },
  { id: 'seats', label: 'Seats', used: seatsInUse, unit: '' },
  { id: 'storage', label: 'Storage', used: 32.4, unit: ' GB' },
]

const invoices = [
  { id: 'INV-2026-0098', date: '2026-11-04', label: 'Nov 4, 2026', amount: '$240.00' },
  { id: 'INV-2026-0085', date: '2026-10-04', label: 'Oct 4, 2026', amount: '$262.50' },
  { id: 'INV-2026-0071', date: '2026-09-04', label: 'Sep 4, 2026', amount: '$240.00' },
  { id: 'INV-2026-0058', date: '2026-08-04', label: 'Aug 4, 2026', amount: '$240.00' },
]

// Past this share of the limit a meter turns to the warning color and says so in words.
const warnAt = 0.8

export default function UsageBilling() {
  const [plan, setPlan] = useState<PlanId>('team')
  const [managing, setManaging] = useState(false)
  const id = useId()
  const current = plans[plan]

  function changePlan(event: FormEvent<HTMLFormElement>) {
    event.preventDefault()
    if (!managing) return
    const next = String(new FormData(event.currentTarget).get('plan')) as PlanId
    setManaging(false)
    if (next === plan) return
    setPlan(next)
    toastSuccess(`Switched to ${plans[next].name}. The change shows on your next invoice.`)
  }

  return (
    <Stack gap="xl" dangerouslySetClassName={styles.billing}>
      <div className={styles.summary}>
        <Card as="section" padding="lg" aria-labelledby={`${id}-plan`}>
          <Stack gap="md" height="full" justify="space-between">
            <Stack gap="xs">
              <Heading as="h2" id={`${id}-plan`} fontSize="sm" fontWeight="medium" color="text-muted">
                Current plan
              </Heading>
              <Stack direction="row" align="baseline" gap="sm" wrap="wrap">
                <Text fontSize="xl" fontWeight="semibold">
                  {current.name}
                </Text>
                <Text color="text-muted">${current.price} / month</Text>
              </Stack>
              <BodySmall margin="none" color="text-muted">
                Renews on <time dateTime="2026-12-04">December 4, 2026</time>
              </BodySmall>
            </Stack>
            <div>
              <Modal open={managing} onOpenChange={setManaging}>
                <Modal.Trigger render={<Button type="button" variant="primary" />}>Manage plan</Modal.Trigger>
                <Modal.Content>
                  <Modal.Header>Change plan</Modal.Header>
                  <Stack as="form" gap="md" onSubmit={changePlan}>
                    <Modal.Body>Changes are prorated to the day and appear on your next invoice.</Modal.Body>
                    <RadioGroup name="plan" defaultValue={plan} aria-label="Plan">
                      {Object.entries(plans).map(([value, item]) => (
                        // A plan with fewer seats than members can't be chosen.
                        <Radio key={value} value={value} disabled={item.limits.seats < seatsInUse}>
                          {item.name} · ${item.price}/month · {item.limits.seats} seats, {item.limits.calls.toLocaleString('en-US')} API calls
                        </Radio>
                      ))}
                    </RadioGroup>
                    <Modal.Footer>
                      <Modal.Close render={<Button type="button" variant="ghost" />}>Cancel</Modal.Close>
                      <Button type="submit" variant="primary">
                        Confirm plan
                      </Button>
                    </Modal.Footer>
                  </Stack>
                </Modal.Content>
              </Modal>
            </div>
          </Stack>
        </Card>

        <Card as="section" padding="lg" aria-labelledby={`${id}-payment`}>
          <Stack gap="md" height="full" justify="space-between">
            <Stack gap="xs">
              <Heading as="h2" id={`${id}-payment`} fontSize="sm" fontWeight="medium" color="text-muted">
                Payment method
              </Heading>
              <Stack direction="row" align="center" gap="md">
                <span className={styles.cardGlyph} aria-hidden>
                  <CreditCard size={18} />
                </span>
                <Stack gap="none">
                  <Text fontWeight="semibold">
                    Card ending <span className={styles.numeric}>7310</span>
                  </Text>
                  <BodySmall margin="none" color="text-muted">
                    Expires 08/2028
                  </BodySmall>
                  <BodySmall margin="none" color="text-muted">
                    billing@fernhill.example
                  </BodySmall>
                </Stack>
              </Stack>
            </Stack>
            <div>
              <Button as="a" href="#billing-payment-method">
                Update payment method
              </Button>
            </div>
          </Stack>
        </Card>
      </div>

      <Stack as="section" gap="md" aria-labelledby={`${id}-usage`}>
        <Stack direction="row" justify="space-between" align="baseline" wrap="wrap" gap="sm">
          <Heading as="h2" id={`${id}-usage`} fontSize="lg">
            Usage this period
          </Heading>
          <BodySmall color="text-muted" margin="none">
            Nov 4 – Dec 3, 2026
          </BodySmall>
        </Stack>
        <Stack as="ul" gap="lg" margin="none" padding="none" dangerouslySetClassName={styles.meters}>
          {usage.map((meter) => {
            const max = current.limits[meter.id]
            const share = meter.used / max
            const high = share >= warnAt
            const used = `${meter.used.toLocaleString('en-US')}${meter.unit}`
            const limit = `${max.toLocaleString('en-US')}${meter.unit}`
            return (
              <Stack as="li" key={meter.id} gap="sm">
                <Stack direction="row" justify="space-between" align="baseline" gap="sm" wrap="wrap">
                  <Text fontSize="sm" fontWeight="semibold" id={`${id}-${meter.id}`}>
                    {meter.label}
                  </Text>
                  <Text fontSize="sm" color="text-muted" dangerouslySetClassName={styles.numeric}>
                    {used} of {limit}
                  </Text>
                </Stack>
                {/* biome-ignore lint/a11y/useSemanticElements: a native <meter> can't be styled the same way in every engine */}
                <div
                  className={styles.meter}
                  role="meter"
                  aria-labelledby={`${id}-${meter.id}`}
                  aria-valuemin={0}
                  aria-valuemax={max}
                  aria-valuenow={Math.min(meter.used, max)}
                  aria-valuetext={`${used} of ${limit}, ${Math.round(share * 100)}%`}
                  data-high={high || undefined}
                >
                  <span className={styles.fill} style={{ width: `${Math.min(share, 1) * 100}%` }} />
                </div>
                {high && (
                  <Text fontSize="xs" color="warning-text">
                    <span className={styles.warning}>
                      <AlertTriangle aria-hidden size={12} />
                      {Math.round(share * 100)}% used. Upgrade before you reach the limit.
                    </span>
                  </Text>
                )}
              </Stack>
            )
          })}
        </Stack>
      </Stack>

      <Stack as="section" gap="md" aria-labelledby={`${id}-history`}>
        <Heading as="h2" id={`${id}-history`} fontSize="lg">
          Billing history
        </Heading>
        <TableContainer tabIndex={0} role="region" dangerouslySetClassName={styles.scroller} aria-labelledby={`${id}-history`}>
          <Table dangerouslySetClassName={styles.invoices}>
            <TableHeader>
              <TableRow>
                <TableHead scope="col">Date</TableHead>
                <TableHead scope="col">Invoice</TableHead>
                <TableHead scope="col">Status</TableHead>
                <TableHead scope="col" textAlign="right">
                  Amount
                </TableHead>
                <TableHead scope="col">
                  <span className="visually-hidden">Download</span>
                </TableHead>
              </TableRow>
            </TableHeader>
            <TableBody>
              {invoices.map((invoice) => (
                <TableRow key={invoice.id} dangerouslySetClassName={styles.invoice}>
                  <TableCell dangerouslySetClassName={`${styles.nowrap} ${styles.cellDate}`}>
                    <time dateTime={invoice.date}>{invoice.label}</time>
                  </TableCell>
                  <TableHead scope="row" dangerouslySetClassName={`${styles.numeric} ${styles.cellInvoice}`}>
                    {invoice.id}
                  </TableHead>
                  <TableCell dangerouslySetClassName={styles.cellStatus}>
                    <Badge tone="success">Paid</Badge>
                  </TableCell>
                  <TableCell textAlign="right" dangerouslySetClassName={`${styles.numeric} ${styles.cellAmount}`}>
                    {invoice.amount}
                  </TableCell>
                  <TableCell textAlign="right" dangerouslySetClassName={styles.cellDownload}>
                    <Link href={`#invoice-${invoice.id}`} dangerouslySetClassName={styles.download}>
                      <Download aria-hidden size={14} />
                      PDF<span className="visually-hidden">, invoice {invoice.id}</span>
                    </Link>
                  </TableCell>
                </TableRow>
              ))}
            </TableBody>
          </Table>
        </TableContainer>
      </Stack>
    </Stack>
  )
}
```

## How it works

- **Meters are meters.** Each bar is a `role="meter"` labelled by its heading, with `aria-valuenow`, `aria-valuemax` and an `aria-valuetext` that reads “84,210 of 100,000, 84%”. Past 80% the fill turns to the warning color and a line of text with an icon says so, so the warning never depends on color.
- **Limits follow the plan.** Changing plan in the dialog re-renders the meters against the new limits. Plans with fewer seats than the team uses are disabled in the `RadioGroup`.
- **The dialog is a form.** The `RadioGroup` submits through `FormData`, Cancel is `type="button"`, and a toast confirms the change.
- **Links say what they download.** Each “PDF” link carries the invoice number in visually hidden text, so a list of links still makes sense out of context. On a phone each invoice becomes two lines (date and amount, then number, status and download), and the plan and payment cards stack.

## App-owned CSS

The two-up summary, the card glyph, the meter bars and the narrow-screen invoice layout.

`examples/recipes/usage-billing.module.css`:

```css
.billing {
  container-type: inline-size;
}

/* Plan and payment side by side when there is room, stacked on a phone. */
.summary {
  display: grid;
  gap: var(--ds-space-lg);
}

@container (min-width: 34rem) {
  .summary {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
}

.cardGlyph {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: calc(var(--ds-space-xl) + var(--ds-space-md));
  height: var(--ds-space-xl);
  border: var(--ds-border-width-sm) solid var(--ds-color-border-subtle);
  border-radius: var(--ds-border-radius-sm);
  background-color: var(--ds-color-bg-subtle);
  color: var(--ds-color-text-muted);
}

.meters {
  list-style: none;
}

.meter {
  height: var(--ds-space-md);
  overflow: hidden;
  border-radius: var(--ds-border-radius-pill);
  background-color: var(--ds-color-surface);
}

.fill {
  display: block;
  height: 100%;
  border-radius: inherit;
  background-color: var(--ds-color-accent);

  .meter[data-high] & {
    background-color: var(--ds-color-warning);
  }
}

.warning {
  display: inline-flex;
  align-items: center;
  gap: var(--ds-space-sm);
}

.download {
  display: inline-flex;
  align-items: center;
  gap: var(--ds-space-sm);
  white-space: nowrap;
}

.numeric {
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

.nowrap {
  white-space: nowrap;
}

/* Positioned, so visually hidden text in the cells scrolls with the table instead of widening the page. */
.scroller {
  position: relative;
}

/*
 * On a narrow container each invoice becomes two lines: the date and amount,
 * then the invoice number, status and download link. The header row stays
 * in the table for screen readers but leaves the layout.
 */
@container (max-width: 30rem) {
  .invoices,
  .invoices > tbody {
    display: block;
  }

  .invoices > thead {
    position: absolute;
    width: 1px;
    height: 1px;
    overflow: hidden;
    clip-path: inset(50%);
  }

  .invoice {
    display: grid;
    grid-template-columns: auto minmax(0, 1fr) auto;
    align-items: center;
    gap: var(--ds-space-xs) var(--ds-space-md);
    padding: var(--ds-space-md) var(--ds-space-sm);

    & > td,
    & > th {
      padding: 0;
    }
  }

  .cellDate {
    grid-area: 1 / 1 / 2 / 3;
    font-weight: var(--ds-font-weight-semibold);
  }

  .cellAmount {
    grid-area: 1 / 3;
    font-weight: var(--ds-font-weight-semibold);
  }

  .cellInvoice {
    grid-area: 2 / 1;
    color: var(--ds-color-text-muted);
    font-weight: var(--ds-font-weight-body);
  }

  .cellStatus {
    grid-area: 2 / 2;
  }

  .cellDownload {
    grid-area: 2 / 3;
  }
}
```
