kipluo
Next.js6 min read2 steps

Magnetic Button in Next.js App Router

The biggest mistake in Next.js App Router is slapping 'use client' at the top of an entire landing page just to animate one hero CTA. Here is the clean server/client boundary pattern that keeps bundle size tiny and SSR hydration mismatch-free.

You needNext.js 14/15/16 App RouterReact Server Componentsnext/link
Loading demo
A thin client wrapper lets Server Components pass static links, icons, and copy directly as children.Component page

1. Isolate pointer physics inside a leaf 'use client' island

Put only the pointer listener and spring ref inside MagneticIsland.tsx. Because children are passed as a prop from your Server Component, any server-rendered markup, icons, or localized text inside the button stays server-rendered—and Next.js <Link> prefetching keeps working untouched.

components/magnetic-island.tsx
"use client";

import { useEffect, useRef, type ReactNode } from "react";

export function MagneticIsland({
  children,
  strength = 0.32,
  max = 18,
}: {
  children: ReactNode;
  strength?: number;
  max?: number;
}) {
  const fieldRef = useRef<HTMLSpanElement>(null);
  const innerRef = useRef<HTMLSpanElement>(null);

  useEffect(() => {
    const field = fieldRef.current;
    const inner = innerRef.current;
    if (!field || !inner) return;
    // Reading matchMedia inside useEffect avoids SSR hydration mismatches
    if (window.matchMedia("(prefers-reduced-motion: reduce)").matches) return;

    let x = 0, y = 0, vx = 0, vy = 0, tx = 0, ty = 0, raf = 0, last = 0;

    const tick = (now: number) => {
      const dt = Math.min(0.05, (now - last) / 1000);
      last = now;
      vx += (-260 * (x - tx) - 18 * vx) * dt;
      vy += (-260 * (y - ty) - 18 * vy) * dt;
      x += vx * dt;
      y += vy * dt;
      inner.style.transform = `translate3d(${x.toFixed(2)}px, ${y.toFixed(2)}px, 0)`;
      if (Math.abs(vx) > 0.05 || Math.abs(vy) > 0.05 || Math.abs(x - tx) > 0.05 || Math.abs(y - ty) > 0.05) {
        raf = requestAnimationFrame(tick);
      } else {
        inner.style.transform = `translate3d(${tx}px, ${ty}px, 0)`;
        raf = 0;
      }
    };

    const wake = () => {
      if (!raf) {
        last = performance.now();
        raf = requestAnimationFrame(tick);
      }
    };

    const onMove = (e: PointerEvent) => {
      if (e.pointerType === "touch") return;
      const r = field.getBoundingClientRect();
      tx = Math.max(-max, Math.min(max, (e.clientX - (r.left + r.width / 2)) * strength));
      ty = Math.max(-max, Math.min(max, (e.clientY - (r.top + r.height / 2)) * strength));
      wake();
    };
    const onLeave = () => { tx = 0; ty = 0; wake(); };

    field.addEventListener("pointermove", onMove);
    field.addEventListener("pointerleave", onLeave);
    return () => {
      cancelAnimationFrame(raf);
      field.removeEventListener("pointermove", onMove);
      field.removeEventListener("pointerleave", onLeave);
    };
  }, [strength, max]);

  return (
    <span ref={fieldRef} className="inline-flex p-2">
      <span ref={innerRef} className="inline-flex will-change-transform">
        {children}
      </span>
    </span>
  );
}

2. Compose inside a Server Component page with next/link

Notice that page.tsx stays a pure Server Component—zero client state, zero flash of unstyled layout, and full App Router route prefetching on hover.

app/page.tsx
import Link from "next/link";
import { MagneticIsland } from "@/components/magnetic-island";

export default async function HomePage() {
  return (
    <section className="py-20 text-center">
      <MagneticIsland strength={0.35}>
        <Link
          href="/pricing"
          className="inline-flex rounded-full bg-primary px-6 py-3 font-semibold text-primary-foreground"
        >
          See pricing
        </Link>
      </MagneticIsland>
    </section>
  );
}

Accessibility checklist

Tick each one off as you check your build. Your ticks stay in this browser.

0 of 2 checked

Build it yourself or install it

Build it yourself when

You only need one lightweight client island around a Next.js Link.

Install Kipluo when

Kipluo's MagneticSurface ships App Router ready with 'use client', CSS token theming, and zero hydration issues.

Keep building