# Scale In

The ScaleIn component creates a smooth scale-in animation for its children when they enter the viewport.

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

## Installation

```bash
npx shadcn@latest add https://uibeats.com/r/scale-in.json
```

Or install the dependencies and copy the source below:

```bash
npm install motion
```

## When to use

Use it when an element should feel like it is arriving out of the page instead of sliding onto it: modals, cards, images. Keep scaleFrom between 0.9 and 0.95. Anything lower reads as a zoom, not an entrance.

## Props

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `children` | `React.ReactNode` | `-` | The content to be scaled in (required) |
| `duration` | `number` | `0.5` | Duration of the animation (in seconds) |
| `delay` | `number` | `0` | Delay before the animation starts (in seconds) |
| `scaleFrom` | `number` | `0.8` | Initial scale value (between 0 and 1) |
| `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 ScaleIn from "@/components/demo/animation/scale-in";

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

export default ScaleInUsage;
```

## Source

Save as `components/ui/scale-in.tsx`.

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

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

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

  // Settled at full size from the first frame, in view or not.
  const variants: Variants = prefersReducedMotion
    ? { hidden: { scale: 1, opacity: 1 }, visible: { scale: 1, opacity: 1 } }
    : {
        hidden: { scale: scaleFrom, opacity: 0 },
        visible: {
          scale: 1,
          opacity: 1,
          transition: {
            duration: duration,
            delay: delay,
            ease: [0.215, 0.61, 0.355, 1],
          },
        },
      };

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

export default ScaleIn;
```

## Credits

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