Magnetic tooltips
Five pointer-reactive tooltips: magnetic drift, float, tilt, spotlight and parallax. Each is self-contained, sized and still opens on keyboard focus without a pointer.
Installation
The styled layer is free and installs like any other ai2 component.
Run the following command
npx shadcn@latest add @ai2/tooltips-magneticDependencies, the @ai2/tokens theme and the component file are installed together.
Install dependencies
npm install motionCopy the source
components/ui/tooltips-magnetic.tsx"use client"
import * as React from "react"
import { motion, useMotionTemplate, useMotionValue, useReducedMotion, useSpring } from "motion/react"
import { cn } from "@/lib/utils"
/* Magnetic tooltip family: 5 bubbles on a cursor-physics theme. The position values
are held with useMotionValue + useSpring (no re-render). The bubble opacity opens
through CSS group-hover/group-focus-within, so it is visible on keyboard focus
even without a pointer. ALL pointer effects are disabled under reduced-motion.
Color comes ONLY from tokens. size = the bubble padding/text. children = the
trigger, content = the tooltip. */
export type StyledSize = "sm" | "md" | "lg" | "xl"
const bubbleSize: Record<StyledSize, string> = {
sm: "px-2 py-1 text-xs",
md: "px-2.5 py-1.5 text-xs",
lg: "px-3 py-2 text-sm",
xl: "px-3.5 py-2.5 text-sm",
}
/* The positioning is on a static outer layer; the inner motion layer leaves the
transform to framer. */
const positioner = "pointer-events-none absolute bottom-full left-1/2 z-10 mb-2 -translate-x-1/2"
const bubbleReveal =
"block w-max max-w-56 rounded-lg font-medium opacity-0 shadow-md transition-opacity duration-(--motion-base) ease-(--motion-ease) group-hover:opacity-100 group-focus-within:opacity-100 motion-reduce:transition-none"
interface Props {
size?: StyledSize
content: React.ReactNode
children: React.ReactNode
className?: string
}
/* Magnetic: balon imlec x'ine dogru yayli suruklenir. */
export function MagneticTooltip({ size = "md", content, children, className }: Props) {
const ref = React.useRef<HTMLSpanElement>(null)
const reduce = useReducedMotion()
const x = useMotionValue(0)
const sx = useSpring(x, { stiffness: 300, damping: 22 })
const onMove = (e: React.PointerEvent<HTMLSpanElement>) => {
if (reduce || !ref.current) return
const r = ref.current.getBoundingClientRect()
x.set((e.clientX - (r.left + r.width / 2)) * 0.25)
}
const reset = () => x.set(0)
return (
<span ref={ref} data-slot="styled-tooltip" onPointerMove={onMove} onPointerLeave={reset} className="group relative inline-flex">
{children}
<span className={positioner}>
<motion.span role="tooltip" style={reduce ? undefined : { x: sx }} className={cn(bubbleReveal, bubbleSize[size], "border border-border bg-popover text-popover-foreground", className)}>
{content}
</motion.span>
</span>
</span>
)
}
/* Float: while open the bubble sways gently upward (static under reduced-motion). */
export function FloatTooltip({ size = "md", content, children, className }: Props) {
const reduce = useReducedMotion()
return (
<span data-slot="styled-tooltip" className="group relative inline-flex">
{children}
<span className={positioner}>
<motion.span
role="tooltip"
animate={reduce ? undefined : { y: [0, -4, 0] }}
transition={reduce ? undefined : { duration: 2.4, repeat: Infinity, ease: "easeInOut" }}
className={cn(bubbleReveal, bubbleSize[size], "border border-border bg-popover text-popover-foreground", className)}
>
{content}
</motion.span>
</span>
</span>
)
}
/* Tilt: balon imlece gore 3B egilir. */
export function TiltTooltip({ size = "md", content, children, className }: Props) {
const ref = React.useRef<HTMLSpanElement>(null)
const reduce = useReducedMotion()
const rx = useMotionValue(0)
const ry = useMotionValue(0)
const srx = useSpring(rx, { stiffness: 300, damping: 20 })
const sry = useSpring(ry, { stiffness: 300, damping: 20 })
const onMove = (e: React.PointerEvent<HTMLSpanElement>) => {
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 * -18)
ry.set(px * 18)
}
const reset = () => {
rx.set(0)
ry.set(0)
}
return (
<span ref={ref} data-slot="styled-tooltip" onPointerMove={onMove} onPointerLeave={reset} className="group relative inline-flex">
{children}
<span className={positioner}>
<motion.span
role="tooltip"
style={reduce ? undefined : { rotateX: srx, rotateY: sry, transformPerspective: 500 }}
className={cn(bubbleReveal, bubbleSize[size], "border border-border bg-popover text-popover-foreground", className)}
>
{content}
</motion.span>
</span>
</span>
)
}
/* Spotlight: balondaki token isik lekesi imleci takip eder. */
export function SpotlightTooltip({ size = "md", content, children, className }: Props) {
const ref = React.useRef<HTMLSpanElement>(null)
const reduce = useReducedMotion()
const mx = useMotionValue(50)
const my = useMotionValue(50)
const spot = useMotionTemplate`radial-gradient(60px circle at ${mx}% ${my}%, color-mix(in oklab, var(--color-info) 40%, transparent), transparent 70%)`
const onMove = (e: React.PointerEvent<HTMLSpanElement>) => {
if (reduce || !ref.current) return
const r = ref.current.getBoundingClientRect()
mx.set(((e.clientX - r.left) / r.width) * 100)
my.set(((e.clientY - r.top) / r.height) * 100)
}
return (
<span ref={ref} data-slot="styled-tooltip" onPointerMove={onMove} className="group relative inline-flex">
{children}
<span className={positioner}>
<span
role="tooltip"
className={cn(
"relative block w-max max-w-56 overflow-hidden rounded-lg font-medium opacity-0 shadow-md transition-opacity duration-(--motion-base) ease-(--motion-ease) group-hover:opacity-100 group-focus-within:opacity-100 motion-reduce:transition-none",
bubbleSize[size],
"border border-border bg-popover text-popover-foreground",
className
)}
>
<motion.span aria-hidden className="pointer-events-none absolute inset-0" style={reduce ? undefined : { background: spot }} />
<span className="relative">{content}</span>
</span>
</span>
</span>
)
}
/* Parallax: balon imlecin TERSINE kayar. */
export function ParallaxTooltip({ size = "md", content, children, className }: Props) {
const ref = React.useRef<HTMLSpanElement>(null)
const reduce = useReducedMotion()
const x = useMotionValue(0)
const y = useMotionValue(0)
const sx = useSpring(x, { stiffness: 260, damping: 24 })
const sy = useSpring(y, { stiffness: 260, damping: 24 })
const onMove = (e: React.PointerEvent<HTMLSpanElement>) => {
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.3)
}
const reset = () => {
x.set(0)
y.set(0)
}
return (
<span ref={ref} data-slot="styled-tooltip" onPointerMove={onMove} onPointerLeave={reset} className="group relative inline-flex">
{children}
<span className={positioner}>
<motion.span role="tooltip" style={reduce ? undefined : { x: sx, y: sy }} className={cn(bubbleReveal, bubbleSize[size], "border border-border bg-popover text-popover-foreground", className)}>
{content}
</motion.span>
</span>
</span>
)
}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 bubble drifts toward the pointer.
import { MagneticTooltip } from "@/components/ui/tooltips-magnetic"
<MagneticTooltip content="Drifts toward the cursor">
<button>Hover me</button>
</MagneticTooltip>Float
The bubble bobs gently while open.
import { FloatTooltip } from "@/components/ui/tooltips-magnetic"
<FloatTooltip content="Bobs gently">
<button>Hover me</button>
</FloatTooltip>Tilt
The bubble tilts based on the pointer.
import { TiltTooltip } from "@/components/ui/tooltips-magnetic"
<TiltTooltip content="Tilts to the pointer">
<button>Hover me</button>
</TiltTooltip>Spotlight
A token spotlight follows the cursor.
import { SpotlightTooltip } from "@/components/ui/tooltips-magnetic"
<SpotlightTooltip content="Spotlight follows">
<button>Hover me</button>
</SpotlightTooltip>Parallax
The bubble shifts opposite to the cursor.
import { ParallaxTooltip } from "@/components/ui/tooltips-magnetic"
<ParallaxTooltip content="Shifts against the cursor">
<button>Hover me</button>
</ParallaxTooltip>ai2 Magnetic tooltips: 5 styled variations on the token system
The ai2 Magnetic tooltips are a set of 5 decorative button variations from the styled layer of the @ai2 design system, built around pointer-reactive tooltips. 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 drift the bubble toward the pointer. 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 motion is disabled and the bubble still opens on focus.
What is in the ai2 Magnetic tooltips?
5 exports in one file: Magnetic, Float, Tilt, Spotlight and Parallax. 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 drift the bubble toward the pointer.
- Reduced-motion aware: Under prefers-reduced-motion, all pointer motion is disabled and the bubble still opens on focus.
- 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 tooltips 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.