Skip to main content

Magnetic

The Magnetic component wraps any element and applies a smooth magnetic effect that makes the content subtly follow the cursor when hovering. Powered by GSAP’s elastic easing, it creates a playful, physics-based interaction.

Installation

This component requires GSAP as a peer dependency.

Import

Usage

Props

React.ReactNode
required
The element to apply the magnetic effect to. Must be a single child element.

Animation Details

GSAP QuickTo Configuration

The component uses GSAP’s quickTo method for high-performance animations:

Elastic Easing

  • Ease: elastic.out(1, 0.3) creates a bouncy, spring-like motion
  • Duration: 1 second for smooth transitions
  • Amplitude: 1 (controls oscillation strength)
  • Period: 0.3 (controls oscillation frequency)

Position Calculation

The magnetic offset is calculated from the element’s center:
The element automatically returns to its original position with elastic easing when the cursor leaves.

Examples

Call-to-Action Button

Floating Action Buttons

Card Hover Effect

Logo with Magnetic Effect

The wrapper div uses display: inline-block to maintain the natural size of the child element.

Best Practices

Performance

  • GSAP’s quickTo provides optimized performance for real-time animations
  • Event listeners are properly cleaned up on unmount
  • Use sparingly on the same page (3-6 magnetic elements maximum)

Visual Design

  • Works best with buttons, cards, and interactive elements
  • Combine with subtle shadows for depth perception
  • Ensure sufficient spacing between magnetic elements
  • Use with elements that have clear hover states

Accessibility

Magnetic effects can be disorienting for some users. Consider:
  • Adding a user preference toggle for reduced motion
  • Keeping the magnetic pull subtle (current implementation is well-balanced)
  • Ensuring touch device fallbacks work properly

Use Cases

  • Hero section CTAs
  • Navigation buttons
  • Social media links
  • Product cards
  • Interactive logos
  • Feature highlights
  • Portfolio items
  • Download buttons