Animated number
A number that counts to its value, for figures that change on screen.
Example
Total:
<p> Total: <focsd-animated-number id="count-demo" value="1280" format="decimal 0" duration="900"></focsd-animated-number> orders</p><focsd-button id="count-demo-button">Add 120 orders</focsd-button>
<script type="module">const number = document.getElementById('count-demo');
document.getElementById('count-demo-button').addEventListener('click', () => { number.value += 120;});</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-animated-number.js';</script>
<focsd-animated-number value="1280" format="decimal 0" duration="900"></focsd-animated-number>import { AnimatedNumber } from '@focsd-dev/react';
export function Example() { return <AnimatedNumber value="1280" format="decimal 0" duration="900"></AnimatedNumber>;}<script setup>import '@focsd-dev/components/focsd-animated-number.js';</script>
<template> <focsd-animated-number value="1280" format="decimal 0" duration="900"></focsd-animated-number></template>// Add CUSTOM_ELEMENTS_SCHEMA to the component's schemas, then:import '@focsd-dev/components/focsd-animated-number.js';
// template<focsd-animated-number value="1280" format="decimal 0" duration="900"></focsd-animated-number><script> import '@focsd-dev/components/focsd-animated-number.js';</script>
<focsd-animated-number value="1280" format="decimal 0" duration="900"></focsd-animated-number>API
Attributes
| Attribute | Property | Type | Default |
|---|---|---|---|
value | value | number | 0 |
format | format | Intl.NumberFormatOptions | undefined | — |
from | from | number | undefined | — |
duration | duration | number | undefined | — |
CSS parts
Style the inside of the component with ::part(), for example focsd-animated-number::part(number).
| Part | Description |
|---|---|
number | The counting text (hidden from assistive technology). |
Accessibility
- The counting digits are hidden from assistive technology. A visually hidden copy of the final value is what is read, once, so a screen reader never hears 1,281, 1,282, 1,283.
- Under
prefers-reduced-motion: reducethe text is the final value in the same frame: there is no counting. formattakes a short form (decimal 0,currency USD,percent 1,compact) orIntl.NumberFormatoptions (JSON as an attribute); an invalid format warns once and falls back to the plain number.fromsets where a count starts anddurationhow long it takes, in milliseconds.- The change is not announced as a live region. If people need to hear it, announce it from your own code.
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.