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.
Installation
Section titled “Installation”npx shadcn@latest add https://joaocarmassi.com/components/magnetic.jsonpnpm dlx shadcn@latest add https://joaocarmassi.com/components/magnetic.jsonnpx shadcn@latest add https://joaocarmassi.com/components/magnetic.jsonbunx --bun shadcn@latest add https://joaocarmassi.com/components/magnetic.jsonImport
Section titled “Import”import { Magnetic } from '@/components/ui/magnetic';Preview
Section titled “Preview”<Magnetic> <Button>Hover me</Button></Magnetic>Custom Intensity & Range
Section titled “Custom Intensity & Range”<Magnetic intensity={0.8} range={200}> <Button variant='outline'>Stronger pull</Button></Magnetic>Action Areas
Section titled “Action Areas”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. |