Spring trail
Lists, menus, sidebars. The leader takes the line, the pack chases and closes up.
- Sport
- Athletics
- Move
- The leader takes the line, the pack chases and closes up.
- For
- Lists, menus, sidebars
lead spring 380 / 26 · 3 ghosts chase it · reduced: one highlight
01 Install
npx shadcn@latest add https://moveui-red.vercel.app/r/spring-trail.jsonIt brings the tokens with it. The first time, import the tokens once in your root layout:
import "@/components/moveui/tokens.css";Not using shadcn? Copy the files below into components/moveui/, and tokens.css next to them.
02 Use it
import { SpringTrail } from "@/components/moveui/spring-trail/spring-trail";
<SpringTrail rows="a">
<a href="/inbox">Inbox</a>
<a href="/drafts">Drafts</a>
<a href="/sent">Sent</a>
</SpringTrail>03 Or ask your agent
Paste this into Claude Code, Cursor or any coding agent. It installs the piece and wires it in.
Add the Move UI "Spring trail" piece to this project. It's for: Lists, menus, sidebars.
1. Install it: npx shadcn@latest add https://moveui-red.vercel.app/r/spring-trail.json
2. If the project doesn't import the Move UI tokens yet, add this once to the root layout: import "@/components/moveui/tokens.css";
3. Use it like this, adapted to where it belongs in my code:
import { SpringTrail } from "@/components/moveui/spring-trail/spring-trail";
<SpringTrail rows="a">
<a href="/inbox">Inbox</a>
<a href="/drafts">Drafts</a>
<a href="/sent">Sent</a>
</SpringTrail>
Keep its reduced-motion behaviour and keyboard support. Don't hardcode durations, easing or colours: they come from the --ml-* tokens.
Docs: https://moveui-red.vercel.app/pieces/spring-trailWant your agent to know the whole library and its motion rules? Add the Move UI skill to your project:
npx shadcn@latest add https://moveui-red.vercel.app/r/skill.json04 Code
"use client";
import { useEffect, useRef, type HTMLAttributes } from "react";
import "./spring-trail.css";
/**
* Spring trail: a hover highlight for any vertical list. It follows the row under the pointer on a
* spring and leans a little toward the cursor; softer ghosts chase it, so a fast move leaves a short
* trail that folds back in at rest. Keyboard focus moves it too.
* Wrap your list: <SpringTrail rows="a">…links…</SpringTrail>. Fine pointers get the trail;
* touch and reduced motion get a single highlight with no trail.
*/
const keys = ["x", "y", "w", "h", "o"] as const;
type Box = Record<(typeof keys)[number], number>;
const zero = (): Box => ({ x: 0, y: 0, w: 0, h: 0, o: 0 });
const num = (el: Element, name: string, fallback: number) => {
const v = parseFloat(getComputedStyle(el).getPropertyValue(name));
return Number.isFinite(v) ? v : fallback;
};
export function SpringTrail({
rows = "a, button",
inset = 0,
className,
children,
...props
}: HTMLAttributes<HTMLDivElement> & { rows?: string; inset?: number }) {
const ref = useRef<HTMLDivElement>(null);
const leadRef = useRef<HTMLSpanElement>(null);
const ghostRefs = useRef<(HTMLSpanElement | null)[]>([]);
useEffect(() => {
const nav = ref.current, leadEl = leadRef.current;
if (!nav || !leadEl) return;
const reduce = matchMedia("(prefers-reduced-motion: reduce)").matches;
const fine = matchMedia("(hover: hover) and (pointer: fine)").matches;
const STIFF = num(nav, "--ml-trail-stiffness", 380), DAMP = num(nav, "--ml-trail-damping", 26);
const PULL_X = 0.1, PULL_Y = 0.3; // how far the highlight leans toward the cursor
const TRAIL = reduce || !fine ? [] : [
{ stiff: 190, damp: 20, alpha: 0.55 },
{ stiff: 110, damp: 16, alpha: 0.35 },
{ stiff: 65, damp: 12, alpha: 0.2 },
];
const lead = { el: leadEl, pos: zero(), vel: zero() };
const ghosts = TRAIL.map((cfg, i) => ({ el: ghostRefs.current[i]!, cfg, pos: zero(), vel: zero() }));
ghostRefs.current.forEach((g, i) => { if (g) g.style.display = i < TRAIL.length ? "" : "none"; });
const goal = zero();
let raf = 0, last = 0;
const list = () => [...nav.querySelectorAll<HTMLElement>(rows)];
const paint = (p: typeof lead, opacity: number) => {
p.el.style.transform = `translate(${p.pos.x}px, ${p.pos.y}px)`;
p.el.style.width = `${p.pos.w}px`;
p.el.style.height = `${p.pos.h}px`;
p.el.style.opacity = String(Math.max(0, Math.min(1, opacity)));
};
const spring = (p: typeof lead, stiff: number, damp: number, dt: number) => {
let moving = false;
for (const k of keys) {
p.vel[k] += (stiff * (goal[k] - p.pos[k]) - damp * p.vel[k]) * dt;
p.pos[k] += p.vel[k] * dt;
if (Math.abs(p.vel[k]) > 0.01 || Math.abs(goal[k] - p.pos[k]) > 0.01) moving = true;
}
return moving;
};
const step = (t: number) => {
const dt = Math.min((t - last) / 1000, 1 / 30);
last = t;
let moving = spring(lead, STIFF, DAMP, dt);
paint(lead, lead.pos.o);
for (const g of ghosts) {
moving = spring(g, g.cfg.stiff, g.cfg.damp, dt) || moving;
// A ghost shows only while it lags behind, so the trail disappears at rest.
const gap = Math.hypot(g.pos.x - lead.pos.x, g.pos.y - lead.pos.y);
paint(g, g.cfg.alpha * lead.pos.o * Math.min(1, gap / 14));
}
raf = moving ? requestAnimationFrame(step) : 0;
};
const start = () => {
if (reduce) { Object.assign(lead.pos, goal); paint(lead, goal.o); return; }
if (!raf) { last = performance.now(); raf = requestAnimationFrame(step); }
};
const aim = (row: HTMLElement, e?: PointerEvent) => {
const r = row.getBoundingClientRect(), n = nav.getBoundingClientRect();
const dx = e ? (e.clientX - (r.left + r.width / 2)) * PULL_X : 0;
const dy = e ? (e.clientY - (r.top + r.height / 2)) * PULL_Y : 0;
Object.assign(goal, {
x: r.left - n.left + inset + dx, y: r.top - n.top + inset + dy,
w: r.width - inset * 2, h: r.height - inset * 2, o: 1,
});
// Coming from nothing: appear in place instead of flying in from the corner.
if (lead.pos.o < 0.05) for (const p of [lead, ...ghosts]) {
for (const k of ["x", "y", "w", "h"] as const) { p.pos[k] = goal[k]; p.vel[k] = 0; }
}
start();
};
let hot: HTMLElement | null = null;
const setHot = (row: HTMLElement | null) => {
if (hot === row) return;
hot?.removeAttribute("data-hot");
row?.setAttribute("data-hot", "");
hot = row;
};
// Pick the row by height, so the full width of the list counts as hovering it.
const onMove = (e: PointerEvent) => {
if (e.pointerType !== "mouse" && e.pointerType !== "pen") return;
const row = list().find((l) => { const r = l.getBoundingClientRect(); return e.clientY >= r.top && e.clientY < r.bottom; });
if (row) { aim(row, e); setHot(row); }
};
const onLeave = () => { goal.o = 0; setHot(null); start(); };
const onFocusIn = (e: FocusEvent) => {
const t = e.target as HTMLElement;
const row = list().find((l) => l.contains(t));
if (row && t.matches(":focus-visible")) { aim(row); setHot(row); }
};
const onFocusOut = (e: FocusEvent) => { if (!nav.contains(e.relatedTarget as Node)) onLeave(); };
nav.addEventListener("pointermove", onMove);
nav.addEventListener("pointerleave", onLeave);
nav.addEventListener("focusin", onFocusIn);
nav.addEventListener("focusout", onFocusOut);
return () => {
cancelAnimationFrame(raf);
nav.removeEventListener("pointermove", onMove);
nav.removeEventListener("pointerleave", onLeave);
nav.removeEventListener("focusin", onFocusIn);
nav.removeEventListener("focusout", onFocusOut);
};
}, [rows, inset]);
return (
<div ref={ref} className={["ml-trail", className].filter(Boolean).join(" ")} {...props}>
{[2, 1, 0].map((i) => (
<span key={i} ref={(el) => { ghostRefs.current[i] = el; }} className="ml-trail__hl ml-trail__ghost" aria-hidden="true" />
))}
<span ref={leadRef} className="ml-trail__hl" aria-hidden="true" />
{children}
</div>
);
}
/*
Spring trail. Lead highlight: spring 380 / 26 (--ml-trail-*), one small overshoot.
Three ghosts on softer springs (190, 110, 65) fade out as they catch up.
Reduced motion and touch: one highlight that jumps, no trail.
*/
.ml-trail {
--ml-trail-stiffness: 380;
--ml-trail-damping: 26;
position: relative;
isolation: isolate;
}
.ml-trail__hl {
position: absolute;
left: 0;
top: 0;
z-index: -1;
width: 0;
height: 0;
opacity: 0;
border-radius: var(--ml-radius);
background: var(--ml-surface-2);
pointer-events: none;
will-change: transform;
}
.ml-trail__hl:not(.ml-trail__ghost) { background: var(--ml-ink); }
.ml-trail [data-hot] { color: var(--ml-ink-fg); }
