An element pulled toward the pointer when it comes close.
Installation
Usage
import { Magnetic } from '@animate-ui/react/primitives/effects/magnetic';
export function CallToAction() {
return (
<Magnetic strength={0.4} range={160}>
<button>Come closer</button>
</Magnetic>
);
}Animation
- Within
rangepixels of its center, the element moves toward the pointer: the closer, the stronger the pull (strength). The distance is measured from where the element currently is, pull included. It springs back when the pointer leaves the range. - The pull follows the pointer on a light, bouncy spring (stiffness 100, damping 10, mass 0.5), so it's interruptible and never jumps. It moves with
x/y, a hardware-accelerated transform. - The pointer is only tracked while the element is on screen. With
onlyOnHover, only while it's hovered. - Off on touch screens (
disableOnTouch), where there is no pointer to follow. <AnimateUIProvider tempo>paces the default spring.- Under reduced motion, the element stays in place.
API Reference
Magnetic
| Prop | Type | Default |
|---|---|---|
asChild? | boolean | false |
strength? | number | 0.5 |
range? | number | 120 |
springOptions? | SpringOptions | { stiffness: 100, damping: 10, mass: 0.5 } |
onlyOnHover? | boolean | false |
disableOnTouch? | boolean | true |
...props? | HTMLMotionProps<'div'> | - |