The best way to control Claude Code, Codex and more from any computer or phone

main
tsx 183 lines 6 KB
Raw
1 import { useEffect, useState, type CSSProperties, type ReactNode } from "react";
2
3 export const SPINNER_FRAMES = ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"];
4
5 export const prefersReducedMotion = () =>
6 typeof window !== "undefined" && window.matchMedia("(prefers-reduced-motion: reduce)").matches;
7
8 /** Pauses of at least this long are a demo holding a finished frame. */
9 const HOLD_MS = 2000;
10
11 /** Set by the page's "Pause motion" control (WCAG 2.2.2). */
12 const isMotionPaused = () =>
13 typeof document !== "undefined" &&
14 document.documentElement.classList.contains("alc-motion-paused");
15
16 /** A wait that stops while motion is paused and resumes where it left off. */
17 const pausableWait = (ms: number) =>
18 new Promise<void>((resolve) => {
19 let left = ms;
20 let since = 0;
21 let timer: ReturnType<typeof setTimeout> | undefined;
22 const sync = () => {
23 if (timer !== undefined) {
24 clearTimeout(timer);
25 timer = undefined;
26 left -= performance.now() - since;
27 }
28 if (isMotionPaused()) return;
29 since = performance.now();
30 timer = setTimeout(() => {
31 removeEventListener("alc-motion-change", sync);
32 resolve();
33 }, left);
34 };
35 addEventListener("alc-motion-change", sync);
36 sync();
37 });
38
39 /**
40 * Timeline pause for the scripted terminal demos. With reduced motion the
41 * script fast-forwards to its first finished frame and stays there: short
42 * steps resolve immediately and the first hold never resolves, so a loop
43 * must reach a hold or stop on its own or it never yields and freezes the
44 * tab. Otherwise the wait honors the page's pause control.
45 */
46 export const sleep = (ms: number): Promise<void> =>
47 prefersReducedMotion()
48 ? ms >= HOLD_MS
49 ? new Promise<void>(() => {})
50 : Promise.resolve()
51 : pausableWait(ms);
52
53 export function useSpinner(active: boolean, intervalMs = 80): string {
54 const [i, setI] = useState(0);
55 useEffect(() => {
56 if (!active || prefersReducedMotion()) return;
57 const t = setInterval(() => setI((v) => (v + 1) % SPINNER_FRAMES.length), intervalMs);
58 return () => clearInterval(t);
59 }, [active, intervalMs]);
60 return SPINNER_FRAMES[i]!;
61 }
62
63 /** Run an async loop only once the element scrolls into view, and stop when it leaves. */
64 export function useInViewLoop(
65 ref: React.RefObject<HTMLElement | null>,
66 run: (signal: { aborted: boolean }) => void | Promise<void>,
67 ) {
68 useEffect(() => {
69 const el = ref.current;
70 if (!el) return;
71 const signal = { aborted: false };
72 let started = false;
73 const obs = new IntersectionObserver(
74 (entries) => {
75 for (const e of entries) {
76 if (e.isIntersecting && !started) {
77 started = true;
78 void run(signal);
79 obs.disconnect();
80 }
81 }
82 },
83 { threshold: 0.2 },
84 );
85 obs.observe(el);
86 return () => {
87 signal.aborted = true;
88 obs.disconnect();
89 };
90 }, []);
91 }
92
93 export function TermChrome({
94 title,
95 badge,
96 badgeColor,
97 children,
98 bodyMinHeight,
99 maxLines,
100 bare,
101 }: {
102 title: string;
103 badge?: string;
104 badgeColor?: string;
105 children: ReactNode;
106 bodyMinHeight?: number;
107 /**
108 * Locks the terminal body to exactly this many lines of content (computed
109 * from the body font-size × line-height + padding). Use in docs/embedded
110 * contexts where layout shift below the terminal must be avoided. Count
111 * the maximum number of visible lines the body ever renders. Takes
112 * precedence over `bodyMinHeight`.
113 */
114 maxLines?: number;
115 /**
116 * When true, omit the outer `.alc-term` wrapper and the dots/title header.
117 * Useful when this terminal is embedded inside another chrome (e.g. the
118 * mobile tab toggle that combines code + deploy into one card).
119 */
120 bare?: boolean;
121 }) {
122 // `1lh` resolves to the computed line-height of `.alc-term__body`, so the
123 // reservation scales with font-size and browser zoom. Padding (28px = 14px
124 // top + 14px bottom) is fixed visual chrome; with `box-sizing: border-box`
125 // it's included in `height`.
126 const bodyStyle: CSSProperties | undefined =
127 maxLines !== undefined
128 ? {
129 boxSizing: "border-box",
130 height: `calc(${maxLines} * 1lh + 28px)`,
131 minHeight: `calc(${maxLines} * 1lh + 28px)`,
132 overflow: "hidden",
133 }
134 : bodyMinHeight
135 ? { minHeight: bodyMinHeight }
136 : undefined;
137 if (bare) {
138 return (
139 <pre className="alc-term__body" style={bodyStyle}>
140 {children}
141 </pre>
142 );
143 }
144 return (
145 // `data-nosnippet`: the simulated terminal is decoration, not prose. Left
146 // visible to crawlers it leaks into Google-generated snippets as chrome
147 // fragments ("~/my-appDEV", "○localhost:1337/ HMR").
148 <div className="alc-term not-content" data-nosnippet="">
149 <div className="alc-term__header">
150 <span className="alc-code-block__dot" style={{ background: "var(--alc-dot-red)" }} />
151 <span className="alc-code-block__dot" style={{ background: "var(--alc-dot-yellow)" }} />
152 <span className="alc-code-block__dot" style={{ background: "var(--alc-dot-green)" }} />
153 <span className="alc-term__title">{title}</span>
154 <span style={{ flex: 1 }} />
155 {badge && badgeColor && (
156 <span
157 style={{
158 fontFamily: "var(--alc-font-mono)",
159 fontSize: 10,
160 letterSpacing: "0.14em",
161 fontWeight: 700,
162 padding: "2px 8px",
163 borderRadius: 4,
164 color: badgeColor,
165 border: `1px solid ${badgeColor}`,
166 background: "transparent",
167 transition: "color 280ms ease, border-color 280ms ease",
168 }}
169 >
170 {badge}
171 </span>
172 )}
173 </div>
174 <pre className="alc-term__body" style={bodyStyle}>
175 {children}
176 </pre>
177 </div>
178 );
179 }
180
181 export function Line({ children, style }: { children?: ReactNode; style?: CSSProperties }) {
182 return <div style={{ minHeight: "1.55em", whiteSpace: "pre", ...style }}>{children}</div>;
183 }