-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcode-block.tsx
More file actions
334 lines (307 loc) · 10.3 KB
/
code-block.tsx
File metadata and controls
334 lines (307 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
import { Check, Copy } from 'lucide-react';
import { useEffect, useRef, useState } from 'react';
import { Prism as SyntaxHighlighter } from 'react-syntax-highlighter';
import {
atomDark,
nightOwl,
nord,
oneDark,
oneLight,
prism,
vs,
vscDarkPlus,
} from 'react-syntax-highlighter/dist/esm/styles/prism/index.js';
import { Button } from '@/components/ui/button';
import { cn } from '@/lib';
// ============================================================================
// Types
// ============================================================================
/**
* All Apollo theme variants supported by CodeBlock.
*
* Standard (apollo-core):
* - `'dark'` / `'light'` — Default dark / light
* - `'dark-hc'` / `'light-hc'` — High contrast variants
*
* Future / Demo themes:
* - `'future-dark'` / `'future-light'` — Future zinc palette, cyan brand
* - `'wireframe'` — Greyscale / prototyping
* - `'vertex'` — Deep blue-grey, teal brand
* - `'canvas'` — Apollo MUI dark, orange brand
*
* When no theme is passed the component auto-detects from the Apollo
* `<body>` class and switches live when the theme changes.
*/
export type CodeBlockTheme =
| 'dark'
| 'light'
| 'dark-hc'
| 'light-hc'
| 'future-dark'
| 'future-light'
| 'wireframe'
| 'vertex'
| 'canvas';
export interface CodeBlockProps {
/** The code string to display */
children: string;
/** Programming language for syntax highlighting (e.g. 'tsx', 'python', 'sql') */
language?: string;
/** Show line numbers on the left */
showLineNumbers?: boolean;
/** Show copy-to-clipboard button in the header */
showCopyButton?: boolean;
/** Optional file name displayed in the header */
fileName?: string;
/**
* Color scheme. When omitted the component auto-follows the active Apollo
* page theme by watching the class on `<body>` — switches live.
*/
theme?: CodeBlockTheme;
/** Wrap long lines instead of scrolling horizontally */
wrapLines?: boolean;
className?: string;
}
// ============================================================================
// Per-theme configuration
// ============================================================================
interface ThemeConfig {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
prismStyle: Record<string, any>;
bg: string;
headerBg: string;
labelColor: string;
iconColor: string;
lineNumberColor: string;
}
const THEME_CONFIG: Record<CodeBlockTheme, ThemeConfig> = {
// ── Dark — Nord for the apollo-core blue-grey palette ────────────────────
dark: {
prismStyle: nord,
bg: '#182027',
headerBg: '#111920',
labelColor: '#8ea1b1',
iconColor: '#6b8899',
lineNumberColor: '#2e3f4c',
},
// ── Light — VS Code Light on a clean white surface ───────────────────────
light: {
prismStyle: vs,
bg: '#ffffff',
headerBg: '#f0f4f8',
labelColor: '#374151',
iconColor: '#6b7280',
lineNumberColor: '#c8d4de',
},
// ── Future dark — VS Code Dark+ for a modern zinc feel ───────────────────
'future-dark': {
prismStyle: vscDarkPlus,
bg: 'var(--surface-raised)',
headerBg: '#09090b',
labelColor: '#a1a1aa',
iconColor: '#71717a',
lineNumberColor: '#3f3f46',
},
// ── Future light — VS Code Light for a clean zinc-50 feel ────────────────
'future-light': {
prismStyle: vs,
bg: 'var(--surface-raised)',
headerBg: '#f4f4f5',
labelColor: '#52525b',
iconColor: '#71717a',
lineNumberColor: '#d4d4d8',
},
// ── Wireframe — classic Prism on grey-50, minimal ────────────────────────
wireframe: {
prismStyle: prism,
bg: '#f9fafb',
headerBg: '#f3f4f6',
labelColor: '#6b7280',
iconColor: '#9ca3af',
lineNumberColor: '#d1d5db',
},
// ── Vertex — Night Owl on deep oklch blue-grey, teal brand ───────────────
vertex: {
prismStyle: nightOwl,
bg: 'oklch(0.21 0.03 258.5)',
headerBg: 'oklch(0.188 0.028 258.5)',
labelColor: '#a6b5c9',
iconColor: '#7a90a8',
lineNumberColor: 'oklch(0.32 0.03 258.5)',
},
// ── Canvas — Atom Dark for Apollo MUI dark, UiPath orange brand ──────────
canvas: {
prismStyle: atomDark,
bg: '#182027',
headerBg: '#111920',
labelColor: '#8ea1b1',
iconColor: '#6b8899',
lineNumberColor: '#2e3f4c',
},
// ── High-contrast dark ───────────────────────────────────────────────────
'dark-hc': {
prismStyle: oneDark,
bg: '#0a0a0a',
headerBg: '#141414',
labelColor: '#e4e4e4',
iconColor: '#c8c8c8',
lineNumberColor: '#505050',
},
// ── High-contrast light ──────────────────────────────────────────────────
'light-hc': {
prismStyle: oneLight,
bg: '#ffffff',
headerBg: '#e8e8e8',
labelColor: '#111827',
iconColor: '#374151',
lineNumberColor: '#9ca3af',
},
};
// ============================================================================
// Auto-detect Apollo theme from <body> class
// ============================================================================
// Check more specific / longer class names before short ones to avoid
// a class like "dark" matching inside "future-dark"
const BODY_CLASS_PRIORITY: CodeBlockTheme[] = [
'future-dark',
'future-light',
'dark-hc',
'light-hc',
'dark',
'light',
'wireframe',
'vertex',
'canvas',
];
function getBodyTheme(): CodeBlockTheme {
if (typeof document === 'undefined') return 'dark';
const bodyClasses = document.body.classList;
const htmlClasses = document.documentElement.classList;
return (
BODY_CLASS_PRIORITY.find((t) => bodyClasses.contains(t) || htmlClasses.contains(t)) ??
'future-dark'
);
}
function useApolloTheme(): CodeBlockTheme {
const [theme, setTheme] = useState<CodeBlockTheme>(getBodyTheme);
useEffect(() => {
if (typeof document === 'undefined') {
// In non-browser environments (e.g. SSR), skip observing
return;
}
const observer = new MutationObserver(() => setTheme(getBodyTheme()));
// Observe both body and documentElement to catch theme changes on either
const targets: (HTMLElement | null)[] = [document.body, document.documentElement];
targets.forEach((target) => {
if (target) {
observer.observe(target, { attributes: true, attributeFilter: ['class'] });
}
});
return () => observer.disconnect();
}, []);
return theme;
}
// ============================================================================
// CodeBlock
// ============================================================================
/**
* Syntax-highlighted code block built on react-syntax-highlighter (Prism engine).
*
* Supports 200+ languages, optional line numbers, a filename header, and a
* one-click copy button. Automatically follows the active Apollo theme by
* watching the body class — or accept an explicit `theme` prop to override.
*
* Supported themes: dark, light, dark-hc, light-hc, future-dark,
* future-light, wireframe, vertex, canvas.
*/
export function CodeBlock({
children,
language = 'tsx',
showLineNumbers = true,
showCopyButton = true,
fileName,
theme,
wrapLines = false,
className,
}: CodeBlockProps) {
const [copied, setCopied] = useState(false);
const timeoutRef = useRef<ReturnType<typeof setTimeout> | null>(null);
const detectedTheme = useApolloTheme();
const activeTheme = theme ?? detectedTheme;
const config = THEME_CONFIG[activeTheme];
const code = children.trim();
async function handleCopy() {
try {
await navigator.clipboard.writeText(code);
setCopied(true);
timeoutRef.current = setTimeout(() => setCopied(false), 1500);
} catch {
// Clipboard API not available — silent fail
}
}
useEffect(() => {
return () => {
if (timeoutRef.current) clearTimeout(timeoutRef.current);
};
}, []);
const showHeader = !!(fileName || language || showCopyButton);
return (
<div
className={cn(
'overflow-hidden rounded-lg border border-border future:border-border-subtle font-mono text-sm',
className
)}
style={{ background: config.bg }}
>
{/* ── Header ─────────────────────────────────────────────── */}
{showHeader && (
<div
className="flex items-center justify-between px-4 py-2 border-b border-border future:border-border-subtle"
style={{ background: config.headerBg }}
>
<span className="text-xs font-medium" style={{ color: config.labelColor }}>
{fileName ?? language}
</span>
{showCopyButton && (
<Button
icon
variant="ghost"
size="2xs"
style={{ color: config.iconColor }}
onClick={handleCopy}
aria-label={copied ? 'Copied!' : 'Copy code'}
>
{copied ? <Check /> : <Copy />}
</Button>
)}
</div>
)}
{/* ── Code ───────────────────────────────────────────────── */}
<SyntaxHighlighter
language={language}
style={config.prismStyle}
showLineNumbers={showLineNumbers}
wrapLines={wrapLines}
wrapLongLines={wrapLines}
customStyle={{
margin: 0,
border: 'none',
borderRadius: 0,
background: 'transparent',
padding: '1rem',
fontSize: 13,
lineHeight: 1.6,
}}
lineNumberStyle={{
color: config.lineNumberColor,
minWidth: '2.5em',
paddingRight: '1.25em',
userSelect: 'none',
}}
codeTagProps={{ style: { fontFamily: 'inherit' } }}
>
{code}
</SyntaxHighlighter>
</div>
);
}