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
classNamefor styling (customClassNameremains 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.
Installation
Usage
Props
| Property | Type | Default | Description |
|---|---|---|---|
children | ReactNode | required | The content to be rendered inside the magnetic button. |
magneticIntensity | number | 0.6 | The intensity of the magnetic pull effect. |
magneticRange | number | 100 | The range within which the magnetic effect is active. |
interactionArea | "self" | "parent" | "self" | Defines the area of interaction for the magnetic effect. |
springConfig | SpringOptions | — | Configuration for the spring animation. |
customClassName | string | — | Additional class names for custom styling. |
className | string | — | Additional classes for the wrapper. Prefer this for new usage. |