kipluo
shadcn/ui6 min read2 steps

Magnetic Button with shadcn/ui

When working in a shadcn/ui codebase, you want magnetic behavior to inherit your exact Button variants (default, outline, ghost), respect disabled states, and keep asChild composition intact.

You needshadcn/ui Buttonclass-variance-authority (cva)Tailwind CSS v4
Loading demo
Magnetic physics wrap shadcn/ui Button variants cleanly without breaking asChild, disabled, or theme tokens.Component page

1. Wrap shadcn's <Button>, don't duplicate buttonVariants

Forking Button.tsx just to add magnetic hover creates drift every time you update your design tokens. Instead, build a MagneticButton wrapper that accepts ButtonProps, forwards refs cleanly, and disables magnetic motion whenever props.disabled is true.

components/ui/magnetic-button.tsx
"use client";

import * as React from "react";
import { Button, type ButtonProps } from "@/components/ui/button";
import { cn } from "@/lib/utils";

type MagneticButtonProps = ButtonProps & {
  strength?: number;
  maxOffset?: number;
};

export function MagneticButton({
  strength = 0.3,
  maxOffset = 16,
  disabled,
  className,
  ...props
}: MagneticButtonProps) {
  const fieldRef = React.useRef<HTMLSpanElement>(null);
  const innerRef = React.useRef<HTMLSpanElement>(null);

  React.useEffect(() => {
    const field = fieldRef.current;
    const inner = innerRef.current;
    if (!field || !inner || disabled) {
      if (inner) inner.style.transform = "";
      return;
    }
    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 += (-300 * (x - tx) - 22 * vx) * dt;
      vy += (-300 * (y - ty) - 22 * 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(-maxOffset, Math.min(maxOffset, (e.clientX - (r.left + r.width / 2)) * strength));
      ty = Math.max(-maxOffset, Math.min(maxOffset, (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, maxOffset, disabled]);

  return (
    <span ref={fieldRef} className="inline-flex">
      <span ref={innerRef} className="inline-flex will-change-transform">
        <Button disabled={disabled} className={cn(className)} {...props} />
      </span>
    </span>
  );
}

2. Or install straight via the shadcn registry

Because Kipluo components follow the shadcn source-ownership model and use the exact same Tailwind v4 CSS variables (--background, --foreground, --ring, --border), you can drop them straight into components/kipluo/ without introducing a competing theme provider.

terminal
npx kipluo add magnetic-surface

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 already maintain a small set of shadcn/ui buttons and want one magnetic variant.

Install Kipluo when

You want a full shadcn-native suite of physics components (MagneticSurface, ActionMorph, SpringToggle, ElasticSlider, etc.) that inherit your Tailwind tokens.

Keep building