Skip to content

Stat

One number that matters, large, with its name above and how it changed below.

import { Stat, StatSkeleton } from '@sypnia/ui/react'

On this page
  1. One number
  2. Units and trends
  3. Loading, in the language of the page
  4. Skeleton
  5. When to use it
  6. For everyone
  7. Props

One number

Name, value and how it changed.

Requests today48,21012% more than yesterday
Code
import { Stat } from '@sypnia/ui/react';

export default function Basic() {
  return <Stat label="Requests today" value="48,210" delta="12% more than yesterday" trend="good" />;
}

Loading, in the language of the page

Press «Load again». Change the language at the top to see the numbers rewritten.

Rows5,552
Storage3.1 GB
Sales this month$18,420.00
Code
import { useState } from 'react';
import { Button, Skeleton, Stat, StatGroup, useSypnia } from '@sypnia/ui/react';

// Press «Load again». Numbers are written by `format`, in the language of the page.
export default function Loading() {
  const { format } = useSypnia();
  const [loading, setLoading] = useState(false);
  const reload = () => { setLoading(true); setTimeout(() => setLoading(false), 2000); };
  const value = (v: string) => (loading ? <Skeleton width="5ch" height="1em" /> : v);
  return (
    <div className="sy-stack">
      <div aria-busy={loading} aria-live="polite">
        <StatGroup>
          <Stat label="Rows" value={value(format.number(5552))} />
          <Stat label="Storage" value={value(format.bytes(3.1 * 1024 ** 3))} />
          <Stat label="Sales this month" value={value(format.currency(18420, 'USD'))} />
        </StatGroup>
      </div>
      <div><Button size="sm" disabled={loading} onClick={reload}>Load again</Button></div>
    </div>
  );
}

Skeleton

The real piece and its skeleton: the same box, so nothing moves when the data arrives.

Requests today1,204+12% from yesterday
Requests today
Code
import type { CSSProperties } from 'react';
import { Stat, StatSkeleton } from '@sypnia/ui/react';

// With «loading» the name stays; pass «delta» too when the real one has a second line, so its line is kept. With
// StatSkeleton nothing is known yet. The skeleton has the same box as the real one, so nothing moves when the data arrives.
export default function Skeleton() {
  return (
    <div className="sy-grid" style={{ '--min': '16rem' } as CSSProperties}>
      <Stat label="Requests today" value="1,204" delta="+12% from yesterday" trend="good" />
      <Stat label="Requests today" delta loading />
      <StatSkeleton />
    </div>
  );
}

When to use it

Use it when

  • A figure people look for first: requests today, sales, storage used.
  • Say how it moved, in words, and mark whether that is good or bad news with trend.
  • Several together: wrap them in a StatGroup.

Do not use it when

  • The number needs a chart to make sense: use a chart with a title.
  • It is one fact among many about a thing: KeyValue.
  • It is a count beside a tab or a button: Count.

For everyone

  • The name comes before the number, so it is read in that order.
  • Good and bad are in the words of the change («9 ms slower»), not only in green and red.
  • Numbers use figures of equal width, so they do not jitter when they update.
  • While it loads, a skeleton takes the place of the number inside a region marked busy.

Props

StatTypeDefault
labelrequired ReactNode
delta ReactNode
loading

The name stays; the number is a skeleton until it comes, as long as `value` when one is given (the last known, or a typical one), and so is how it moved when the Stat has one: pass `delta` while loading to keep its line (a typical one, or `true` for a bar).

boolean
trend "flat" | "good" | "bad"
unit ReactNode
value ReactNode
StatSkeletonTypeDefault
delta boolean true
  1. One number
  2. Units and trends
  3. Loading, in the language of the page
  4. Skeleton
  5. When to use it
  6. For everyone
  7. Props

    Search pages, sections and terms