Magnetic Button Component

The MagneticButton component is an interactive UI element that creates a magnetic effect, pulling the cursor towards the button when it is within a certain range. This component is highly customizable and provides a unique user experience.

Features

  • Magnetic Effect: Attracts the cursor when it is within a defined range.
  • Customizable Intensity: Adjust the magnetic pull strength.
  • Flexible Interaction Area: Choose between the component itself or its parent.
  • Consistent wrapper API: Use className for styling (customClassName remains supported).
  • Smooth Animations: Uses spring-based animations for smooth transitions.
  • Lightweight: Pointer tracking is scoped to the active interaction and cleaned up automatically.
  • Reduced-motion safe: Returns to its resting position when motion is reduced.

Playground

Tune the pull strength and range, then move your cursor near the button.

Controls
Controls

Installation

Usage

Props

PropertyTypeDefaultDescription
childrenReactNoderequiredThe content to be rendered inside the magnetic button.
magneticIntensitynumber0.6The intensity of the magnetic pull effect.
magneticRangenumber100The range within which the magnetic effect is active.
interactionArea"self" | "parent""self"Defines the area of interaction for the magnetic effect.
springConfigSpringOptionsConfiguration for the spring animation.
customClassNamestringAdditional class names for custom styling.
classNamestringAdditional classes for the wrapper. Prefer this for new usage.