# Animated Number

Animated numeric text powered by Number Flow, with the complete Zenos Text styling API.

Animated Number combines [Number Flow](https://number-flow.barvian.me/) with [Text](/components/text/). Numeric changes roll between values while Zenos supplies typography and semantic colors.

## Install

```bash
pnpm dlx @zentara-co/zenos-cli add animated-number
npx @zentara-co/zenos-cli add animated-number
yarn dlx @zentara-co/zenos-cli add animated-number
bunx @zentara-co/zenos-cli add animated-number
```

The CLI installs `@number-flow/react` and copies `animated-number.tsx` into your chosen directory. Import it from your application-owned path:

```tsx
import { AnimatedNumber } from "./components/animated-number";
```

## Changing values

```tsx
import { useState } from "react";
import { Button, Stack } from "@zentara-co/zenos-ui";
import { AnimatedNumber } from "./components/animated-number";

function AnimatedNumberExample() {
  const [value, setValue] = useState(1260);
  return (
    <Stack gap="4" align="center">
      <AnimatedNumber
        as="output"
        aria-label="Sample count"
        type="heading"
        size="h2"
        value={value}
        locales="en-US"
      />
      <Stack direction="row" gap="2">
        <Button variant="primary" onClick={() => setValue(value - 125)}>
          Subtract 125
        </Button>
        <Button variant="primary" onClick={() => setValue(value + 125)}>
          Add 125
        </Button>
      </Stack>
    </Stack>
  );
}
```

`value` belongs to the application. Updating it starts Number Flow's transition. The first render displays the initial value without an entrance animation.

## Formatting and motion

```tsx
import { useState } from "react";
import { Button, Stack } from "@zentara-co/zenos-ui";
import { AnimatedNumber } from "./components/animated-number";

function AnimatedNumberFormattingExample() {
  const [value, setValue] = useState(36.5);
  return (
    <Stack gap="4" align="center">
      <AnimatedNumber
        as="output"
        aria-label="Sample amount"
        value={value}
        locales="en-US"
        format={{ style: "currency", currency: "USD", minimumFractionDigits: 2 }}
        suffix="/mo"
        type="emphasis"
        size="l"
        color="brand"
        numberFlowProps={{
          transformTiming: { duration: 500, easing: "ease-out" },
          opacityTiming: { duration: 200 },
        }}
      />
      <Button variant="primary" onClick={() => setValue(value === 36.5 ? 1250.75 : 36.5)}>
        Change sample amount
      </Button>
    </Stack>
  );
}
```

Set `locales` explicitly when server and client formatting must agree. `format` accepts Number Flow's supported `Intl.NumberFormat` options. Pass advanced settings, digit rules, plugins, engine styles, events, CSP `nonce`, or the engine ref through `numberFlowProps`. Direct formatting props take precedence over the same properties inside `numberFlowProps`.

## Text customization

All [Text props](/components/text/#styling-and-semantics) are available: `as`, `type`, `size`, `weight`, `color`, `align`, `italic`, `truncate`, `maxLines`, `className`, and `style`, along with native attributes, event handlers, and a root `ref`. Heading typography chooses the same semantic heading defaults as Text. Use `as="span"` for inline text or `as="output"` for a calculated value.

```tsx
<AnimatedNumber
  as="span"
  value={42}
  size="l"
  weight="semibold"
  color="success"
  align="end"
  animated={false}
/>
```

## API

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| value | NumberFlowProps['value'] | — | Required value to display and animate. |
| format / locales | NumberFlowProps['format'] / Intl.LocalesArgument | — | Number format and locale selection. |
| prefix / suffix | string | — | Optional content before or after the formatted number. |
| animated | boolean | true | Disables engine transitions when false. |
| numberFlowProps | Number Flow props excluding value and children | — | Advanced engine options, className, style, ref, and animation callbacks. |
| Text props / ref | TextProps<C> | — | Typography, polymorphic root semantics, native props, and root ref. |

## Accessibility and limits

Number Flow handles its animated digits and respects reduced-motion preferences by default. Keep that preference enabled. Add an explicit `aria-label` when an abbreviated number needs a fuller name. Use `aria-live` only when changes need announcements, and avoid making rapidly changing counters live regions.

This recipe inherits Number Flow's limitations: scientific and engineering notation, non-Latin digits, and RTL number formatting are not supported. Text alignment and surrounding RTL layout remain configurable. See the [upstream limitations](https://number-flow.barvian.me/#limitations) before choosing a locale.

Source: https://zenos-ui.pages.dev/recipes/animated-number/