Loading…
← All components

Cursors · Terminal

Retro Terminal

A blinking phosphor prompt that types the action you hover and leaves a trail of hex code.

Move over the words, then the buttons.

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

Customize

Phosphor#33ff66

Installation

Use it in your project

Or skip the copy-paste

One command: writes the file and installs the dependencies.

$ npx deepui-cli add retro-terminal
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/RetroTerminalPromptCursor.tsx and paste it in.

components/RetroTerminalPromptCursor.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/** Full-viewport, click-through layer above the page. */
10const OVERLAY_STYLE: CSSProperties = {
11 position: "fixed",
12 top: 0,
13 left: 0,
14 width: "100vw",
15 height: "100vh",
16 pointerEvents: "none",
17 zIndex: 9999,
18};
19
20const MONO_FONT = "ui-monospace, SFMono-Regular, Menlo, Consolas, monospace";
21
22/**
23 * False on touch screens (`pointer: coarse`) and during server rendering: the custom cursor is not
24 * drawn and the native cursor is left alone. Follows the media query live (tablet + mouse…).
25 */
26function useCursorEnabled() {
27 const [enabled, setEnabled] = useState(false);
28 useEffect(() => {
29 const query = window.matchMedia("(pointer: coarse)");
30 const sync = () => setEnabled(!query.matches);
31 sync();
32 query.addEventListener("change", sync);
33 return () => query.removeEventListener("change", sync);
34 }, []);
35 return enabled;
36}
37
38/** Hides the system cursor on the whole page while `active` (restored on unmount). */
39function useHideNativeCursor(active: boolean) {
40 useEffect(() => {
41 if (!active) return undefined;
42 const style = document.createElement("style");
43 style.setAttribute("data-custom-cursor", "");
44 style.textContent = "*, *::before, *::after { cursor: none !important; }";
45 document.head.appendChild(style);
46 return () => style.remove();
47 }, [active]);
48}
49
50interface CursorPointer {
51 /** Position in CSS pixels, relative to the viewport. */
52 x: number;
53 y: number;
54 /** Smoothed velocity, in CSS pixels per second. */
55 vx: number;
56 vy: number;
57 speed: number;
58 /** False until the first move, and while the pointer is outside the window. */
59 visible: boolean;
60 down: boolean;
61 /** The interactive element under the pointer (closest match of the selector), or null. */
62 target: HTMLElement | null;
63 /** Incremented on every press, to trigger click effects. */
64 clicks: number;
65}
66
67/**
68 * Tracks the pointer through window listeners and returns a ref read inside the animation loop:
69 * moving the mouse never re-renders React. All listeners are removed on unmount.
70 */
71function useCursorPointer(enabled: boolean, selector = INTERACTIVE_SELECTOR): RefObject<CursorPointer> {
72 const pointer = useRef<CursorPointer>({ x: -100, y: -100, vx: 0, vy: 0, speed: 0, visible: false, down: false, target: null, clicks: 0 });
73
74 useEffect(() => {
75 if (!enabled) return undefined;
76 let last = performance.now();
77 const p = pointer.current;
78 const pick = (event: PointerEvent) => {
79 const el = event.target instanceof Element ? event.target.closest(selector) : null;
80 p.target = el instanceof HTMLElement ? el : null;
81 };
82 const move = (event: PointerEvent) => {
83 if (event.pointerType === "touch") return;
84 const now = performance.now();
85 const dt = Math.max(1, now - last) / 1000;
86 last = now;
87 if (p.visible) {
88 // Smoothed: one jittery event must not make the effects jump
89 p.vx = p.vx * 0.6 + ((event.clientX - p.x) / dt) * 0.4;
90 p.vy = p.vy * 0.6 + ((event.clientY - p.y) / dt) * 0.4;
91 }
92 p.x = event.clientX;
93 p.y = event.clientY;
94 p.visible = true;
95 pick(event);
96 };
97 const down = (event: PointerEvent) => {
98 if (event.pointerType === "touch") return;
99 p.down = true;
100 p.clicks += 1;
101 pick(event);
102 };
103 const up = () => {
104 p.down = false;
105 };
106 const leave = () => {
107 p.visible = false;
108 p.down = false;
109 };
110 window.addEventListener("pointermove", move, { passive: true });
111 window.addEventListener("pointerdown", down, { passive: true });
112 window.addEventListener("pointerup", up, { passive: true });
113 document.documentElement.addEventListener("pointerleave", leave);
114 window.addEventListener("blur", leave);
115 return () => {
116 window.removeEventListener("pointermove", move);
117 window.removeEventListener("pointerdown", down);
118 window.removeEventListener("pointerup", up);
119 document.documentElement.removeEventListener("pointerleave", leave);
120 window.removeEventListener("blur", leave);
121 };
122 }, [enabled, selector]);
123
124 return pointer;
125}
126
127/**
128 * requestAnimationFrame loop: `frame(dt, time)` runs every frame (dt in seconds, capped) while
129 * `enabled`; the browser pauses it in background tabs. The latest callback is always used.
130 */
131function useAnimationFrame(enabled: boolean, frame: (dt: number, time: number) => void) {
132 const frameRef = useRef(frame);
133 useEffect(() => {
134 frameRef.current = frame;
135 });
136 useEffect(() => {
137 if (!enabled) return undefined;
138 let id = 0;
139 let last = performance.now();
140 const tick = (now: number) => {
141 const dt = Math.min((now - last) / 1000, 1 / 20);
142 last = now;
143 frameRef.current(dt, now / 1000);
144 id = requestAnimationFrame(tick);
145 };
146 id = requestAnimationFrame(tick);
147 return () => cancelAnimationFrame(id);
148 }, [enabled]);
149}
150
151/**
152 * Velocity only changes on pointer events: call this once per frame so it fades out when the pointer
153 * stops, and read `speed` afterwards.
154 */
155function settlePointer(p: CursorPointer, dt: number) {
156 const k = Math.exp(-10 * dt);
157 p.vx *= k;
158 p.vy *= k;
159 p.speed = Math.hypot(p.vx, p.vy);
160}
161
162/**
163 * Full-viewport 2D canvas sized to the window (pixel ratio capped at 2) and kept in sync on resize.
164 * Returns the canvas ref; read `canvas.getContext("2d")` in the loop. Draw in CSS pixels.
165 */
166function useViewportCanvas(enabled: boolean) {
167 const ref = useRef<HTMLCanvasElement>(null);
168 useEffect(() => {
169 if (!enabled) return undefined;
170 const canvas = ref.current;
171 if (!canvas) return undefined;
172 const resize = () => {
173 const dpr = Math.min(window.devicePixelRatio || 1, 2);
174 canvas.width = Math.round(window.innerWidth * dpr);
175 canvas.height = Math.round(window.innerHeight * dpr);
176 canvas.getContext("2d")?.setTransform(dpr, 0, 0, dpr, 0, 0);
177 };
178 resize();
179 window.addEventListener("resize", resize);
180 return () => window.removeEventListener("resize", resize);
181 }, [enabled]);
182 return ref;
183}
184
185/** Clears a canvas drawn in CSS pixels. */
186function clearCanvas(ctx: CanvasRenderingContext2D) {
187 ctx.save();
188 ctx.setTransform(1, 0, 0, 1, 0, 0);
189 ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);
190 ctx.restore();
191}
192
193/** The text a hovered element asks the cursor to show: data-cursor-text, then aria-label. */
194function cursorLabel(el: HTMLElement | null, fallback = "") {
195 if (!el) return "";
196 return el.dataset.cursorText ?? el.getAttribute("aria-label") ?? fallback;
197}
198
199/** "#rrggbb" (or "#rgb") to "r, g, b", for rgba() strings. */
200function hexToRgb(hex: string) {
201 let h = hex.replace("#", "");
202 if (h.length === 3) h = h.split("").map((c) => c + c).join("");
203 const n = parseInt(h.slice(0, 6), 16);
204 if (Number.isNaN(n)) return "255, 255, 255";
205 return `${(n >> 16) & 255}, ${(n >> 8) & 255}, ${n & 255}`;
206}
207
208/** 4. Rendering, materials and glow. */
209interface MaterialProps {
210 /** Main color of the object. Default "#6366f1". */
211 primaryColor?: string;
212 /** Color of the glow. Default "#00f0ff". */
213 emissiveColor?: string;
214 /** Strength of the glow, 0 (none) to 3. Default 1. */
215 emissiveIntensity?: number;
216 /** 0 (mirror, glass) to 1 (matte). Default 0.3. */
217 roughness?: number;
218 /** 0 (plastic, organic) to 1 (metal). Default 0.5. */
219 metalness?: number;
220 /** 0 (opaque) to 1 (clear glass): see-through, refracting surfaces. Default 0. */
221 transmission?: number;
222 /** Technical wireframe rendering. Default false. */
223 wireframe?: boolean;
224}
225
226export interface RetroTerminalCursorProps extends Pick<MaterialProps, "primaryColor"> {
227 /** Phosphor color. Default "#33ff66". */
228 primaryColor?: string;
229 /** @deprecated Use `primaryColor`. */
230 terminalColor?: string;
231 /** Blinks of the block per second. Default 2. */
232 blinkRate?: number;
233 /** Hex glyphs left per 100 px of movement. Default 5. */
234 trailDensity?: number;
235 /** Hide the system cursor while the terminal cursor is shown. Default true. */
236 hideNativeCursor?: boolean;
237}
238
239interface Glyph {
240 x: number;
241 y: number;
242 text: string;
243 age: number;
244 life: number;
245}
246
247const HEX = "0123456789ABCDEF";
248const randomHex = () => {
249 const n = Math.random() < 0.5 ? 2 : 4;
250 let s = Math.random() < 0.3 ? "0x" : "";
251 for (let i = 0; i < n; i++) s += HEX[Math.floor(Math.random() * 16)];
252 return s;
253};
254
255/**
256 * DOS / UNIX terminal cursor: a blinking phosphor block with a `>` prompt, leaving a trail of hex code
257 * that flickers out behind it. Over an interactive element the prompt types the action
258 * (`data-cursor-text`, else `aria-label`, else "OPEN").
259 */
260function RetroTerminalPromptCursor({ primaryColor, terminalColor = primaryColor ?? "#33ff66", blinkRate = 2, trailDensity = 5, hideNativeCursor = true }: RetroTerminalCursorProps) {
261 const enabled = useCursorEnabled();
262 const pointer = useCursorPointer(enabled);
263 useHideNativeCursor(enabled && hideNativeCursor);
264 const canvas = useViewportCanvas(enabled);
265 const state = useRef({ glyphs: [] as Glyph[], lastX: NaN, lastY: NaN, debt: 0, typed: 0, label: "" });
266
267 useAnimationFrame(enabled, (dt, time) => {
268 const ctx = canvas.current?.getContext("2d");
269 if (!ctx) return;
270 const p = pointer.current;
271 settlePointer(p, dt);
272 const s = state.current;
273 clearCanvas(ctx);
274 const rgb = hexToRgb(terminalColor);
275
276 if (p.visible) {
277 if (Number.isNaN(s.lastX)) {
278 s.lastX = p.x;
279 s.lastY = p.y;
280 }
281 s.debt += (Math.hypot(p.x - s.lastX, p.y - s.lastY) / 100) * trailDensity;
282 while (s.debt >= 1) {
283 s.debt -= 1;
284 s.glyphs.push({ x: s.lastX + (Math.random() - 0.5) * 16, y: s.lastY + (Math.random() - 0.5) * 16, text: randomHex(), age: 0, life: 0.6 + Math.random() * 0.7 });
285 }
286 s.lastX = p.x;
287 s.lastY = p.y;
288 } else {
289 s.lastX = NaN;
290 }
291
292 ctx.font = `500 12px ${MONO_FONT}`;
293 ctx.textBaseline = "middle";
294 ctx.shadowColor = `rgba(${rgb}, 0.8)`;
295 ctx.shadowBlur = 6;
296 s.glyphs = s.glyphs.filter((g) => (g.age += dt) < g.life);
297 for (const g of s.glyphs) {
298 const t = g.age / g.life;
299 // Phosphor decay with a little flicker; glyphs mutate as they fade
300 const flicker = Math.random() < 0.08 ? 0.3 : 1;
301 if (Math.random() < 0.04) g.text = randomHex();
302 ctx.fillStyle = `rgba(${rgb}, ${(1 - t) * 0.8 * flicker})`;
303 ctx.fillText(g.text, g.x, g.y + t * 6);
304 }
305 if (!p.visible) return;
306
307 // Prompt: "> █", or the action typed out letter by letter
308 const label = p.target ? cursorLabel(p.target, "OPEN").toUpperCase() : "";
309 if (label !== s.label) {
310 s.label = label;
311 s.typed = 0;
312 }
313 s.typed = Math.min(label.length, s.typed + dt * 28);
314 const on = Math.floor(time * blinkRate * 2) % 2 === 0;
315 const x = p.x + 6;
316 const y = p.y;
317 ctx.font = `700 16px ${MONO_FONT}`;
318 ctx.fillStyle = `rgba(${rgb}, 1)`;
319 ctx.fillText(">", x, y);
320 const typed = label.slice(0, Math.floor(s.typed));
321 ctx.fillText(typed, x + 14, y);
322 const caret = x + 14 + ctx.measureText(typed).width;
323 if (on) ctx.fillRect(caret + (typed ? 3 : 0), y - 9, 10, 18);
324 else ctx.fillRect(caret + (typed ? 3 : 0), y + 7, 10, 2);
325 // Faint scanlines over the block
326 ctx.shadowBlur = 0;
327 ctx.fillStyle = "rgba(0, 0, 0, 0.25)";
328 for (let sy = y - 9; sy < y + 9; sy += 3) ctx.fillRect(x - 2, sy, caret - x + 18, 1);
329 });
330
331 if (!enabled) return null;
332 return <canvas ref={canvas} aria-hidden="true" style={OVERLAY_STYLE} />;
333}
334
335export default memo(RetroTerminalPromptCursor);
03

Drop it in a page

Updated live with the settings above.

app/page.tsx
1import RetroTerminalPromptCursor from "@/components/RetroTerminalPromptCursor";
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 <RetroTerminalPromptCursor
8 blinkRate={2}
9 trailDensity={5}
10 primaryColor="#33ff66"
11 />
12 </div>
13 );
14}
Next componentAura Spotlight→