← Components

Utilities

Magnetic text

A display heading whose letters drift toward the pointer, each on its own spring, so a ripple runs through the word.

Examples and variants

Headline

Move the pointer over the word.

Example
function MagneticHeadline() {
  return (
    <MagneticText as="h3" className="text-[length:var(--type-display-size)] leading-(--type-display-leading) font-semibold tracking-(--type-display-tracking)">
      Liquid
    </MagneticText>
  )
}

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-text.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-text.tsxmagnetic-text
      components/ui/magnetic-text.tsx
      "use client"
      
      import * as React from "react"
      import { cn } from "cn"
      import { motionLevel } from "@/lib/overlay"
      
      /*
        Magnetic text: a display heading whose letters drift toward the pointer.
        Letters within `radius` px move up to `strength` px; each letter springs
        back on its own, so a ripple runs through the word. Display headings only.
      */
      function MagneticText({
        children,
        as: Tag = "h2",
        radius = 80,
        strength = 4,
        className,
      }: {
        children: string
        as?: "h1" | "h2" | "h3" | "p"
        radius?: number
        strength?: number
        className?: string
      }) {
        const ref = React.useRef<HTMLElement>(null)
      
        function onMove(event: React.PointerEvent) {
          const root = ref.current
          if (!root || event.pointerType !== "mouse") return
          const level = motionLevel(root)
          if (level === "still") return
          const max = level === "calm" ? Math.min(1, strength) : strength
          root.querySelectorAll<HTMLElement>("[data-letter]").forEach((letter) => {
            const box = letter.getBoundingClientRect()
            const dx = event.clientX - (box.left + box.width / 2)
            const dy = event.clientY - (box.top + box.height / 2)
            const dist = Math.hypot(dx, dy)
            const pull = dist < radius ? (1 - dist / radius) * max : 0
            letter.style.setProperty("--lx", `${dist ? (dx / dist) * pull : 0}px`)
            letter.style.setProperty("--ly", `${dist ? (dy / dist) * pull : 0}px`)
          })
        }
      
        function onLeave() {
          ref.current?.querySelectorAll<HTMLElement>("[data-letter]").forEach((letter) => {
            letter.style.setProperty("--lx", "0px")
            letter.style.setProperty("--ly", "0px")
          })
        }
      
        return (
          <Tag
            ref={ref as React.Ref<HTMLHeadingElement>}
            data-slot="magnetic-text"
            aria-label={children}
            onPointerMove={onMove}
            onPointerLeave={onLeave}
            className={cn("magnetic-text", className)}
          >
            {children.split("").map((char, i) => (
              <span key={i} aria-hidden data-letter="" className="magnetic-letter inline-block whitespace-pre">
                {char}
              </span>
            ))}
          </Tag>
        )
      }
      
      export { MagneticText }
      

Imports

What the examples on this page import:

Imports
import { Chip, ChipRemove } from "@/components/ui/chip"
import { MagneticText } from "@/components/ui/magnetic-text"
import { PasswordInput } from "@/components/ui/password-input"
import { Progress } from "@/components/ui/progress"
import { tokenMs } from "@/lib/motion/token-ms"
import { motionLevel } from "@/lib/overlay"

Anatomy

  1. 1Letters · One span per character, moved by --lx / --ly.

Props

PropTypeDefaultDescription
childrenstring—The text.
as"h1" | "h2" | "h3" | "p""h2"Element.
radius, strengthnumber80, 4Reach and maximum drift in px.

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 nearLetters drift toward the pointer and spring back one by one.--spring-bouncy

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

Accessibility

  • The heading keeps its text as aria-label; letters are hidden from screen readers.
  • Mouse only; still and reduced motion keep letters in place.

Usage

Do

  • Use on one display heading.

Don’t

  • Don't use on body text or long headings.

Code

components/ui/magnetic-text.tsx
"use client"

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

/*
  Magnetic text: a display heading whose letters drift toward the pointer.
  Letters within `radius` px move up to `strength` px; each letter springs
  back on its own, so a ripple runs through the word. Display headings only.
*/
function MagneticText({
  children,
  as: Tag = "h2",
  radius = 80,
  strength = 4,
  className,
}: {
  children: string
  as?: "h1" | "h2" | "h3" | "p"
  radius?: number
  strength?: number
  className?: string
}) {
  const ref = React.useRef<HTMLElement>(null)

  function onMove(event: React.PointerEvent) {
    const root = ref.current
    if (!root || event.pointerType !== "mouse") return
    const level = motionLevel(root)
    if (level === "still") return
    const max = level === "calm" ? Math.min(1, strength) : strength
    root.querySelectorAll<HTMLElement>("[data-letter]").forEach((letter) => {
      const box = letter.getBoundingClientRect()
      const dx = event.clientX - (box.left + box.width / 2)
      const dy = event.clientY - (box.top + box.height / 2)
      const dist = Math.hypot(dx, dy)
      const pull = dist < radius ? (1 - dist / radius) * max : 0
      letter.style.setProperty("--lx", `${dist ? (dx / dist) * pull : 0}px`)
      letter.style.setProperty("--ly", `${dist ? (dy / dist) * pull : 0}px`)
    })
  }

  function onLeave() {
    ref.current?.querySelectorAll<HTMLElement>("[data-letter]").forEach((letter) => {
      letter.style.setProperty("--lx", "0px")
      letter.style.setProperty("--ly", "0px")
    })
  }

  return (
    <Tag
      ref={ref as React.Ref<HTMLHeadingElement>}
      data-slot="magnetic-text"
      aria-label={children}
      onPointerMove={onMove}
      onPointerLeave={onLeave}
      className={cn("magnetic-text", className)}
    >
      {children.split("").map((char, i) => (
        <span key={i} aria-hidden data-letter="" className="magnetic-letter inline-block whitespace-pre">
          {char}
        </span>
      ))}
    </Tag>
  )
}

export { MagneticText }