cursor preview.
A preview card that trails the pointer down a list with a little physical lag, and follows keyboard focus as well.
live component, try it
what to expect.
how it works.
The easing is 1 - exp(-dt/tau) rather than a fixed slice of the remaining gap each frame, so the trail feels the same at 30fps and at 144. The loop parks itself the moment it arrives, which means a pointer resting on one row schedules no frames at all. It follows focus as well as the pointer, because a preview that only exists for a mouse does not exist for anyone arriving by keyboard. On a touch screen there is no pointer to follow, so the demo walks the rows with a drawn cursor instead.
take it with you.
Three steps, and the stage above runs in your own project.
Install
Nothing to add. It runs on React alone, 18 or newer.
Copy the files
4 files, kept together in one folder,
cursor-preview/. The styles travel with them."use client"; import { useEffect, useLayoutEffect, useRef, useState } from "react"; import { createFollow, type FollowHandle } from "./follow"; import { useFinePointer, useReducedMotion } from "./motion"; import "./cursor-preview.css"; export interface PreviewRow { id: string; title: string; image: string; // optional clip, played muted in the card video?: string; } interface Props { rows: PreviewRow[]; // ms the card takes to close most of the gap to the pointer: lower is snappier tau?: number; } function Media({ row, still }: { row?: PreviewRow; still: boolean }) { if (!row) return null; if (row.video && !still) { return <video src={row.video} poster={row.image} muted loop autoPlay playsInline />; } return <img src={row.image} alt="" decoding="async" draggable={false} />; } const GAP = 18; const MARGIN = 10; const DWELL_MS = 1150; const SCRIPT_TAU = 240; const clamp = (v: number, lo: number, hi: number) => Math.min(Math.max(v, lo), hi); export default function CursorPreview({ rows, tau = 90 }: Props) { const fine = useFinePointer(); const reduced = useReducedMotion(); const boxRef = useRef<HTMLDivElement>(null); const cardRef = useRef<HTMLElement>(null); const ghostRef = useRef<HTMLDivElement>(null); const cardFollow = useRef<FollowHandle | null>(null); const [active, setActive] = useState<number | null>(fine ? null : 0); useLayoutEffect(() => { const box = boxRef.current; const card = cardRef.current; if (!box || !card) return; const place = (x: number, y: number) => { const w = card.offsetWidth; const h = card.offsetHeight; const flip = x + GAP + w > box.clientWidth - MARGIN; const left = flip ? x - GAP - w : x + GAP; card.style.translate = `${Math.round(clamp(left, MARGIN, Math.max(MARGIN, box.clientWidth - w - MARGIN)))}px ` + `${Math.round(clamp(y - h / 2, MARGIN, Math.max(MARGIN, box.clientHeight - h - MARGIN)))}px`; }; const follow = createFollow({ onFrame: place, tau }); cardFollow.current = follow; return () => { follow.kill(); cardFollow.current = null; }; }, [tau]); useEffect(() => { const box = boxRef.current; if (!box || !fine) return; const onMove = (e: PointerEvent) => { const rect = box.getBoundingClientRect(); const x = e.clientX - rect.left; const y = e.clientY - rect.top; if (reduced) cardFollow.current?.snap(x, y); else cardFollow.current?.target(x, y); }; box.addEventListener("pointermove", onMove, { passive: true }); return () => box.removeEventListener("pointermove", onMove); }, [fine, reduced]); useEffect(() => { const box = boxRef.current; const ghost = ghostRef.current; if (fine || !box || !ghost) return; const script = createFollow({ tau: SCRIPT_TAU, onFrame(x, y) { ghost.style.translate = `${Math.round(x)}px ${Math.round(y)}px`; if (reduced) cardFollow.current?.snap(x, y); else cardFollow.current?.target(x, y); }, }); let at = 0; const aim = () => { const rows = box.querySelectorAll(".cursor-lab-row"); const row = rows[at % rows.length]; if (!(row instanceof HTMLElement)) return; setActive(at % rows.length); script.target(row.offsetLeft + row.offsetWidth * 0.34, row.offsetTop + row.offsetHeight / 2); at++; }; aim(); const id = window.setInterval(aim, DWELL_MS); return () => { window.clearInterval(id); script.kill(); }; }, [fine, reduced]); return ( <div className="cursor-lab" ref={boxRef} data-shown={String(active !== null)}> <ul className="cursor-lab-rows"> {rows.map((row, i) => ( <li key={row.id}> <button type="button" className="cursor-lab-row" data-active={String(active === i)} onPointerEnter={() => setActive(i)} onPointerLeave={() => setActive(null)} onFocus={(e) => { setActive(i); const el = e.currentTarget; cardFollow.current?.snap( el.offsetLeft + el.offsetWidth * 0.34, el.offsetTop + el.offsetHeight / 2, ); }} onBlur={() => setActive(null)} > <span>{row.title}</span> <span aria-hidden="true">→</span> </button> </li> ))} </ul> <figure className="cursor-lab-card" ref={cardRef} aria-hidden="true"> <Media key={rows[active ?? 0]?.id} row={rows[active ?? 0]} still={reduced} /> </figure> {!fine && <div className="cursor-lab-ghost" ref={ghostRef} aria-hidden="true" />} </div> ); }Use it
The stage above, exactly as it runs here. The comments mark what to change.
"use client"; import CursorPreview, { type PreviewRow } from "./cursor-preview/CursorPreview"; // swap these for your own rows; add a video to play a clip in the card const ROWS: PreviewRow[] = [ { id: "movievault", title: "MovieVault", image: "https://picsum.photos/seed/movievault/640/400" }, { id: "quizzme", title: "QuizzMe!", image: "https://picsum.photos/seed/quizzme/640/400" }, { id: "pasteup", title: "Pasteup", image: "https://picsum.photos/seed/pasteup/640/400" }, { id: "tenzies", title: "Tenzies", image: "https://picsum.photos/seed/tenzies/640/400" }, ]; export default function CursorPreviewExample({ rows = ROWS }: { rows?: PreviewRow[] }) { // on touch screens a scripted cursor walks the rows instead return <CursorPreview rows={rows} tau={90} />; }