Join our Discord community
Special effects

Magnetic

Interactive magnetic attraction effects that follow cursor movement. Perfect for React applications requiring engaging hover animations with Next.js integration and TypeScript support.

Powered by

Loading component...

Installation

npx shadcn@latest add https://www.shadcn.io/registry/magnetic.json
npx shadcn@latest add https://www.shadcn.io/registry/magnetic.json
pnpm dlx shadcn@latest add https://www.shadcn.io/registry/magnetic.json
bunx shadcn@latest add https://www.shadcn.io/registry/magnetic.json

Features

  • Smooth magnetic attraction with configurable strength and range using Motion for fluid animations
  • Customizable spring physics with adjustable stiffness, damping, and mass parameters
  • Hover activation modes supporting always-on or hover-only magnetic behavior
  • Touch device detection with automatic disabling on touch interfaces for optimal UX
  • TypeScript support with complete interface definitions and prop validation
  • shadcn/ui compatibility using standard React patterns and flexible styling

Use Cases

This free open source React component works well for:

  • Interactive buttons - Call-to-action elements with magnetic hover effects built with Next.js
  • Portfolio showcases - Creative portfolio items using TypeScript and Tailwind CSS
  • Landing page elements - Hero section components that engage users on scroll
  • Gaming interfaces - Interactive UI elements for games and immersive experiences

API Reference

Magnetic

PropTypeDefaultDescription
childrenReact.ReactElementrequiredSingle child element to apply magnetic effect to
strengthnumber0.5Magnetic attraction strength factor (0-1)
rangenumber120Detection range in pixels from element center
springOptionsSpringOptions{ stiffness: 100, damping: 10, mass: 0.5 }Motion spring configuration object
onlyOnHoverbooleanfalseOnly activate magnetic effect when hovering element
disableOnTouchbooleantrueDisable effect on touch devices for performance

Spring Options

  • stiffness - Spring stiffness value (higher = snappier)
  • damping - Spring damping factor (higher = less oscillation)
  • mass - Element mass simulation (higher = slower response)

Implementation Notes

  • Wraps children in a motion.div with inline-block display
  • Uses global mousemove listener for cursor tracking
  • Automatically detects touch devices via media queries
  • Smooth spring animations with configurable physics
  • Compatible with shadcn/ui design system