# Rotate In

The RotateIn component creates a smooth rotation animation for its children when they enter the viewport.

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

## Installation

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

Or install the dependencies and copy the source below:

```bash
npm install motion
```

## When to use

Best on small, self-contained elements: icons, badges, logos, avatars. Rotating a block of text makes it briefly unreadable, so keep this for things that read as objects instead of as content.

## Props

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `children` | `React.ReactNode` | `-` | The content to be rotated in (required) |
| `duration` | `number` | `0.5` | Duration of the animation (in seconds) |
| `delay` | `number` | `0` | Delay before the animation starts (in seconds) |
| `rotateFrom` | `number` | `90` | Initial rotation angle (in degrees) |
| `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 RotateIn from "@/components/demo/animation/rotate-in";

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

export default RotateInUsage;
```

## Source

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

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

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

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

  // A quarter turn is the most vestibular-provoking entrance in the set, so
  // this one matters most: settled and unrotated from the first frame.
  const variants: Variants = prefersReducedMotion
    ? { hidden: { rotate: 0, opacity: 1 }, visible: { rotate: 0, opacity: 1 } }
    : {
        hidden: { rotate: rotateFrom, opacity: 0 },
        visible: {
          rotate: 0,
          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 RotateIn;
```

## Credits

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