Sure! Here's a simple and reusable React Button component with Framer Motion animations . This button includes hover and tap (click) animations such as scaling slightly when hovered and shrinking a bit when clicked.
✅ Requirements:
Make sure you have framer-motion installed in your React project:
bash Copy code
npm install framer-motion
💻 React Animated Button Component (with Framer Motion)
jsx Copy code
// MotionButton.js
import React from 'react';
import { motion } from 'framer-motion';
import PropTypes from 'prop-types';
const MotionButton = ({ children, onClick, className, style, disabled }) => {
return (
<motion.button
whileHover={{ scale: 1.05 }}
whileTap={{ scale: 0.95 }}
transition={{ type: 'spring', stiffness: 300 }}
onClick={onClick}
className={className}
style={{
padding: '12px 24px',
fontSize: '16px',
borderRadius: '8px',
border: 'none',
backgroundColor: '#007bff',
color: 'white',
cursor: disabled ? 'not-allowed' : 'pointer',
opacity: disabled ? 0.6 : 1,
...style
}}
disabled={disabled}
>
{children}
</motion.button>
);
};
MotionButton.propTypes = {
children: PropTypes.node.isRequired,
onClick: PropTypes.func,
className: PropTypes.string,
style: PropTypes.object,
disabled: PropTypes.bool
};
MotionButton.defaultProps = {
onClick: () => {},
className: '',
style: {},
disabled: false
};
export default MotionButton;
✅ Example Usage
jsx Copy code
// App.js
import React from 'react';
import MotionButton from './MotionButton';
function App() {
return (
<div style={{ padding: '2rem' }}>
<MotionButton onClick={() => alert('Button clicked!')}>
Click Me
</MotionButton>
</div>
);
}
export default App;
🎨 Features You Can Customize
whileHover: Change colors, shadows, or scale on hover.
whileTap: Simulate click feedback using scale or rotation.
Responsive styles with props or utility classes (e.g., Tailwind or your custom CSS).
Let me know if you'd like a variant with icon support or loading animation built-in!