Stat
One number that matters, large, with its name above and how it changed below.
import { Stat, StatSkeleton } from '@sypnia/ui/react'
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" />;
} Units and trends
Up is not always good: the trend says which it is.
New customers12618 more than last week
Median response84ms9 ms slower
Failed requests0.2%0.3 points fewer
Tables5No change
Code
import { Icon, StatGroup, Stat } from '@sypnia/ui/react';
// Good and bad are about meaning, not direction: slower answers are bad news even though the number went up.
export default function Trends() {
return (
<StatGroup>
<Stat label="New customers" value="126" delta={<><Icon name="chevron-up" />18 more than last week</>} trend="good" />
<Stat label="Median response" value="84" unit="ms" delta={<><Icon name="chevron-up" />9 ms slower</>} trend="bad" />
<Stat label="Failed requests" value="0.2" unit="%" delta={<><Icon name="chevron-down" />0.3 points fewer</>} trend="good" />
<Stat label="Tables" value="5" delta="No change" trend="flat" />
</StatGroup>
);
} 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
| Stat | Type | Default |
|---|---|---|
labelrequired | ReactNode | |
delta | ReactNode | |
loadingThe 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 |
| StatSkeleton | Type | Default |
|---|---|---|
delta | boolean | true |