Join our Discord Community

React Magnetic Button

React magnetic button with animated particles that attract to center on hover. Features Framer Motion spring physics and customizable particle effects with TypeScript and shadcn/ui.

Building interactive particle animations?

Join our Discord community for help from other developers.


Ever watched iron filings dance around a magnet? That mesmerizing pull where scattered particles suddenly snap into formation? Most button hover effects are predictable—color shifts, tiny bounces, maybe a shadow. This React component brings real physics to your interface with floating particles that get magnetically attracted to the center on hover, creating that satisfying "snap" moment that makes users want to interact again.

Magnetic particle attraction on hover

Watch particles float randomly then get pulled toward the center with spring physics:

Loading component...

Built for React applications with TypeScript and Next.js. Uses Framer Motion's spring physics engine for natural particle movements that feel realistic, not mechanical. The magnetic effect triggers on both mouse hover and touch interactions, with customizable particle count and attraction behavior. Perfect integration with shadcn/ui design systems and dark mode support.

Installation

npx shadcn@latest add https://www.shadcn.io/registry/magnetic-button.json
npx shadcn@latest add https://www.shadcn.io/registry/magnetic-button.json
pnpm dlx shadcn@latest add https://www.shadcn.io/registry/magnetic-button.json
bunx shadcn@latest add https://www.shadcn.io/registry/magnetic-button.json

Why static hover effects feel lifeless

Developers add a color change or scale effect and call it interactive. But users have seen the same hover states thousands of times. They don't create delight or memorable moments anymore. The problem isn't the effect itself—it's the predictability.

Magnetic attraction is fundamentally different. It creates anticipation as particles float, then satisfaction when they snap into place. The spring physics make it feel like real matter responding to forces. Users instinctively want to trigger it again because it mimics natural phenomena they understand.

This component handles the complex physics calculations, particle management, and performance optimization automatically. Multiple particles animate simultaneously without janky performance. The attraction radius and particle count are fully customizable for different interaction intensities.

Features

  • Realistic spring physics using Framer Motion's animation engine with natural acceleration curves
  • Customizable particle system with configurable count and attraction radius parameters
  • Touch and mouse support with identical behavior across desktop and mobile devices
  • Performance optimized with GPU-accelerated transforms and efficient particle rendering
  • TypeScript interfaces with complete prop definitions and particle system types
  • Dark mode compatible using CSS custom properties and theme-aware violet color scheme
  • Accessible interactions maintaining proper button semantics and keyboard navigation
  • Spring customization with separate attraction and release physics for different feels

API Reference

MagneticButton

PropTypeDefaultDescription
particleCountnumber12Number of floating particles around the button
attractRadiusnumber50Radius in pixels for particle attraction effect
childrenReactNodeDefault contentCustom button content (overrides default text/icon)
classNamestring-Additional CSS classes for button styling

Particle Physics

The component uses two different spring configurations for realistic motion:

PhaseStiffnessDampingBehavior
Attraction5010Slower, dramatic pull toward center
Release10015Faster, bouncier return to original positions

Customization Examples

// More particles, tighter attraction
<MagneticButton particleCount={20} attractRadius={30} />

// Fewer particles, wider attraction field  
<MagneticButton particleCount={8} attractRadius={80} />

// Custom content
<MagneticButton>
  <span className="flex items-center gap-2">
    <Sparkles className="w-4 h-4" />
    Magic Button
  </span>
</MagneticButton>

Common gotchas

Performance on mobile: The particle animations are GPU-accelerated, but older devices might struggle with high particle counts. Consider reducing particleCount to 6-8 for broader compatibility.

Touch interaction timing: On touch devices, the magnetic effect triggers on touch start and releases on touch end. This feels natural but differs slightly from mouse hover behavior where particles can attract/release based on cursor proximity.

Particle positioning edge cases: Particles are positioned randomly within a 360px radius. Very small containers might clip particles during their initial scatter phase before attraction begins.

Spring physics interruption: Rapidly triggering attraction/release can interrupt spring animations mid-motion. The component handles this gracefully by recalculating from current positions, but very fast interactions might feel slightly jarky.

Z-index and stacking: The particles render behind the button text but above the button background. Complex parent stacking contexts might affect particle visibility during animation.

You might also like

Questions developers actually ask