← Components

Utilities

Magnetic

Wrap any control to make it lean toward the pointer. Within a radius it moves up to a few px toward you, its content a little further; leaving springs it home.

Examples and variants

Button and FAB

Move the pointer near them.

Example
function MagneticButtons() {
  return (
    <div className="flex items-center gap-10">
      <Magnetic>
        <Button size="lg">
          Get started
          <ArrowRightIcon data-icon="inline-end" />
        </Button>
      </Magnetic>
      <Magnetic strength={8}>
        <Fab icon={<PlusIcon />} aria-label="New" />
      </Magnetic>
    </div>
  )
}

Spec drawing

Measured from the live component at default density. Each code maps to the token that sets it; change the token and the drawing follows.

Install

With the CLI

The shadcn CLI only delivers files: it copies our code from the Isotopi registry into your project and installs the packages it uses. Add the foundation once, then the component.

Terminal
npx shadcn@latest add https://isotopi.com/r/minimal.json
npx shadcn@latest add https://isotopi.com/r/magnetic.json

By hand

No CLI needed. Add the foundation once (see Getting started), then follow these steps. They include every component this one builds on.

  1. Install the packages it uses.

    Terminal
    npm install cn
  2. Copy this file to the same paths in your project. Imports use the @/ alias for your project root.

    • components/ui/magnetic.tsxmagnetic
      components/ui/magnetic.tsx
      "use client"
      
      import * as React from "react"
      import { cn } from "cn"
      import { motionLevel } from "@/lib/overlay"
      
      /*
        Magnetic: makes any control lean toward the pointer. Within `radius` px of
        its edge, the control moves up to `strength` px toward the pointer and its
        inner content a little further (parallax); leaving springs it home.
        Off on touch, in still motion and under reduced motion; 2px in calm.
      */
      function Magnetic({
        children,
        radius = 60,
        strength = 6,
        className,
      }: {
        children: React.ReactNode
        /** Reach beyond the control's edge, in px. */
        radius?: number
        /** Maximum lean in px. */
        strength?: number
        className?: string
      }) {
        const ref = React.useRef<HTMLSpanElement>(null)
      
        React.useEffect(() => {
          const node = ref.current
          if (!node) return
          const onMove = (event: PointerEvent) => {
            if (event.pointerType !== "mouse") return
            const level = motionLevel(node)
            if (level === "still") return
            const box = node.getBoundingClientRect()
            const cx = box.left + box.width / 2
            const cy = box.top + box.height / 2
            const dx = event.clientX - cx
            const dy = event.clientY - cy
            const reachX = box.width / 2 + radius
            const reachY = box.height / 2 + radius
            const inside = Math.abs(dx) < reachX && Math.abs(dy) < reachY
            const max = level === "calm" ? Math.min(2, strength) : strength
            const x = inside ? (dx / reachX) * max : 0
            const y = inside ? (dy / reachY) * max : 0
            node.style.setProperty("--magnet-x", `${x}px`)
            node.style.setProperty("--magnet-y", `${y}px`)
            if (inside) node.dataset.pulled = ""
            else delete node.dataset.pulled
          }
          window.addEventListener("pointermove", onMove, { passive: true })
          return () => window.removeEventListener("pointermove", onMove)
        }, [radius, strength])
      
        return (
          <span ref={ref} data-slot="magnetic" className={cn("magnetic inline-flex", className)}>
            {children}
          </span>
        )
      }
      
      export { Magnetic }
      

Imports

What the examples on this page import:

Imports
import { ArrowRightIcon } from "lucide-react"
import { Avatar, AvatarFallback } from "@/components/ui/avatar"
import { Badge } from "@/components/ui/badge"
import { Button } from "@/components/ui/button"
import { Card, CardDescription, CardHeader, CardTitle } from "@/components/ui/card"
import { Fab } from "@/components/ui/fab"
import { ImageTrail } from "@/components/ui/image-trail"
import { Magnetic } from "@/components/ui/magnetic"
import { Popover, PopoverContent, PopoverTrigger } from "@/components/ui/popover"
import { Heading } from "@/components/ui/typography"
import { PlusIcon } from "lucide-react"

Anatomy

  1. 1Wrapper · Moves by --magnet-x / --magnet-y; children move half as much again.

Props

PropTypeDefaultDescription
radiusnumber60Reach beyond the control's edge, in px.
strengthnumber6Maximum lean, in px (2 in calm).

Tokens

Every visual spec is a component token in styles/tokens/components.css, pointing at a semantic token. Change a token to restyle the component; themes change the semantic layer.

This utility has no component tokens; it uses semantic tokens directly.

TokenPoints toLightDark

Motion

TriggerWhat happensTokens
Pointer near / awayLeans toward the pointer; springs home.--spring-snappy

Calm keeps the motion without overshoot. Still (and reduced motion) keeps a designed frame with no movement.

Accessibility

  • Mouse only; nothing moves on touch, in still motion or under reduced motion.

Usage

Do

  • Use on one hero action.

Don’t

  • Don't put it on every button; it gets tiring.

Code

components/ui/magnetic.tsx
"use client"

import * as React from "react"
import { cn } from "cn"
import { motionLevel } from "@/lib/overlay"

/*
  Magnetic: makes any control lean toward the pointer. Within `radius` px of
  its edge, the control moves up to `strength` px toward the pointer and its
  inner content a little further (parallax); leaving springs it home.
  Off on touch, in still motion and under reduced motion; 2px in calm.
*/
function Magnetic({
  children,
  radius = 60,
  strength = 6,
  className,
}: {
  children: React.ReactNode
  /** Reach beyond the control's edge, in px. */
  radius?: number
  /** Maximum lean in px. */
  strength?: number
  className?: string
}) {
  const ref = React.useRef<HTMLSpanElement>(null)

  React.useEffect(() => {
    const node = ref.current
    if (!node) return
    const onMove = (event: PointerEvent) => {
      if (event.pointerType !== "mouse") return
      const level = motionLevel(node)
      if (level === "still") return
      const box = node.getBoundingClientRect()
      const cx = box.left + box.width / 2
      const cy = box.top + box.height / 2
      const dx = event.clientX - cx
      const dy = event.clientY - cy
      const reachX = box.width / 2 + radius
      const reachY = box.height / 2 + radius
      const inside = Math.abs(dx) < reachX && Math.abs(dy) < reachY
      const max = level === "calm" ? Math.min(2, strength) : strength
      const x = inside ? (dx / reachX) * max : 0
      const y = inside ? (dy / reachY) * max : 0
      node.style.setProperty("--magnet-x", `${x}px`)
      node.style.setProperty("--magnet-y", `${y}px`)
      if (inside) node.dataset.pulled = ""
      else delete node.dataset.pulled
    }
    window.addEventListener("pointermove", onMove, { passive: true })
    return () => window.removeEventListener("pointermove", onMove)
  }, [radius, strength])

  return (
    <span ref={ref} data-slot="magnetic" className={cn("magnetic inline-flex", className)}>
      {children}
    </span>
  )
}

export { Magnetic }