Stat
Un número que importa, grande, con su nombre arriba y cómo cambió abajo.
import { Stat, StatSkeleton } from '@sypnia/ui/react'
Un número
Nombre, valor y cómo cambió.
Requests today48,21012% more than yesterday
Código
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" />;
} Unidades y tendencias
Subir no siempre es bueno: la tendencia dice cuál es.
New customers12618 more than last week
Median response84ms9 ms slower
Failed requests0.2%0.3 points fewer
Tables5No change
Código
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>
);
} Cargando, en el idioma de la página
Presiona «Load again». Cambia el idioma arriba para ver los números reescritos.
Rows5552
Storage3,1 GB
Sales this month18.420,00 US$
Código
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
La pieza real y su skeleton: la misma caja, así nada se mueve cuando llegan los datos.
Requests today1,204+12% from yesterday
Requests today
Código
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>
);
} Cuándo usarlo
Úsalo cuando
- Una cifra que la gente busca primero: solicitudes de hoy, ventas, almacenamiento usado.
- Di cómo se movió, con palabras, y marca si es buena o mala noticia con
trend. - Varios juntos: envuélvelos en un StatGroup.
No lo uses cuando
- El número necesita un gráfico para tener sentido: usa un gráfico con título.
- Es un dato entre muchos de una cosa: KeyValue.
- Es una cuenta junto a una pestaña o un botón: Count.
Para todos
- El nombre va antes del número, así se lee en ese orden.
- Lo bueno y lo malo están en las palabras del cambio («9 ms más lento»), no solo en verde y rojo.
- Los números usan cifras del mismo ancho, así no tiemblan al actualizarse.
- Mientras carga, un skeleton toma el lugar del número dentro de una región marcada como ocupada.
Props
| Stat | Tipo | Por defecto |
|---|---|---|
labelobligatorio | 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 | Tipo | Por defecto |
|---|---|---|
delta | boolean | true |