HextaUI

Number flow: Interruptions and events

A new value mid-spin continues from where each digit is, so rapid updates never jump. onAnimationsStart and onAnimationsFinish fire once per batch of digits.

1,200Idle
"use client"

import * as React from "react"

import { Button } from "@/components/ui/button"
import { NumberFlow } from "@/components/ui/number-flow"

export function NumberFlowEvents() {
  const [value, setValue] = React.useState(1200)
  const [status, setStatus] = React.useState("Idle")
  const timers = React.useRef<number[]>([])

  React.useEffect(() => {
    const pending = timers.current
    return () => pending.forEach((id) => window.clearTimeout(id))
  }, [])

  function burst() {
    timers.current.forEach((id) => window.clearTimeout(id))
    timers.current = Array.from({ length: 10 }, (_, index) =>
      window.setTimeout(
        () => setValue((current) => current + Math.ceil(Math.random() * 40)),
        index * 90
      )
    )
  }

  return (
    <div className="flex flex-col items-center gap-4">
      <NumberFlow
        value={value}
        onAnimationsStart={() => setStatus("Animating")}
        onAnimationsFinish={() => setStatus("Settled")}
        className="text-5xl font-semibold"
      />
      <span className="text-sm text-muted-foreground">{status}</span>
      <Button variant="outline" size="sm" onClick={burst}>
        Burst ×10
      </Button>
    </div>
  )
}

This adds the example as components/examples/number-flow/events.tsx, together with the Number flow component and the HextaUI theme tokens it needs.

pnpm dlx shadcn@latest add https://hextaui.com/r/number-flow-events.json

To add only the component, follow the Number flow installation.

Props in this example

The Number flow props this example sets, from the API reference.

PropTypeDefault
valueRequired.
number–
onAnimationsStartCalled when a batch of digits starts moving.
() => void–
onAnimationsFinishCalled when the latest batch settles. Skipped if a newer value interrupts it.
() => void–

Every prop is in the Number flow API reference.

See them all, with keyboard support and accessibility notes, on the Number flow page.