# Bounce

The Bounce component adds a playful, elastic animation to its children when they enter the viewport.

- **Category:** Animation
- **Docs:** https://uibeats.com/docs/animation/bounce
- **Registry:** https://uibeats.com/r/bounce.json
- **License:** MIT
- **npm dependencies:** `motion`

## Installation

```bash
npx shadcn@latest add https://uibeats.com/r/bounce.json
```

Or install the dependencies and copy the source below:

```bash
npm install motion
```

## When to use

Reach for Bounce when an element should feel physical on arrival: a price, a stat, a confirmation. The spring overshoot pulls the eye, so use it on one or two elements per screen. Across a whole list the repeated overshoot just reads as noise.

## Props

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `children` | `React.ReactNode` | `-` | The content to be animated (required) |
| `delay` | `number` | `0` | Delay before the animation starts (in seconds) |
| `duration` | `number` | `0.5` | Duration of the animation (in seconds) |
| `bounceHeight` | `number` | `20` | Height of the bounce effect (in pixels) |
| `className` | `string` | `''` | Additional CSS classes for styling |
| `once` | `boolean` | `true` | Whether to trigger the animation only once |

## Example usage

```tsx
import React from "react";
import Bounce from "@/components/demo/animation/bounce";

const BounceUsage = () => {
  return (
    <Bounce delay={1.2}>
      <div className="text-md mb-4 md:text-lg">
        This content will bounce when visible
      </div>
    </Bounce>
  );
};

export default BounceUsage;
```

## Source

Save as `components/ui/bounce.tsx`.

```tsx
"use client";
import React from "react";
import {
  motion,
  useInView,
  useReducedMotion,
  type Variants,
} from "motion/react";

interface BounceProps {
  children: React.ReactNode;
  delay?: number;
  duration?: number;
  bounceHeight?: number;
  className?: string;
  once?: boolean;
}

const Bounce: React.FC<BounceProps> = ({
  children,
  delay = 0,
  duration = 0.5,
  bounceHeight = 20,
  className = "",
  once = true,
}) => {
  const ref = React.useRef<HTMLDivElement>(null);
  const isInView = useInView(ref, { once, amount: 0.1 });
  const prefersReducedMotion = useReducedMotion();

  /*
   * Under reduced motion the element is settled from the first frame.
   * Dropping only the transition would not do: the `hidden` variant stays
   * applied until the element scrolls into view, so the content would sit
   * invisible and offset instead of simply not bouncing.
   */
  const variants: Variants = prefersReducedMotion
    ? { hidden: { opacity: 1, y: 0 }, visible: { opacity: 1, y: 0 } }
    : {
        hidden: { opacity: 0, y: bounceHeight },
        visible: {
          opacity: 1,
          y: 0,
          transition: {
            delay,
            duration,
            type: "spring",
            stiffness: 200,
            damping: 10,
          },
        },
      };

  return (
    <motion.div
      ref={ref}
      initial="hidden"
      animate={isInView ? "visible" : "hidden"}
      variants={variants}
      className={className}
    >
      {children}
    </motion.div>
  );
};

export default Bounce;
```

## Credits

Written by [Michael](https://github.com/lappemic).
