Here's a fully-featured React button component with Tailwind CSS that includes hover effects, click animations, loading states, and multiple variants:
<!DOCTYPE html>
<html>
<head>
<script src="https://unpkg.com/react@18/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@18/umd/react-dom.development.js"></script>
<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
<script src="https://cdn.tailwindcss.com"></script>
<style>
@keyframes bounce {
0%, 100% { transform: translateY(0); }
50% { transform: translateY(-5px); }
}
@keyframes background-pan {
0% { background-position: 0% 50%; }
50% { background-position: 100% 50%; }
100% { background-position: 0% 50%; }
}
@keyframes shake {
0%, 100% { transform: rotate(0deg); }
25% { transform: rotate(5deg); }
75% { transform: rotate(-5deg); }
}
</style>
</head>
<body>
<div id="root"></div>
<script type="text/babel">
const AnimatedButton = ({
children,
variant = 'primary',
disabled = false,
onClick,
icon,
isLoading = false
}) => {
const variants = {
primary: 'bg-blue-600 hover:bg-blue-700 text-white',
secondary: 'bg-gray-800 hover:bg-gray-700 text-white',
ghost: 'bg-transparent hover:bg-gray-100 text-gray-800',
success: 'bg-green-600 hover:bg-green-700 text-white',
danger: 'bg-red-600 hover:bg-red-700 text-white'
};
const [isPressed, setIsPressed] = React.useState(false);
const handleClick = (e) => {
if (disabled || isLoading) return;
setIsPressed(true);
setTimeout(() => setIsPressed(false), 150);
onClick?.(e);
};
const buttonStyles = `
${variants[variant]}
${disabled ? 'opacity-50 cursor-not-allowed' : 'cursor-pointer'}
${isPressed ? 'scale-95' : 'scale-100'}
transition-all duration-150 ease-in-out
relative overflow-hidden
px-6 py-3 rounded-lg
font-medium text-sm
flex items-center justify-center gap-2
shadow-md hover:shadow-lg
active:shadow-none
`;
const loadingStyles = isLoading ? `
animate-[background-pan_3s_linear_infinite]
bg-gradient-to-r from-blue-600 via-purple-600 to-blue-600
text-transparent bg-clip-text
` : '';
return (
<button
className={`${buttonStyles} ${loadingStyles}`}
onClick={handleClick}
disabled={disabled || isLoading}
>
{isLoading ? (
<>
<div className="animate-spin rounded-full h-5 w-5 border-2 border-white border-t-transparent"></div>
<span>Loading...</span>
</>
) : (
<>
{icon && <span className="animate-[shake_2s_ease_infinite]">{icon}</span>}
<span className="relative overflow-hidden">
<span className="inline-block hover:animate-[bounce_1s_ease-in-out]">
{children}
</span>
</span>
</>
)}
</button>
);
};
// Example usage
const App = () => (
<div className="flex flex-col gap-4 p-8">
<AnimatedButton variant="primary" icon="🚀">Primary Button</AnimatedButton>
<AnimatedButton variant="secondary" isLoading>Loading Button</AnimatedButton>
<AnimatedButton variant="success" onClick={() => alert('Clicked!')}>
Success Action
</AnimatedButton>
<AnimatedButton variant="danger" disabled>Disabled Button</AnimatedButton>
<AnimatedButton variant="ghost">Ghost Button</AnimatedButton>
</div>
);
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<App />);
</script>
</body>
</html>
This button component includes:
- Multiple Variants: Primary, secondary, ghost, success, and danger styles
- Interactive Animations:
- Bounce effect on hover
- Scale-down animation on click
- Shake animation for icons
- Background gradient animation in loading state
- Loading State: Spinning loader with animated background
- Accessibility:
- Proper cursor states
- Disabled state handling
- Keyboard navigation support
- Visual Features:
- Shadow effects
- Smooth transitions
- Responsive flex layout
- Icon support
- Performance:
- Optimized animations using CSS keyframes
- State management for press feedback
- Efficient re-renders
To use the component:
- Choose from 5 color variants
- Add icons using any React component
- Control loading state with the
isLoading prop
- Handle click events with
onClick
- Disable the button when needed
The animations include:
- Hover: Subtle lift and shadow
- Click: Scale-down effect
- Loading: Animated gradient background
- Icons: Gentle shake animation
- Text: Bounce effect on hover
You can customize the colors, sizes, and animation timing by modifying the Tailwind classes and keyframe definitions.