A reusable React button component with a smooth magnetic hover effect, built using React, Tailwind CSS, and clsx.
-
🧲 Magnetic hover interaction
-
🎨 Fully customizable with
className -
🔗 Supports:
- Button
- Internal routing (
react-router-dom) - External links
-
⚡ Lightweight and reusable
-
🎯 Smart default styles (auto background & text)
Make sure you have these installed:
npm install clsx react-router-domimport MagneticButton from "./MagneticButton";<MagneticButton text="Click Me" /><MagneticButton>
Click Me 🚀
</MagneticButton><MagneticButton className="bg-amber-300 text-xl hover:scale-105">
Custom Button
</MagneticButton><MagneticButton to="/about" as="link">
Go to About
</MagneticButton><MagneticButton href="https://google.com" as="a">
Open Google
</MagneticButton><MagneticButton onClick={() => alert("Clicked!")}>
Click Action
</MagneticButton>| Prop | Type | Description |
|---|---|---|
children |
node | Custom content inside button |
text |
string | Alternative to children |
className |
string | Custom Tailwind classes |
to |
string | Route path (for React Router) |
href |
string | External link |
onClick |
function | Click handler |
as |
string | "button" (default), "link", "a" |
-
inline-flexlayout -
rounded-full -
shadow-xs→hover:shadow-sm -
transition-transform -
Default:
bg-white(if no bg provided)text-black(if no text color provided)
Wrap your app with:
import { BrowserRouter } from "react-router-dom";
<BrowserRouter>
<App />
</BrowserRouter>shadow-xs is not default in Tailwind.
Add this to your tailwind.config.js:
theme: {
extend: {
boxShadow: {
xs: "0 1px 2px rgba(0,0,0,0.05)",
},
},
},You can override defaults:
<MagneticButton className="bg-red-500 text-white">
Override Style
</MagneticButton>- Tracks mouse position inside the button
- Applies
transform: translate()for magnetic effect - Resets on mouse leave
- Uses
useRef+useEffect
/components
└── MagneticButton.jsx
- Variants (
primary,outline,ghost) - Sizes (
sm,md,lg) - Disable magnetic effect on mobile
- Glow / gradient effects
Made with ❤️ using React + Tailwind
Free to use and modify.