Here’s a concise, reusable “AnimatedButton” component that uses Framer Motion for entrance, hover, and tap (press) animations.
jsx
// AnimatedButton.jsx
import React from 'react';
import { motion } from 'framer-motion';
import PropTypes from 'prop-types';
// --------------------------------------------------
// Animation variants
// --------------------------------------------------
const variants = {
initial: { opacity: 0, y: 12 },
animate: { opacity: 1, y: 0, transition: { duration: 0.35, ease: 'easeOut' } },
hover: { scale: 1.06, boxShadow: '0px 8px 20px rgba(0,0,0,0.15)' },
tap: { scale: 0.97 }
};
const AnimatedButton = ({
children,
onClick,
disabled = false,
className = '',
style = {},
...rest // capture any extra props (e.g. type="submit")
}) => (
<motion.button
className={`animated-btn ${className}`}
style={{
padding: '0.6rem 1.4rem',
borderRadius: 8,
border: 'none',
cursor: disabled ? 'not-allowed' : 'pointer',
background: '#5c6ac4',
color: '#fff',
fontSize: 16,
fontWeight: 600,
outline: 'none',
...style
}}
disabled={disabled}
onClick={onClick}
variants={variants}
initial="initial"
animate="animate"
whileHover={!disabled ? 'hover' : ''}
whileTap={!disabled ? 'tap' : ''}
{...rest}
>
{children}
</motion.button>
);
AnimatedButton.propTypes = {
children: PropTypes.node.isRequired,
onClick: PropTypes.func,
disabled: PropTypes.bool,
className: PropTypes.string,
style: PropTypes.object
};
export default AnimatedButton;
Usage Example
jsx
import React from 'react';
import AnimatedButton from './AnimatedButton';
export default function Demo() {
return (
<div style={{ textAlign: 'center', marginTop: 80 }}>
<AnimatedButton onClick={() => alert('Clicked!')}>
🚀 Launch
</AnimatedButton>
</div>
);
}
Why this works:
- Variants – Reusable animation states (
initial, animate, hover, tap) keep the component declarative.
- Accessibility –
disabled prop disables both pointer events and animations.
- Composable – Pass any native
<button> props (type, form, etc.) via {...rest}.
Need custom motion?
Just override props:
jsx
<AnimatedButton
whileHover={{ scale: 1.15, rotate: 1 }}
transition={{ type: 'spring', stiffness: 200 }}
>
Custom Hover
</AnimatedButton>
Enjoy smooth, springy React buttons with almost no extra code! 🎉