Skip to content

Magnetic

A magnetic wrapper component that attracts its children towards the mouse cursor. Inspired by Motion Primitives’ Magnetic. Supports self, parent, and global action areas.

Terminal window
npx shadcn@latest add https://joaocarmassi.com/components/magnetic.json
import { Magnetic } from '@/components/ui/magnetic';

<Magnetic>
<Button>Hover me</Button>
</Magnetic>

<Magnetic intensity={0.8} range={200}>
<Button variant='outline'>Stronger pull</Button>
</Magnetic>

Control which element triggers the magnetic effect.

{
/* Default: self */
}
<Magnetic actionArea='self'>
<Button>Self</Button>
</Magnetic>;
{
/* Parent triggers the effect */
}
<Magnetic actionArea='parent'>
<Button>Parent</Button>
</Magnetic>;
{
/* Global mouse tracking */
}
<Magnetic actionArea='global' range={300}>
<Button>Global</Button>
</Magnetic>;

Prop Type Default Description
children React.ReactNode Content to apply the effect to.
className string Additional CSS classes.
intensity number 0.6 Strength of the magnetic pull.
range number 100 Effective range in pixels.
actionArea 'self' | 'parent' | 'global' 'self' Trigger area.
springOptions SpringOptions { stiffness: 26.7, damping: 4.1, mass: 0.2 } Spring config.