Stat
A key figure with a label and its change, such as revenue up 12% from last month.
Example
<div class="stack"> <focsd-stat label="Revenue" value="48200" format="currency USD 0" period="last month" delta="0.124"></focsd-stat> <focsd-stat label="Churn" value="0.032" format="percent 1" period="last month" delta="-0.008" good="down"></focsd-stat> <focsd-stat label="Open tickets" value="213" delta="0"></focsd-stat></div>Counting up
With animate the figure counts to each new value. Reduced motion shows the final value at once.
<div class="stack"> <focsd-stat id="stat-live" label="Active users" value="1200" animate period="yesterday" delta="0.05"></focsd-stat> <focsd-button id="stat-live-button">Add 340 users</focsd-button></div>
<script type="module">const tile = document.getElementById('stat-live');
document.getElementById('stat-live-button').addEventListener('click', () => { tile.value = Number(tile.value) + 340;});</script>Usage
Import the module once, then use the tag like any HTML element. Every framework below renders the same element; see Frameworks for setup.
<script type="module"> import '@focsd-dev/components/focsd-stat.js';</script>
<focsd-stat label="Revenue" value="48200" format="currency USD 0" period="last month" delta="0.124"></focsd-stat>import { Stat } from '@focsd-dev/react';
export function Example() { return <Stat label="Revenue" value="48200" format="currency USD 0" period="last month" delta="0.124"></Stat>;}<script setup>import '@focsd-dev/components/focsd-stat.js';</script>
<template> <focsd-stat label="Revenue" value="48200" format="currency USD 0" period="last month" delta="0.124"></focsd-stat></template>// Add CUSTOM_ELEMENTS_SCHEMA to the component's schemas, then:import '@focsd-dev/components/focsd-stat.js';
// template<focsd-stat label="Revenue" value="48200" format="currency USD 0" period="last month" delta="0.124"></focsd-stat><script> import '@focsd-dev/components/focsd-stat.js';</script>
<focsd-stat label="Revenue" value="48200" format="currency USD 0" period="last month" delta="0.124"></focsd-stat>API
Attributes
| Attribute | Property | Type | Default |
|---|---|---|---|
label | label | string | — |
value | value | number | string | — |
format | format | Intl.NumberFormatOptions | undefined | — |
delta | delta | number | undefined | — |
delta-format | deltaFormat | Intl.NumberFormatOptions | undefined | — |
trend | trend | StatTrend | '' | — |
good | good | 'up' | 'down' | up |
period | period | string | — |
href | href | string | — |
animate | animated | boolean | false |
Slots
| Slot | Description |
|---|---|
chart | A small chart under the value, such as a focsd-sparkline. |
footer | A note at the bottom of the tile, such as when the figure was updated. |
CSS parts
Style the inside of the component with ::part(), for example focsd-stat::part(base).
| Part | Description |
|---|---|
base | The tile: a group named by the label. |
label | The label (a link with `href`). |
value | The key figure. |
delta | The change in words, with its arrow. |
delta-icon | The arrow (hidden from assistive technology). |
chart | The wrapper around the `chart` slot. |
footer | The wrapper around the `footer` slot. |
Accessibility
- A group named by its
label. The value, then the change, are read as words: "Up 12.4% from last month, good". An arrow is only decoration. - The change is never carried by color alone: the direction is in the words, and the good or bad judgement is visible text, not only a green or red tint.
deltais a fraction under the defaultdelta-format(0.12 is 12%); the sign sets the direction unlesstrendis set.good="down"marks a fall as good news, for costs or churn.valuecan be a number or any text. Format numbers withformat: a short form (currency USD,currency EUR 0,percent 1,compact,unit kilometer,decimal 2, where a trailing number is the most decimals), JSON for any otherIntl.NumberFormatoptions, or a property.- With
href, the label is a link.
Counting up
- The counting text is hidden from screen readers; a visually hidden copy holds the final value, so nothing is read out while the digits change.
- Under
prefers-reduced-motion: reducethe value changes at once, with no counting. - The property is
animated(the attribute isanimate), because every element already has ananimate()method.
Ask the docs
Hi, what are you building?
Ask about any component, theming, forms or frameworks. I answer with the right component, a snippet and a link to its docs.