Skip to content
Focsd homeFocsd homeFocsd
Forms

Stat

A key figure with a label and its change, such as revenue up 12% from last month.

  • Tag: focsd-stat
  • Pattern: Group named by its label; the change in words
  • Category: Display

Example

HTML
<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.

Add 340 users
HTML
<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>

API

Attributes

AttributePropertyTypeDefault
labellabelstring—
valuevaluenumber | string—
formatformatIntl.NumberFormatOptions | undefined—
deltadeltanumber | undefined—
delta-formatdeltaFormatIntl.NumberFormatOptions | undefined—
trendtrendStatTrend | ''—
goodgood'up' | 'down'up
periodperiodstring—
hrefhrefstring—
animateanimatedbooleanfalse

Slots

SlotDescription
chartA small chart under the value, such as a focsd-sparkline.
footerA 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).

PartDescription
baseThe tile: a group named by the label.
labelThe label (a link with `href`).
valueThe key figure.
deltaThe change in words, with its arrow.
delta-iconThe arrow (hidden from assistive technology).
chartThe wrapper around the `chart` slot.
footerThe 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.
  • delta is a fraction under the default delta-format (0.12 is 12%); the sign sets the direction unless trend is set. good="down" marks a fall as good news, for costs or churn.
  • value can be a number or any text. Format numbers with format: 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 other Intl.NumberFormat options, 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: reduce the value changes at once, with no counting.
  • The property is animated (the attribute is animate), because every element already has an animate() method.