Build Your Own Animated Component Library with React + Framer Motion




Why Build a Component Library?

  • Reuse animations without rewriting code
  • Keep your UI consistent across projects
  • Make it easy for teammates (or your future self)
  • Learn how to structure scalable React components



Step 1: Setup

Install React + Framer Motion:

npm install framer-motion
Enter fullscreen mode

Exit fullscreen mode

Create a folder structure like this:

animated-components/
│
├── src/
│   ├── components/
│   │   ├── AnimatedButton.jsx
│   │   ├── AnimatedCard.jsx
│   │   ├── AnimatedModal.jsx
│   │
│   └── index.js
│
├── example/   # Demo playground
│   ├── App.jsx
│   └── index.js
│
├── package.json
├── README.md
└── vite.config.js (or Next.js if you prefer)
Enter fullscreen mode

Exit fullscreen mode



Step 2: Create an Animated Button

// components/AnimatedButton.jsx
import { motion } from "framer-motion";

export default function AnimatedButton({ children, onClick }) {
  return (
    
      {children}
    
  );
}
Enter fullscreen mode

Exit fullscreen mode



Step 3: Create an Animated Card

// components/AnimatedCard.jsx
import { motion } from "framer-motion";

export default function AnimatedCard({ children }) {
  return (
    
      {children}
    
  );
}
Enter fullscreen mode

Exit fullscreen mode



Step 4: Create an Animated Modal

// components/AnimatedModal.jsx
import { motion, AnimatePresence } from "framer-motion";

export default function AnimatedModal({ isOpen, onClose, children }) {
  return (
    
      {isOpen && (
        
           e.stopPropagation()}
          >
            {children}
          
        
      )}
    
  );
}
Enter fullscreen mode

Exit fullscreen mode



Step 5: Export from index.js

// index.js
export { default as AnimatedButton } from "./components/AnimatedButton";
export { default as AnimatedCard } from "./components/AnimatedCard";
export { default as AnimatedModal } from "./components/AnimatedModal";
Enter fullscreen mode

Exit fullscreen mode

Now you have a mini animated component library ready to import anywhere:

import { AnimatedButton, AnimatedCard, AnimatedModal } from "./components";
Enter fullscreen mode

Exit fullscreen mode



Step 6: Demo App (example/)

This is where users can see your components in action.

// example/App.jsx
import { useState } from "react";
import { AnimatedButton, AnimatedCard, AnimatedModal } from "../src";

export default function App() {
  const [isOpen, setIsOpen] = useState(false);

  return (
    

Hello from Animated Card 🎉

setIsOpen(true)}> Open Modal setIsOpen(false)}>

I am an Animated Modal 🚀

); }
Enter fullscreen mode

Exit fullscreen mode



Step 7: Docs + GitHub

  • Create a README.md with installation + usage instructions.
  • Push to GitHub.
  • Add a demo site using Vite or Next.js + GitHub Pages / Vercel.



Step 8: Publish to npm

You can even publish this as an npm package so others can install it:

npm login
npm publish --access public
Enter fullscreen mode

Exit fullscreen mode

Then developers can do:

npm install animated-components-lib
Enter fullscreen mode

Exit fullscreen mode

And use it like:

import { AnimatedButton } from "animated-components-lib";
Enter fullscreen mode

Exit fullscreen mode



Source link

Leave a Reply

Your email address will not be published. Required fields are marked *