# Count-up number

> A number that counts up from zero to your figure as soon as it scrolls into view.

- Category: [Text & scroll](https://library.aimran.es/components/text-scroll)
- Built with: React + Motion
- Live editor: https://library.aimran.es/blocks/in-view-counter
- Library: AIMRAN Library (https://library.aimran.es), free, no sign-up, by Aimran Oukham

## Where to use it

Numbers that count up get read, while static ones get skimmed. Use it for the proof section of a landing page: “2,400 happy travelers”, projects delivered by a studio, meals served by a charity or downloads of an app. This animated number counter in React starts when the block becomes visible, not on page load.

Only use figures you can back up, since an animated number draws extra attention to it. Keep the duration around a second; longer and people scroll past before it ends. It counts again every time it comes back into view.

## Code

### React (Motion)

```tsx
import { useEffect, useRef, useState } from "react";
import { useInView } from "framer-motion";

interface InViewCounterProps {
  end?: number;
  /** Milisegundos que tarda en llegar a la cifra final. */
  duration?: number;
  label?: string;
}

export default function InViewCounter({
  end = 2400,
  duration = 1200,
  label = "happy travellers",
}: InViewCounterProps) {
  const ref = useRef<HTMLDivElement>(null);
  // useInView mira el viewport y respeta el recorte de los contenedores con scroll: funciona en ambos casos.
  const seen = useInView(ref, { once: false });
  const [n, setN] = useState(0);

  useEffect(() => {
    if (!seen) {
      setN(0);
      return;
    }
    const start = performance.now();
    let raf = 0;
    const run = (t: number) => {
      const p = Math.min(1, (t - start) / duration);
      setN(Math.round(end * p));
      if (p < 1) raf = requestAnimationFrame(run);
    };
    raf = requestAnimationFrame(run);
    return () => cancelAnimationFrame(raf);
  }, [seen, duration, end]);

  return (
    <div ref={ref} className="text-center text-[#1d1d1f] dark:text-[#f5f5f7]">
      <strong className="text-5xl font-semibold tabular-nums">{n.toLocaleString("es-ES")}</strong>
      <p className="text-sm text-[#86868b]">{label}</p>
    </div>
  );
}
```

## Prompt for AI assistants

Paste this into ChatGPT, Claude, Cursor, v0 or Lovable to adapt the block to your stack (Tailwind, Vue, Svelte…):

````text
I want to add the “Count-up number” block (Text & scroll) from AIMRAN Library to my project.

## What it is
A number that counts up from zero to your figure as soon as it scrolls into view.

## Chosen settings
- Final number: 2400 (from 100 to 10000)
- Duration: 1200ms (from 400 to 2500ms)
- Label: "happy travellers"

## What I need
1. Add it to my project with the same look and animation as the reference code: same sizes, colours, radii, shadows, timings and easing.
2. It is built with React + TypeScript, with framer-motion for the animation and lucide-react for the icons. If my project uses something else (Vue, Svelte, Astro, plain HTML…), translate it without losing any behaviour. If I use Tailwind, turn the styles into classes; if not, keep the CSS in its own file.
3. Expose the settings above as props (or CSS variables) with those values as defaults.
4. Make it work well on phones: nothing that depends only on hover, touch targets of at least 44 px and no overflow on 320 px screens.
5. Make it look right in light and dark mode.
6. Keep it accessible: semantic HTML, roles and aria-* where needed, visible focus and keyboard support.
7. Respect prefers-reduced-motion: with reduced motion, no big animations but the same end result.

## Reference code
```tsx
import { useEffect, useRef, useState } from "react";
import { useInView } from "framer-motion";

interface InViewCounterProps {
  end?: number;
  /** Milisegundos que tarda en llegar a la cifra final. */
  duration?: number;
  label?: string;
}

export default function InViewCounter({
  end = 2400,
  duration = 1200,
  label = "happy travellers",
}: InViewCounterProps) {
  const ref = useRef<HTMLDivElement>(null);
  // useInView mira el viewport y respeta el recorte de los contenedores con scroll: funciona en ambos casos.
  const seen = useInView(ref, { once: false });
  const [n, setN] = useState(0);

  useEffect(() => {
    if (!seen) {
      setN(0);
      return;
    }
    const start = performance.now();
    let raf = 0;
    const run = (t: number) => {
      const p = Math.min(1, (t - start) / duration);
      setN(Math.round(end * p));
      if (p < 1) raf = requestAnimationFrame(run);
    };
    raf = requestAnimationFrame(run);
    return () => cancelAnimationFrame(raf);
  }, [seen, duration, end]);

  return (
    <div ref={ref} className="text-center text-[#1d1d1f] dark:text-[#f5f5f7]">
      <strong className="text-5xl font-semibold tabular-nums">{n.toLocaleString("es-ES")}</strong>
      <p className="text-sm text-[#86868b]">{label}</p>
    </div>
  );
}
```

Give me the complete final code, ready to paste, and tell me in one line where to put it in my project.
````
