Magnetic avatars
Five interactive avatars: a magnetic pull, a 3D tilt, a pop, a wobble and a following ring. 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/avatars-magneticDependencies, the @ai2/tokens theme and the component file are installed together.
Install dependencies
npm install motionCopy the source
components/ui/avatars-magnetic.tsx"use client"
import * as React from "react"
import { motion, useMotionValue, useReducedMotion, useSpring } from "motion/react"
import { cn } from "@/lib/utils"
/* Magnetic avatar family: 5 decorative avatars built on cursor physics and hover. Initials-based (no external image) - a consumer can put an <img> in place of the initials. Position and angle values are held with useMotionValue plus useSpring (no re-renders). Colour comes ONLY from tokens, size = the box scale. ALL pointer and hover effects are off under reduced-motion. */
export type StyledSize = "sm" | "md" | "lg" | "xl"
const box: Record<StyledSize, string> = {
sm: "size-8 text-xs",
md: "size-10 text-sm",
lg: "size-12 text-base",
xl: "size-16 text-lg",
}
const face =
"flex size-full items-center justify-center rounded-full bg-secondary font-medium text-secondary-foreground select-none"
type Props = React.ComponentProps<"div"> & { size?: StyledSize; initials?: string }
/* Magnetic: avatar imlece dogru yayli kayar. */
export function MagneticAvatar({ className, size = "md", initials = "AI", ...props }: Props) {
const ref = React.useRef<HTMLDivElement>(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<HTMLDivElement>) => {
if (reduce || !ref.current) return
const r = ref.current.getBoundingClientRect()
x.set((e.clientX - (r.left + r.width / 2)) * 0.35)
y.set((e.clientY - (r.top + r.height / 2)) * 0.35)
}
const reset = () => {
x.set(0)
y.set(0)
}
return (
<div
ref={ref}
data-slot="styled-avatar"
onPointerMove={onMove}
onPointerLeave={reset}
className={cn("relative inline-flex", box[size], className)}
{...props}
>
<motion.span className={face} style={reduce ? undefined : { x: springX, y: springY }}>
{initials}
</motion.span>
</div>
)
}
/* Tilt: the avatar tilts in 3D towards the cursor (rotateX/rotateY from the pointer). */
export function TiltAvatar({ className, size = "md", initials = "AI", ...props }: Props) {
const ref = React.useRef<HTMLDivElement>(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<HTMLDivElement>) => {
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 * -24)
ry.set(px * 24)
}
const reset = () => {
rx.set(0)
ry.set(0)
}
return (
<div
ref={ref}
data-slot="styled-avatar"
onPointerMove={onMove}
onPointerLeave={reset}
className={cn("relative inline-flex", box[size], className)}
{...props}
>
<motion.span
className={face}
style={reduce ? undefined : { rotateX: springRx, rotateY: springRy, transformPerspective: 400 }}
>
{initials}
</motion.span>
</div>
)
}
/* Pop: grows with a spring on hover. */
export function PopAvatar({ className, size = "md", initials = "AI", ...props }: Props) {
const reduce = useReducedMotion()
return (
<div data-slot="styled-avatar" className={cn("relative inline-flex", box[size], className)} {...props}>
<motion.span
className={face}
whileHover={reduce ? undefined : { scale: 1.15 }}
transition={{ type: "spring", stiffness: 400, damping: 14 }}
>
{initials}
</motion.span>
</div>
)
}
/* Wobble: does a gentle rotate wobble on hover. */
export function WobbleAvatar({ className, size = "md", initials = "AI", ...props }: Props) {
const reduce = useReducedMotion()
return (
<div data-slot="styled-avatar" className={cn("relative inline-flex", box[size], className)} {...props}>
<motion.span
className={face}
whileHover={reduce ? undefined : { rotate: [0, -9, 7, -5, 0] }}
transition={{ duration: 0.6, ease: "easeInOut" }}
>
{initials}
</motion.span>
</div>
)
}
/* Follow: a small token ring trailing the cursor on hover. */
export function FollowAvatar({ className, size = "md", initials = "AI", ...props }: Props) {
const ref = React.useRef<HTMLDivElement>(null)
const reduce = useReducedMotion()
const x = useMotionValue(0)
const y = useMotionValue(0)
const springX = useSpring(x, { stiffness: 150, damping: 15 })
const springY = useSpring(y, { stiffness: 150, damping: 15 })
const onMove = (e: React.PointerEvent<HTMLDivElement>) => {
if (reduce || !ref.current) return
const r = ref.current.getBoundingClientRect()
x.set((e.clientX - (r.left + r.width / 2)) * 0.6)
y.set((e.clientY - (r.top + r.height / 2)) * 0.6)
}
const reset = () => {
x.set(0)
y.set(0)
}
return (
<div
ref={ref}
data-slot="styled-avatar"
onPointerMove={onMove}
onPointerLeave={reset}
className={cn("group relative inline-flex", box[size], className)}
{...props}
>
<motion.span
aria-hidden="true"
className="pointer-events-none absolute left-1/2 top-1/2 size-1/2 -translate-x-1/2 -translate-y-1/2 rounded-full border-2 border-primary opacity-0 transition-opacity duration-(--motion-base) group-hover:opacity-100 motion-reduce:hidden"
style={reduce ? undefined : { x: springX, y: springY }}
/>
<span className={face}>{initials}</span>
</div>
)
}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
Springs toward the cursor.
import { MagneticAvatar } from "@/components/ui/avatars-magnetic"
<MagneticAvatar />Tilt
Tilts in 3D toward the cursor.
import { TiltAvatar } from "@/components/ui/avatars-magnetic"
<TiltAvatar />Pop
Scales up with a spring on hover.
import { PopAvatar } from "@/components/ui/avatars-magnetic"
<PopAvatar />Wobble
A gentle rotate wobble on hover.
import { WobbleAvatar } from "@/components/ui/avatars-magnetic"
<WobbleAvatar />Follow
A token ring lags and follows on hover.
import { FollowAvatar } from "@/components/ui/avatars-magnetic"
<FollowAvatar />ai2 Magnetic avatars: 5 styled variations on the token system
The ai2 Magnetic avatars are a set of 5 decorative button variations from the styled layer of the @ai2 design system, built around pointer-reactive avatars. 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, tilt and follow without re-rendering. 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 avatars stay still.
What is in the ai2 Magnetic avatars?
5 exports in one file: Magnetic, Tilt, Pop, Wobble and Follow. 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, tilt and follow without re-rendering.
- Reduced-motion aware: Under prefers-reduced-motion, all pointer tracking and springs are disabled and the avatars stay still.
- 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 avatars 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.