Magnetic inputs
Five pointer-reactive inputs: a tilt, a glow trail, an expand, a shake and a spotlight. Each is sized, token-driven and wraps a real input.
Installation
The styled layer is free and installs like any other ai2 component.
Run the following command
npx shadcn@latest add @ai2/inputs-magneticDependencies, the @ai2/tokens theme and the component file are installed together.
Install dependencies
npm install motionCopy the source
components/ui/inputs-magnetic.tsx"use client"
import * as React from "react"
import {
motion,
useMotionTemplate,
useMotionValue,
useReducedMotion,
useSpring,
} from "motion/react"
import { cn } from "@/lib/utils"
/* Magnetic input family: 5 text inputs reacting to pointer and focus physics. Position values are held with useMotionValue plus useSpring (no re-renders). Colour comes ONLY from tokens, and every pointer and motion effect is off under reduced-motion. Each wraps a real <input> and passes every native prop through. */
export type StyledSize = "sm" | "md" | "lg" | "xl"
const height: Record<StyledSize, string> = {
sm: "h-8 text-sm",
md: "h-9 text-sm",
lg: "h-10 text-base",
xl: "h-12 text-base",
}
const fieldBase =
"w-full bg-transparent text-foreground outline-none placeholder:text-muted-foreground disabled:cursor-not-allowed disabled:opacity-50"
const wrapBase =
"relative inline-flex w-56 max-w-full items-center rounded-lg border border-field-border px-3 transition-colors focus-within:border-ring focus-within:ring-[3px] focus-within:ring-ring/50"
type Props = Omit<React.ComponentProps<"input">, "size"> & { size?: StyledSize }
/* Tilt: alan, imlece dogru hafifce 3B egilir. */
export function TiltInput({ className, size = "md", ...props }: Props) {
const ref = React.useRef<HTMLSpanElement>(null)
const reduce = useReducedMotion()
const rx = useSpring(useMotionValue(0), { stiffness: 300, damping: 20 })
const ry = useSpring(useMotionValue(0), { stiffness: 300, damping: 20 })
const onMove = (e: React.PointerEvent<HTMLSpanElement>) => {
if (reduce || !ref.current) return
const r = ref.current.getBoundingClientRect()
ry.set(((e.clientX - (r.left + r.width / 2)) / r.width) * 12)
rx.set(((r.top + r.height / 2 - e.clientY) / r.height) * 12)
}
const reset = () => {
rx.set(0)
ry.set(0)
}
return (
<motion.span
ref={ref}
data-slot="styled-input"
onPointerMove={onMove}
onPointerLeave={reset}
style={reduce ? undefined : { rotateX: rx, rotateY: ry, transformPerspective: 600 }}
className={cn(wrapBase, height[size], className)}
>
<input {...props} className={cn(fieldBase, "h-full")} />
</motion.span>
)
}
/* GlowTrail: a token glow blob follows the cursor horizontally on focus and hover. */
export function GlowTrailInput({ className, size = "md", ...props }: Props) {
const ref = React.useRef<HTMLSpanElement>(null)
const reduce = useReducedMotion()
const x = useSpring(useMotionValue(0), { stiffness: 250, damping: 25 })
const left = useMotionTemplate`${x}px`
const onMove = (e: React.PointerEvent<HTMLSpanElement>) => {
if (reduce || !ref.current) return
const r = ref.current.getBoundingClientRect()
x.set(e.clientX - r.left)
}
return (
<span
ref={ref}
data-slot="styled-input"
onPointerMove={onMove}
className={cn(wrapBase, "group overflow-hidden", height[size], className)}
>
{reduce ? null : (
<motion.span
aria-hidden="true"
style={{ left, x: "-50%" }}
className="pointer-events-none absolute top-1/2 h-24 w-24 -translate-y-1/2 rounded-full bg-info/25 opacity-0 blur-2xl transition-opacity duration-(--motion-base) group-focus-within:opacity-100"
/>
)}
<input {...props} className={cn(fieldBase, "relative h-full")} />
</span>
)
}
/* Expand: odakta yayli olarak biraz genisler. */
export function ExpandInput({ className, size = "md", ...props }: Props) {
const reduce = useReducedMotion()
const [focused, setFocused] = React.useState(false)
return (
<motion.span
data-slot="styled-input"
animate={reduce ? undefined : { width: focused ? 256 : 224 }}
transition={{ type: "spring", stiffness: 300, damping: 26 }}
className={cn(wrapBase, "w-56", height[size], className)}
>
<input
{...props}
onFocus={(e) => {
setFocused(true)
props.onFocus?.(e)
}}
onBlur={(e) => {
setFocused(false)
props.onBlur?.(e)
}}
className={cn(fieldBase, "h-full")}
/>
</motion.span>
)
}
/* Shake: invalid prop true iken kisa bir titreme oynatir (danger kenarlik). */
export function ShakeInput({
className,
size = "md",
invalid = false,
...props
}: Props & { invalid?: boolean }) {
const reduce = useReducedMotion()
return (
<motion.span
data-slot="styled-input"
data-tone={invalid ? "danger" : "neutral"}
animate={reduce ? undefined : invalid ? { x: [0, -5, 5, -4, 4, 0] } : { x: 0 }}
transition={{ duration: 0.4, ease: "easeInOut" }}
className={cn(
wrapBase,
invalid && "border-danger focus-within:border-danger focus-within:ring-danger/25",
height[size],
className
)}
>
<input
{...props}
aria-invalid={invalid ? "true" : undefined}
className={cn(fieldBase, "h-full")}
/>
</motion.span>
)
}
/* Spotlight: alan boyunca imleci takip eden radyal token isik. */
export function SpotlightInput({ className, size = "md", ...props }: Props) {
const ref = React.useRef<HTMLSpanElement>(null)
const reduce = useReducedMotion()
const x = useMotionValue(0)
const y = useMotionValue(0)
const bg = useMotionTemplate`radial-gradient(120px circle at ${x}px ${y}px, color-mix(in oklab, var(--color-info) 22%, transparent), transparent 70%)`
const onMove = (e: React.PointerEvent<HTMLSpanElement>) => {
if (reduce || !ref.current) return
const r = ref.current.getBoundingClientRect()
x.set(e.clientX - r.left)
y.set(e.clientY - r.top)
}
return (
<span
ref={ref}
data-slot="styled-input"
onPointerMove={onMove}
className={cn(wrapBase, "group overflow-hidden", height[size], className)}
>
{reduce ? null : (
<motion.span
aria-hidden="true"
style={{ background: bg }}
className="pointer-events-none absolute inset-0 opacity-0 transition-opacity duration-(--motion-base) group-hover:opacity-100 group-focus-within:opacity-100"
/>
)}
<input {...props} className={cn(fieldBase, "relative h-full")} />
</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.
Tilt
A subtle 3D tilt toward the cursor.
import { TiltInput } from "@/components/ui/inputs-magnetic"
<TiltInput placeholder="Focus me" />Glow trail
A token glow follows focus.
import { GlowTrailInput } from "@/components/ui/inputs-magnetic"
<GlowTrailInput placeholder="Focus me" />Expand
Grows slightly wider on focus with a spring.
import { ExpandInput } from "@/components/ui/inputs-magnetic"
<ExpandInput placeholder="Focus me" />Shake
Shakes on an invalid prop.
import { ShakeInput } from "@/components/ui/inputs-magnetic"
<ShakeInput placeholder="Focus me" />Spotlight
A token spotlight follows the cursor.
import { SpotlightInput } from "@/components/ui/inputs-magnetic"
<SpotlightInput placeholder="Focus me" />ai2 Magnetic inputs: 5 styled variations on the token system
The ai2 Magnetic inputs are a set of 5 decorative button variations from the styled layer of the @ai2 design system, built around pointer-reactive inputs. 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 tilt, glow and spotlight 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 is disabled and the fields still focus.
What is in the ai2 Magnetic inputs?
5 exports in one file: Tilt, Glow trail, Expand, Shake and Spotlight. 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 tilt, glow and spotlight without re-rendering.
- Reduced-motion aware: Under prefers-reduced-motion, all pointer tracking is disabled and the fields still 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 inputs 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.