Modus UI

Magnet

Web (React)

A micro-interaction component that pulls an element towards the mouse cursor.

A simple but highly effective physics micro-interaction. It wraps any element and pulls it towards the mouse cursor as it approaches, creating a magnetic gravity effect.

Installation

CLI

npx @modus-ui/cli add magnet

Manual

  1. Copy the code from the component tab.
  2. Create a file at components/ui/magnet.tsx.
  3. Paste the code.

Usage

tsx
import { Magnet } from "@/components/ui/magnet";

export default function Example() {
  return (
    <Magnet padding={150} magnetStrength={3}>
      <button>Magnetic Button</button>
    </Magnet>
  );
}

Props

PropTypeDefaultDescription
paddingnumber100Distance in pixels around the element within which the pull starts.
disabledbooleanfalseTurns the magnetic effect off.
magnetStrengthnumber2Divisor for the pull; higher values move the element less.
activeTransitionstring"transform 0.3s ease-out"CSS transition used while the cursor is in range.
inactiveTransitionstring"transform 0.5s ease-in-out"CSS transition used when the element springs back.
wrapperClassNamestring""Classes for the outer wrapper.
innerClassNamestring""Classes for the moving inner element.

Comments

  • No comments yet.