Yo-yo
Swings on its string below the pointer. Press to throw it down to sleep; let go to reel it back.
npx shadcn@latest add https://hoverui.fun/ r/ yoyo.jsonAdds components/cursors/yoyo.tsx. The CLI also installs the useCursor hook it runs on, once, into your hooks folder. The files are yours from there.
Move through the preview, then hover the controls and press.Open this on a computer to move it around: it follows a mouse, not a finger.
- Kind
- Toys
- Hook
- useCursor
- Dependencies
- None
- License
- MIT
How it behaves
A yo-yo swinging on its string below the pointer; a press throws it down to sleep, spinning, and letting go reels it back. Over anything clickable it climbs up close.
Usage
import { YoyoCursor } from "@/components/cursors/yoyo";
export function Section() {
return (
<section>
<YoyoCursor />
{/* this section now wears Yo-yo */}
</section>
);
}Put it inside <body> in your root layout to wear it on every page. Nested cursors take over inside their own element.
Props
- className
- Extra classes for the cursor's root. It draws in its own colors, set in the file; any outline or pointer dot it has follows currentColor, e.g. text-pink-500. Leave mix-blend-difference off: it would turn those colors to negatives.
- Hover targets
- a, button, input, select, textarea, label, [role=button], [data-cursor-label]
Source
"use client";
import { useRef } from "react";
import { cursorRoot, useCursor } from "@/hooks/use-cursor";
/** The yo-yo, face on, its markings in a `data-spin` group; exported so a page can draw it at rest. */
export function YoyoArt({ className = "" }: { className?: string }) {
return (
<svg viewBox="0 0 24 24" className={className} aria-hidden>
<circle cx="12" cy="12" r="11" fill="#3d7bff" stroke="currentColor" strokeWidth="1.2" />
<g data-spin style={{ transformOrigin: "12px 12px" }} stroke="#fff" strokeLinecap="round">
<circle cx="12" cy="12" r="6.5" fill="none" strokeWidth="0.8" opacity="0.6" />
<path d="M12 2.5v3M12 18.5v3" strokeWidth="1.4" />
</g>
<circle cx="12" cy="12" r="2" fill="#fff" stroke="currentColor" strokeWidth="0.8" />
</svg>
);
}
/** A yo-yo swinging on its string below the pointer; a press throws it down to sleep, spinning, and letting go reels it back. Over anything clickable it climbs up close. */
export function YoyoCursor({ className = "" }: { className?: string }) {
const s = useRef({ x: 0, y: 0, vx: 0, vy: 0, len: 48, spin: 0 });
const ref = useCursor((p, el) => {
const st = s.current;
const goal = p.down ? 120 : p.hover ? 24 : 48;
const before = st.len;
st.len += (goal - st.len) * p.ease(p.down ? 9 : 6);
if (!p.dt || p.reduced) [st.x, st.y, st.vx, st.vy] = [p.x, p.y + st.len, 0, 0];
else {
// A pendulum: gravity, a little air drag, and a string held taut at its length.
st.vy += 1400 * p.dt;
st.vx *= 1 - Math.min(p.dt * 1.5, 1);
st.vy *= 1 - Math.min(p.dt * 1.5, 1);
st.x += st.vx * p.dt;
st.y += st.vy * p.dt;
const [dx, dy] = [st.x - p.x, st.y - p.y];
const d = Math.hypot(dx, dy) || 1;
const [nx, ny] = [dx / d, dy / d];
[st.x, st.y] = [p.x + nx * st.len, p.y + ny * st.len];
const out = st.vx * nx + st.vy * ny;
[st.vx, st.vy] = [st.vx - out * nx, st.vy - out * ny];
// Unwinding spins it; held at the bottom, it sleeps, still spinning.
st.spin += (st.len - before) * 0.15 + (p.down ? p.dt * 20 : 0);
}
const [string, yoyo] = el.children as HTMLCollectionOf<HTMLElement>;
const line = string.firstElementChild!;
line.setAttribute("x1", String(p.x));
line.setAttribute("y1", String(p.y));
line.setAttribute("x2", String(st.x));
line.setAttribute("y2", String(st.y));
yoyo.style.transform = `translate(${st.x}px, ${st.y}px)`;
(yoyo.querySelector("[data-spin]") as SVGElement).style.transform = `rotate(${st.spin}rad)`;
});
return (
<div ref={ref} aria-hidden className={`${cursorRoot} ${className}`}>
<svg className="absolute top-0 left-0 size-px overflow-visible">
<line stroke="currentColor" strokeWidth="1" />
</svg>
<YoyoArt className="absolute size-6 -translate-1/2" />
</div>
);
}
It runs on the useCursor hook, which has its own source and API.
More toys cursors
- BalloonA balloon on a string, tugging up and swaying as you move. Puffs up over controls; a click pops it.
- DiceA die that tumbles as you move, lands on six over anything clickable, and rolls while you press.
- PlaneA paper plane that glides after the pointer and banks into its turns. It circles controls; a click throws it.
- TopA spinning top that wobbles and teeters when you stop, but never falls. Controls sink into dream levels; a click kicks it.
- LaserThe pointer is a laser dot and a cat chases it: it creeps, wiggles and pounces, bats at controls and naps when you stop.
Made a cursor of your own? Contributed cursors carry their maker's name on their page.
Contribute a cursor