Loading…
← All components

Cursors · Blend

Inverted Disc

A disc in difference blend mode that inverts everything it passes over and labels actions (VIEW, READ…).

Move over the words, then the buttons.

Read the docsdata-cursor-text
<InvertedTypographyCursor />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 inverted-disc
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/InvertedTypographyCursor.tsx and paste it in.

components/InvertedTypographyCursor.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/** Exponential smoothing factor for a frame: frame-rate independent `lerp`. */
160const damp = (lambda: number, dt: number) => 1 - Math.exp(-lambda * dt);
161
162export interface InvertedCursorProps {
163 /** Diameter of the disc at rest, in CSS pixels (it grows over elements with a label). Default 22. */
164 size?: number;
165 /** Label over interactive elements that have no `data-cursor-text`. Default "VIEW". */
166 defaultText?: string;
167 /** Hide the system cursor while the disc is shown. Default true. */
168 hideNativeCursor?: boolean;
169}
170
171/**
172 * A white disc in `mix-blend-mode: difference`: it inverts every color it passes over. Over an
173 * element with `data-cursor-text="READ"` (or any link / button) it grows into a badge carrying that
174 * word — in the same inverted ink.
175 */
176function InvertedTypographyCursor({ size = 22, defaultText = "VIEW", hideNativeCursor = true }: InvertedCursorProps) {
177 const enabled = useCursorEnabled();
178 const pointer = useCursorPointer(enabled);
179 useHideNativeCursor(enabled && hideNativeCursor);
180 const disc = useRef<HTMLDivElement>(null);
181 const label = useRef<HTMLSpanElement>(null);
182 const state = useRef({ x: -100, y: -100, d: size, text: "", opacity: 0 });
183
184 useAnimationFrame(enabled, (dt) => {
185 const el = disc.current;
186 const p = pointer.current;
187 if (!el) return;
188 settlePointer(p, dt);
189 const s = state.current;
190 s.x += (p.x - s.x) * damp(16, dt);
191 s.y += (p.y - s.y) * damp(16, dt);
192 const text = p.target ? (p.target.dataset.cursorText ?? defaultText) : "";
193 const big = Math.max(size * 3.6, text.length * 11 + 34);
194 s.d += ((text ? big : p.down ? size * 0.7 : size) - s.d) * damp(12, dt);
195 s.opacity += ((p.visible ? 1 : 0) - s.opacity) * damp(12, dt);
196 if (label.current && text !== s.text) {
197 s.text = text;
198 label.current.textContent = text;
199 }
200 if (label.current) label.current.style.opacity = text ? String(Math.min(1, (s.d - size) / (big - size))) : "0";
201 el.style.width = el.style.height = `${s.d}px`;
202 el.style.opacity = s.opacity.toFixed(3);
203 el.style.transform = `translate3d(${s.x - s.d / 2}px, ${s.y - s.d / 2}px, 0)`;
204 });
205
206 if (!enabled) return null;
207 return (
208 <div
209 ref={disc}
210 aria-hidden="true"
211 style={{
212 ...FOLLOWER_STYLE,
213 width: size,
214 height: size,
215 borderRadius: "50%",
216 background: "#fff",
217 mixBlendMode: "difference",
218 opacity: 0,
219 display: "flex",
220 alignItems: "center",
221 justifyContent: "center",
222 overflow: "hidden",
223 }}
224 >
225 {/* Black text on the white disc: in difference mode it shows the page's own colors, cut out */}
226 <span ref={label} style={{ color: "#000", font: "700 13px/1 system-ui, sans-serif", letterSpacing: "0.14em", textTransform: "uppercase", whiteSpace: "nowrap", opacity: 0 }} />
227 </div>
228 );
229}
230
231export default memo(InvertedTypographyCursor);
03

Drop it in a page

Updated live with the settings above.

app/page.tsx
1import InvertedTypographyCursor from "@/components/InvertedTypographyCursor";
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 <InvertedTypographyCursor
8 defaultText="VIEW"
9 size={22}
10 />
11 </div>
12 );
13}
Next componentConstellation→