Magnetic buttons
Five takes on pointer physics: a magnetic pull, a repel, a 3D tilt toward the cursor, an elastic overshoot and a squish on press. Each is sized and token-driven.
Installation
The styled layer is free and installs like any other ai2 component.
Run the following command
npx shadcn@latest add @ai2/buttons-magneticDependencies, the @ai2/tokens theme and the component file are installed together.
Install dependencies
npm install motionCopy the source
components/ui/buttons-magnetic.tsx"use client"
import * as React from "react"
import {
motion,
useMotionValue,
useReducedMotion,
useSpring,
type HTMLMotionProps,
} from "motion/react"
import { cn } from "@/lib/utils"
/* Magnetic button family: 5 variations on cursor physics. Position values are held with useMotionValue plus useSpring (no re-renders). All are sized, take colour ONLY from tokens, and every pointer effect is disabled under reduced-motion. */
export type StyledSize = "sm" | "md" | "lg" | "xl"
const sizes: Record<StyledSize, string> = {
sm: "h-8 gap-1.5 rounded-md px-3 text-sm",
md: "h-9 gap-2 rounded-lg px-4 text-sm",
lg: "h-10 gap-2 rounded-lg px-5 text-sm",
xl: "h-12 gap-2.5 rounded-xl px-6 text-base",
}
const base =
"relative inline-flex shrink-0 select-none items-center justify-center font-medium whitespace-nowrap outline-none focus-visible:ring-[3px] focus-visible:ring-ring/50 disabled:pointer-events-none disabled:opacity-50 [&_svg]:pointer-events-none [&_svg]:size-4 [&_svg]:shrink-0 [&_i]:text-base [&_i]:leading-none"
type Props = HTMLMotionProps<"button"> & { size?: StyledSize }
/* Magnetic: buton imlece dogru yayli kayar. */
export function MagneticButton({ className, size = "md", children, ...props }: Props) {
const ref = React.useRef<HTMLButtonElement>(null)
const reduce = useReducedMotion()
const x = useMotionValue(0)
const y = useMotionValue(0)
const springX = useSpring(x, { stiffness: 300, damping: 20 })
const springY = useSpring(y, { stiffness: 300, damping: 20 })
const onMove = (e: React.PointerEvent<HTMLButtonElement>) => {
if (reduce || !ref.current) return
const r = ref.current.getBoundingClientRect()
x.set((e.clientX - (r.left + r.width / 2)) * 0.3)
y.set((e.clientY - (r.top + r.height / 2)) * 0.5)
}
const reset = () => {
x.set(0)
y.set(0)
}
return (
<motion.button
ref={ref}
data-slot="styled-button"
onPointerMove={onMove}
onPointerLeave={reset}
style={reduce ? undefined : { x: springX, y: springY }}
whileTap={reduce ? undefined : { scale: 0.95 }}
className={cn(base, sizes[size], "bg-primary text-primary-foreground", className)}
{...props}
>
{children}
</motion.button>
)
}
/* Repel: buton imlecten yayli sekilde KACAR. */
export function RepelButton({ className, size = "md", children, ...props }: Props) {
const ref = React.useRef<HTMLButtonElement>(null)
const reduce = useReducedMotion()
const x = useMotionValue(0)
const y = useMotionValue(0)
const springX = useSpring(x, { stiffness: 250, damping: 18 })
const springY = useSpring(y, { stiffness: 250, damping: 18 })
const onMove = (e: React.PointerEvent<HTMLButtonElement>) => {
if (reduce || !ref.current) return
const r = ref.current.getBoundingClientRect()
x.set((e.clientX - (r.left + r.width / 2)) * -0.4)
y.set((e.clientY - (r.top + r.height / 2)) * -0.6)
}
const reset = () => {
x.set(0)
y.set(0)
}
return (
<motion.button
ref={ref}
data-slot="styled-button"
onPointerMove={onMove}
onPointerLeave={reset}
style={reduce ? undefined : { x: springX, y: springY }}
whileTap={reduce ? undefined : { scale: 0.95 }}
className={cn(base, sizes[size], "bg-brand text-brand-foreground", className)}
{...props}
>
{children}
</motion.button>
)
}
/* Tilt: the button tilts in 3D towards the cursor (rotateX/Y from the pointer). */
export function TiltPointerButton({ className, size = "md", children, ...props }: Props) {
const ref = React.useRef<HTMLButtonElement>(null)
const reduce = useReducedMotion()
const rx = useMotionValue(0)
const ry = useMotionValue(0)
const springRx = useSpring(rx, { stiffness: 300, damping: 20 })
const springRy = useSpring(ry, { stiffness: 300, damping: 20 })
const onMove = (e: React.PointerEvent<HTMLButtonElement>) => {
if (reduce || !ref.current) return
const r = ref.current.getBoundingClientRect()
const px = (e.clientX - r.left) / r.width - 0.5
const py = (e.clientY - r.top) / r.height - 0.5
rx.set(py * -22)
ry.set(px * 22)
}
const reset = () => {
rx.set(0)
ry.set(0)
}
return (
<motion.button
ref={ref}
data-slot="styled-button"
onPointerMove={onMove}
onPointerLeave={reset}
style={reduce ? undefined : { rotateX: springRx, rotateY: springRy, transformPerspective: 500 }}
whileTap={reduce ? undefined : { scale: 0.96 }}
className={cn(base, sizes[size], "bg-primary text-primary-foreground", className)}
{...props}
>
{children}
</motion.button>
)
}
/* Elastic: overshoots and settles with a low-damping spring on hover. */
export function ElasticButton({ className, size = "md", children, ...props }: Props) {
const reduce = useReducedMotion()
return (
<motion.button
data-slot="styled-button"
whileHover={reduce ? undefined : { scale: 1.12 }}
whileTap={reduce ? undefined : { scale: 0.9 }}
transition={{ type: "spring", stiffness: 500, damping: 10 }}
className={cn(base, sizes[size], "bg-info text-info-foreground", className)}
{...props}
>
{children}
</motion.button>
)
}
/* Squish: basinca yassilir (scaleX artar, scaleY azalir). */
export function SquishButton({ className, size = "md", children, ...props }: Props) {
const reduce = useReducedMotion()
return (
<motion.button
data-slot="styled-button"
whileTap={reduce ? undefined : { scaleX: 1.12, scaleY: 0.82 }}
transition={{ type: "spring", stiffness: 500, damping: 15 }}
className={cn(base, sizes[size], "bg-primary text-primary-foreground", className)}
{...props}
>
{children}
</motion.button>
)
}Manual installs skip the @ai2/tokens theme, so add the token CSS from the theming guide or the tone colors will be missing.
Variations
5 takes on the same idea. Each is its own export, and every one accepts a size prop (sm, md, lg, xl) aligned to the base Button scale.
Magnetic
The button springs toward the cursor.
import { MagneticButton } from "@/components/ui/buttons-magnetic"
<MagneticButton>Magnetic</MagneticButton>Repel
The button springs away from the cursor.
import { RepelButton } from "@/components/ui/buttons-magnetic"
<RepelButton>Repel</RepelButton>Tilt
The button tilts in 3D toward the cursor.
import { TiltPointerButton } from "@/components/ui/buttons-magnetic"
<TiltPointerButton>Tilt</TiltPointerButton>Elastic
An overshooting spring on hover and tap.
import { ElasticButton } from "@/components/ui/buttons-magnetic"
<ElasticButton>Elastic</ElasticButton>Squish
The button squashes on press and springs back.
import { SquishButton } from "@/components/ui/buttons-magnetic"
<SquishButton>Squish</SquishButton>ai2 Magnetic buttons: 5 styled variations on the token system
The ai2 Magnetic buttons are a set of 5 decorative button variations from the styled layer of the @ai2 design system, built around pointer physics. They are free and MIT licensed, and every color comes from a semantic token, so they theme with the rest of ai2 in light and dark.
Motion runs on framer-motion: framer-motion useMotionValue and useSpring drive the pull, repel and tilt without re-rendering, plus elastic and squish springs. The styled layer is opt-in, so the dependency only lands if you use it; the base components stay lean. Under reduced motion, all pointer tracking and springs are disabled and the buttons stay fixed.
What is in the ai2 Magnetic buttons?
5 exports in one file: Magnetic, Repel, Tilt, Elastic and Squish. Each renders a native button and takes a size prop (sm, md, lg, xl) aligned to the base Button. They are separate from the base Button on purpose: the base keeps its clean variant, tone and size axes, while the styled layer carries the effects.
You own the file. Copy the one category file and you have all 5 variations, with no runtime dependency on ai2 itself.
Why use it
- On-system by construction: Every color resolves to an ai2 semantic token, so the buttons follow your theme in light and dark with no extra work.
- Effect without the sprawl: The decorations live in a dedicated styled file, so the base Button keeps its clean, predictable API.
- Accessible and honest: Each renders a real button element, keeps a visible focus ring, and respects prefers-reduced-motion.
Features
- Token-driven color: No hardcoded hex or oklch; the look recolors with your theme tokens.
- framer-motion: framer-motion useMotionValue and useSpring drive the pull, repel and tilt without re-rendering, plus elastic and squish springs.
- Reduced-motion aware: Under prefers-reduced-motion, all pointer tracking and springs are disabled and the buttons stay fixed.
- Size aligned to the base: Every variation takes sm, md, lg and xl matching the base Button height scale, so styled and base buttons line up in a row.
Production tips
- Use it for emphasis, not everywhere: Styled buttons draw the eye. Reserve them for the one action you want people to take on a screen, and use the base Button for the rest.
- Keep labels as verbs: The decoration adds weight, so a clear action label keeps the button scannable.
- Pick one variation per surface: The variations share a family; using two different ones in the same view competes for attention.
Works with the rest of ai2
The Magnetic buttons sit alongside the base Button and the rest of the @ai2 registry. They share the same token file, so a styled action next to a base button or a badge stays visually consistent in both modes.