Loading…
← All components

Cursors · Glass

VisionOS Pill

A translucent glass pill on a spring that stretches and merges into the exact shape of the buttons you hover.

Move over the words, then the buttons.

Read the docsdata-cursor-text
<VisionProFluidCursor />Live

Customize

Installation

Use it in your project

Or skip the copy-paste

One command: writes the file and installs the dependencies.

$ npx deepui-cli add visionos-pill
01

Install

The 3D packages this component is built on.

Nothing to install: React is all it needs.

02

Add the component

One self-contained file: create components/VisionProFluidCursor.tsx and paste it in.

components/VisionProFluidCursor.tsx
1"use client";
2
3import { memo, useEffect, useRef, useState, type CSSProperties, type RefObject } from "react";
4
5/** What counts as interactive: the cursors react when the pointer is over one of these. */
6const INTERACTIVE_SELECTOR =
7 "a, button, [role='button'], input, select, textarea, label, summary, [data-cursor], [data-cursor-text], [data-cursor-hover]";
8
9/** A layer pinned to the top-left corner and moved with transforms (no layout, no repaint). */
10const FOLLOWER_STYLE: CSSProperties = {
11 position: "fixed",
12 top: 0,
13 left: 0,
14 pointerEvents: "none",
15 zIndex: 9999,
16 willChange: "transform",
17};
18
19/**
20 * False on touch screens (`pointer: coarse`) and during server rendering: the custom cursor is not
21 * drawn and the native cursor is left alone. Follows the media query live (tablet + mouse…).
22 */
23function useCursorEnabled() {
24 const [enabled, setEnabled] = useState(false);
25 useEffect(() => {
26 const query = window.matchMedia("(pointer: coarse)");
27 const sync = () => setEnabled(!query.matches);
28 sync();
29 query.addEventListener("change", sync);
30 return () => query.removeEventListener("change", sync);
31 }, []);
32 return enabled;
33}
34
35/** Hides the system cursor on the whole page while `active` (restored on unmount). */
36function useHideNativeCursor(active: boolean) {
37 useEffect(() => {
38 if (!active) return undefined;
39 const style = document.createElement("style");
40 style.setAttribute("data-custom-cursor", "");
41 style.textContent = "*, *::before, *::after { cursor: none !important; }";
42 document.head.appendChild(style);
43 return () => style.remove();
44 }, [active]);
45}
46
47interface CursorPointer {
48 /** Position in CSS pixels, relative to the viewport. */
49 x: number;
50 y: number;
51 /** Smoothed velocity, in CSS pixels per second. */
52 vx: number;
53 vy: number;
54 speed: number;
55 /** False until the first move, and while the pointer is outside the window. */
56 visible: boolean;
57 down: boolean;
58 /** The interactive element under the pointer (closest match of the selector), or null. */
59 target: HTMLElement | null;
60 /** Incremented on every press, to trigger click effects. */
61 clicks: number;
62}
63
64/**
65 * Tracks the pointer through window listeners and returns a ref read inside the animation loop:
66 * moving the mouse never re-renders React. All listeners are removed on unmount.
67 */
68function useCursorPointer(enabled: boolean, selector = INTERACTIVE_SELECTOR): RefObject<CursorPointer> {
69 const pointer = useRef<CursorPointer>({ x: -100, y: -100, vx: 0, vy: 0, speed: 0, visible: false, down: false, target: null, clicks: 0 });
70
71 useEffect(() => {
72 if (!enabled) return undefined;
73 let last = performance.now();
74 const p = pointer.current;
75 const pick = (event: PointerEvent) => {
76 const el = event.target instanceof Element ? event.target.closest(selector) : null;
77 p.target = el instanceof HTMLElement ? el : null;
78 };
79 const move = (event: PointerEvent) => {
80 if (event.pointerType === "touch") return;
81 const now = performance.now();
82 const dt = Math.max(1, now - last) / 1000;
83 last = now;
84 if (p.visible) {
85 // Smoothed: one jittery event must not make the effects jump
86 p.vx = p.vx * 0.6 + ((event.clientX - p.x) / dt) * 0.4;
87 p.vy = p.vy * 0.6 + ((event.clientY - p.y) / dt) * 0.4;
88 }
89 p.x = event.clientX;
90 p.y = event.clientY;
91 p.visible = true;
92 pick(event);
93 };
94 const down = (event: PointerEvent) => {
95 if (event.pointerType === "touch") return;
96 p.down = true;
97 p.clicks += 1;
98 pick(event);
99 };
100 const up = () => {
101 p.down = false;
102 };
103 const leave = () => {
104 p.visible = false;
105 p.down = false;
106 };
107 window.addEventListener("pointermove", move, { passive: true });
108 window.addEventListener("pointerdown", down, { passive: true });
109 window.addEventListener("pointerup", up, { passive: true });
110 document.documentElement.addEventListener("pointerleave", leave);
111 window.addEventListener("blur", leave);
112 return () => {
113 window.removeEventListener("pointermove", move);
114 window.removeEventListener("pointerdown", down);
115 window.removeEventListener("pointerup", up);
116 document.documentElement.removeEventListener("pointerleave", leave);
117 window.removeEventListener("blur", leave);
118 };
119 }, [enabled, selector]);
120
121 return pointer;
122}
123
124/**
125 * requestAnimationFrame loop: `frame(dt, time)` runs every frame (dt in seconds, capped) while
126 * `enabled`; the browser pauses it in background tabs. The latest callback is always used.
127 */
128function useAnimationFrame(enabled: boolean, frame: (dt: number, time: number) => void) {
129 const frameRef = useRef(frame);
130 useEffect(() => {
131 frameRef.current = frame;
132 });
133 useEffect(() => {
134 if (!enabled) return undefined;
135 let id = 0;
136 let last = performance.now();
137 const tick = (now: number) => {
138 const dt = Math.min((now - last) / 1000, 1 / 20);
139 last = now;
140 frameRef.current(dt, now / 1000);
141 id = requestAnimationFrame(tick);
142 };
143 id = requestAnimationFrame(tick);
144 return () => cancelAnimationFrame(id);
145 }, [enabled]);
146}
147
148/**
149 * Velocity only changes on pointer events: call this once per frame so it fades out when the pointer
150 * stops, and read `speed` afterwards.
151 */
152function settlePointer(p: CursorPointer, dt: number) {
153 const k = Math.exp(-10 * dt);
154 p.vx *= k;
155 p.vy *= k;
156 p.speed = Math.hypot(p.vx, p.vy);
157}
158
159/** Critically-damped-ish spring step for one value (mutates and returns the state). */
160interface Spring {
161 value: number;
162 velocity: number;
163}
164
165function stepSpring(s: Spring, target: number, stiffness: number, damping: number, dt: number) {
166 const force = (target - s.value) * stiffness - s.velocity * damping;
167 s.velocity += force * dt;
168 s.value += s.velocity * dt;
169 return s.value;
170}
171
172export interface VisionProCursorProps {
173 /** Backdrop blur of the glass. Default "14px". */
174 blurAmount?: string;
175 /** Tint of the glass (any CSS color, translucent). Default "rgba(255, 255, 255, 0.16)". */
176 bgColor?: string;
177 /** 0.2 (bouncy) → 1 (no overshoot): how the pill settles on its target. Default 0.72. */
178 springDamping?: number;
179 /** Size of the pill at rest, in CSS pixels. Default 22. */
180 size?: number;
181 /** Hide the system cursor while the pill is shown. Default true. */
182 hideNativeCursor?: boolean;
183 /** Elements the pill wraps itself around. Default: `[data-cursor="magnetic"]`, links and buttons. */
184 magneticSelector?: string;
185}
186
187const DEFAULT_MAGNETIC = "[data-cursor='magnetic'], a, button, [role='button']";
188const STIFFNESS = 340;
189// How far the wrapped element lets the pill drift toward the pointer (share of the offset)
190const DRIFT = 0.12;
191
192/**
193 * Apple visionOS-style pointer: a translucent glass pill (backdrop blur) that follows the pointer on a
194 * spring and, over `data-cursor="magnetic"` elements (and links, buttons), stretches and merges into the
195 * exact shape of the element — its size and corner radius — drifting slightly with the pointer.
196 */
197function VisionProFluidCursor({
198 blurAmount = "14px",
199 bgColor = "rgba(255, 255, 255, 0.16)",
200 springDamping = 0.72,
201 size = 22,
202 hideNativeCursor = true,
203 magneticSelector = DEFAULT_MAGNETIC,
204}: VisionProCursorProps) {
205 const enabled = useCursorEnabled();
206 const pointer = useCursorPointer(enabled, magneticSelector);
207 useHideNativeCursor(enabled && hideNativeCursor);
208 const pill = useRef<HTMLDivElement>(null);
209 const springs = useRef<Record<"x" | "y" | "w" | "h" | "r" | "o", Spring>>({
210 x: { value: -100, velocity: 0 },
211 y: { value: -100, velocity: 0 },
212 w: { value: size, velocity: 0 },
213 h: { value: size, velocity: 0 },
214 r: { value: size / 2, velocity: 0 },
215 o: { value: 0, velocity: 0 },
216 });
217 const appliedFilter = useRef("");
218
219 useAnimationFrame(enabled, (dt) => {
220 const el = pill.current;
221 const p = pointer.current;
222 if (!el) return;
223 settlePointer(p, dt);
224 const s = springs.current;
225 const damping = 2 * Math.sqrt(STIFFNESS) * Math.min(Math.max(springDamping, 0.2), 1.2);
226
227 let cx = p.x;
228 let cy = p.y;
229 let w = size;
230 let h = size;
231 let r = size / 2;
232 const target = p.target;
233 if (target) {
234 // Wrap the element: its box plus a small margin, its own rounding
235 const rect = target.getBoundingClientRect();
236 const pad = 6;
237 w = rect.width + pad * 2;
238 h = rect.height + pad * 2;
239 const radius = parseFloat(getComputedStyle(target).borderTopLeftRadius) || 10;
240 r = Math.min(radius + pad, h / 2, w / 2);
241 cx = rect.left + rect.width / 2 + (p.x - (rect.left + rect.width / 2)) * DRIFT;
242 cy = rect.top + rect.height / 2 + (p.y - (rect.top + rect.height / 2)) * DRIFT;
243 } else if (p.down) {
244 w = h = size * 0.82;
245 r = w / 2;
246 }
247
248 const x = stepSpring(s.x, cx, STIFFNESS, damping, dt);
249 const y = stepSpring(s.y, cy, STIFFNESS, damping, dt);
250 const ww = Math.max(4, stepSpring(s.w, w, STIFFNESS, damping, dt));
251 const hh = Math.max(4, stepSpring(s.h, h, STIFFNESS, damping, dt));
252 const rr = Math.max(2, stepSpring(s.r, r, STIFFNESS, damping, dt));
253 const o = stepSpring(s.o, p.visible ? 1 : 0, 200, 28, dt);
254 // A free pill stretches a little along its motion, like a liquid
255 const stretch = target ? 1 : Math.min(p.speed / 2400, 0.35);
256 const angle = Math.atan2(p.vy, p.vx);
257
258 // Free, the pill is frosted glass; wrapped around an element it only lifts it (blur would hide its text)
259 const filter = target ? "brightness(1.12) saturate(1.2)" : `blur(${blurAmount}) saturate(1.6)`;
260 if (appliedFilter.current !== filter) {
261 appliedFilter.current = filter;
262 el.style.backdropFilter = filter;
263 el.style.setProperty("-webkit-backdrop-filter", filter);
264 }
265 el.style.width = `${ww}px`;
266 el.style.height = `${hh}px`;
267 el.style.borderRadius = `${rr}px`;
268 el.style.opacity = String(Math.max(0, Math.min(1, o)));
269 el.style.transform = target
270 ? `translate3d(${x - ww / 2}px, ${y - hh / 2}px, 0)`
271 : `translate3d(${x - ww / 2}px, ${y - hh / 2}px, 0) rotate(${angle}rad) scale(${1 + stretch}, ${1 - stretch * 0.5}) rotate(${-angle}rad)`;
272 });
273
274 if (!enabled) return null;
275 return (
276 <div
277 ref={pill}
278 aria-hidden="true"
279 style={{
280 ...FOLLOWER_STYLE,
281 width: size,
282 height: size,
283 borderRadius: size / 2,
284 opacity: 0,
285 background: bgColor,
286 backdropFilter: `blur(${blurAmount}) saturate(1.6)`,
287 WebkitBackdropFilter: `blur(${blurAmount}) saturate(1.6)`,
288 boxShadow: "inset 0 1px 0 rgba(255,255,255,0.45), inset 0 -1px 0 rgba(255,255,255,0.08), 0 8px 24px rgba(0,0,0,0.18)",
289 border: "1px solid rgba(255,255,255,0.22)",
290 mixBlendMode: "normal",
291 }}
292 />
293 );
294}
295
296export default memo(VisionProFluidCursor);
03

Drop it in a page

Updated live with the settings above.

app/page.tsx
1import VisionProFluidCursor from "@/components/VisionProFluidCursor";
2
3export default function Section() {
4 return (
5 // The component fills this box: give it a position and a height
6 <div style={{ position: "relative", width: "100%", height: "100vh" }}>
7 <VisionProFluidCursor
8 blurAmount="14px"
9 springDamping={0.72}
10 size={22}
11 />
12 </div>
13 );
14}
Next componentTactical Crosshair→