# Integrate `AnimatedNumber` from Motion Garden

You are helping me add the **AnimatedNumber** component to an existing project.
Everything you need is below — full source, dependencies, props and the
behavioural contract. Read it all before writing anything.

- **Component:** AnimatedNumber
- **Category:** Text Animations
- **Summary:** Animated numeric transitions with flip, slide, fade, and bounce styles.
- **Reference:** https://motion-garden.netlify.app/components/animated-number
- **Full catalog (for you, the agent):** https://motion-garden.netlify.app/llms.txt

## Stack this source assumes

- React 19 with the Next.js App Router (never the `pages/` directory)
- TypeScript, strict
- Tailwind CSS v4, CSS-first config (utilities are inline; no `tailwind.config` needed)

If my project differs, adapt the integration — but do not rewrite the animation math to suit a different stack. Ask me first.

## Dependencies

```bash
bun add motion
```

## Files to create

One file. Paths are as shipped; the component may live anywhere in my project, but keep every peer file at the exact path shown so the import aliases resolve.

### 1. `components/registry/animated-number/animated-number.tsx`

```tsx
"use client";

import { motion, AnimatePresence } from "motion/react";
import { useMemo } from "react";

export type AnimationType = "flip" | "slide" | "fade" | "scale-bounce";

interface AnimatedNumberDisplayProps {
	value: string | number;
	animationType?: AnimationType;
	className?: string;
	duration?: number;
}

export function AnimatedNumber({
	value,
	animationType = "flip",
	className = "",
	duration = 0.4,
}: AnimatedNumberDisplayProps) {
	const animationVariants = useMemo(() => {
		const baseConfig = {
			transition: {
				duration,
				ease: "easeOut",
			},
		};

		const variants = {
			flip: {
				initial: { rotateX: 90, opacity: 0 },
				animate: { rotateX: 0, opacity: 1 },
				exit: { rotateX: -90, opacity: 0 },
				transition: { ...baseConfig.transition, ease: "easeInOut" },
			},
			slide: {
				initial: { y: 20, opacity: 0 },
				animate: { y: 0, opacity: 1 },
				exit: { y: -20, opacity: 0 },
				transition: baseConfig.transition,
			},
			fade: {
				initial: { opacity: 0 },
				animate: { opacity: 1 },
				exit: { opacity: 0 },
				transition: { ...baseConfig.transition, duration: duration * 0.6 },
			},
			"scale-bounce": {
				initial: { scale: 0.5, opacity: 0 },
				animate: { scale: 1, opacity: 1 },
				exit: { scale: 0.8, opacity: 0 },
				transition: {
					...baseConfig.transition,
					type: "spring",
					stiffness: 200,
					damping: 15,
				},
			},
		};

		return variants[animationType];
	}, [animationType, duration]);

	const key = `number-${value}-${animationType}`;

	return (
		<div className={`relative overflow-hidden ${className}`}>
			<AnimatePresence mode="wait">
				<motion.div
					key={key}
					initial="initial"
					animate="animate"
					exit="exit"
					variants={animationVariants}
					// @ts-expect-error variants carry a `transition` key that isn't part of the Variants type
					transition={animationVariants.transition}
					style={{
						perspective: 1000,
						transformStyle: "preserve-3d",
					}}
				>
					{value}
				</motion.div>
			</AnimatePresence>
		</div>
	);
}

export function AnimatedNumberAdvanced({
	value: initialValue,
	animationType = "slide",
	className = "",
	duration = 0.4,
	staggerDelay = 0.05,
}: AnimatedNumberDisplayProps & { staggerDelay?: number }) {
	const digits = String(initialValue).split("");

	const animationVariants = useMemo(() => {
		const baseConfig = {
			transition: {
				duration,
				ease: "easeOut",
			},
		};

		const variants = {
			flip: {
				initial: { rotateX: 90, opacity: 0 },
				animate: { rotateX: 0, opacity: 1 },
				exit: { rotateX: -90, opacity: 0 },
				transition: { ...baseConfig.transition, ease: "easeInOut" },
			},
			slide: {
				initial: { y: 20, opacity: 0 },
				animate: { y: 0, opacity: 1 },
				exit: { y: -20, opacity: 0 },
				transition: baseConfig.transition,
			},
			fade: {
				initial: { opacity: 0 },
				animate: { opacity: 1 },
				exit: { opacity: 0 },
				transition: { ...baseConfig.transition, duration: duration * 0.6 },
			},
			"scale-bounce": {
				initial: { scale: 0.5, opacity: 0 },
				animate: { scale: 1, opacity: 1 },
				exit: { scale: 0.8, opacity: 0 },
				transition: {
					...baseConfig.transition,
					type: "spring",
					stiffness: 200,
					damping: 15,
				},
			},
		};

		return variants[animationType];
	}, [animationType, duration]);

	return (
		<div className={`flex ${className}`}>
			<AnimatePresence mode="popLayout">
				{digits.map((digit, index) => (
					<motion.div
						key={`${initialValue}-${animationType}-${index}`}
						initial="initial"
						animate="animate"
						exit="exit"
						variants={animationVariants}
						// @ts-expect-error variants carry a `transition` key that isn't part of the Variants type
						transition={{
							...animationVariants.transition,
							delay: index * staggerDelay,
						}}
						style={{
							perspective: 1000,
							transformStyle: "preserve-3d",
						}}
					>
						{digit}
					</motion.div>
				))}
			</AnimatePresence>
		</div>
	);
}

export function SmoothCounter({
	value,
	animationType = "slide",
	className = "",
	showDelta = false,
	duration,
}: AnimatedNumberDisplayProps & { showDelta?: boolean }) {
	return (
		<motion.div
			className={className}
			layout
			transition={{ type: "spring", stiffness: 200, damping: 20 }}
		>
			<AnimatedNumber
				value={value}
				animationType={animationType}
				className="font-medium"
				duration={duration}
			/>
		</motion.div>
	);
}
```


## Props

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `value` | `number` | `999` | The value to display and animate to. Range 0–9999. |
| `animationType` | `"flip" | "slide" | "fade" | "scale-bounce"` | `"flip"` | Transition style between values. |
| `duration` | `number` | `0.4` | Transition duration. Range 0.1–2 s. |
| `staggerDelay` | `number` | `0.05` | Delay between each digit's animation. Range 0–0.3 s. |

## Usage

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

<AnimatedNumber
  value={999}
  animationType="flip"
  duration={0.4}
  staggerDelay={0.05}
/>
```

The import path matches the file path above; change it if you move the component. Every value shown is the documented default — pass only what you actually want to change. The snippet lists the tunable props only: if the component's props type in the source requires others (content, handlers, data), pass those too.

## Integration rules

- Keep the `"use client"` directive. This is a client component and will not render as a Server Component.
- It fills its parent. Give the wrapper an explicit height (or a definite grid area) — against a height-auto parent it collapses to nothing.

## Component-specific notes

The file exports three components, and the props table above describes only one of them:

- `AnimatedNumberAdvanced` animates digit by digit. It is what the live preview on the reference page shows, and it is the only one that takes `staggerDelay`. Its `animationType` defaults to `"slide"`.
- `AnimatedNumber` swaps the whole value at once. It takes `value`, `animationType`, `duration` and `className`, and has no `staggerDelay` — passing it is a type error. Its `animationType` defaults to `"flip"`.
- `SmoothCounter` wraps `AnimatedNumber` in a layout spring, for a number whose width changes.

Ask me which one I want if my request does not make it clear. All three accept a pre-formatted string as `value` (for example `"$300,000"`), so currency and grouping survive.

## Your task

1. Tell me where in my project this should live and why.
2. Create the files above verbatim.
3. Wire the component into the place I point you at, with sensible props for that context.
4. Flag anything in my setup that conflicts with the stack assumptions above.

Do not restyle the component or alter the animation math unless I ask.
