Repository navigation
Expand file tree
/
Copy pathcode-block.tsx
More file actions
358 lines (332 loc) · 13.1 KB
/
Copy pathcode-block.tsx
File metadata and controls
358 lines (332 loc) · 13.1 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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
"use client";
import { useEffect, useId, useMemo, useRef, useState } from "react";
import { LayoutGroup, motion } from "motion/react";
import { useReducedMotion } from "@/lib/use-reduced-motion";
import { cn } from "@/lib/cn";
export type CodeFile = { name: string; language: "tsx" | "css"; code: string };
type Kind = "keyword" | "string" | "comment" | "number" | "plain";
type Token = { kind: Kind; text: string };
// Syntax colors are data, like the swatches in color-swatches: they encode
// token kinds rather than decorate, so they sit outside the token palette.
// Each pair keeps the same hue and roughly 4.5:1 against the surface in both
// themes; comments and plain text stay on the muted and foreground tokens.
const COLORS: Record<Kind, string | undefined> = {
keyword: "light-dark(oklch(0.5 0.14 285), oklch(0.78 0.11 285))",
string: "light-dark(oklch(0.5 0.1 155), oklch(0.8 0.1 155))",
number: "light-dark(oklch(0.5 0.14 285), oklch(0.78 0.11 285))",
comment: "var(--muted)",
plain: undefined,
};
const TS_KEYWORDS = new Set(
"import from export default function return const let type interface if else as new true false null undefined async await".split(
" ",
),
);
// One pass per language. Earlier alternatives win, so a number inside a
// string or comment stays part of that string or comment.
// Each group maps, in order, to the kind in GROUPS. A TS word is only a
// keyword when it is in TS_KEYWORDS; a CSS keyword is an at-rule or a
// property name at the start of a line.
const PATTERNS = {
tsx: /(\/\/[^\n]*|\/\*[\s\S]*?\*\/)|("[^"\n]*"|'[^'\n]*'|`[^`]*`)|(\b\d+(?:\.\d+)?\b)|([A-Za-z_$][\w$]*)/g,
css: /(\/\*[\s\S]*?\*\/)|("[^"\n]*"|'[^'\n]*')|(\b\d+(?:\.\d+)?(?:px|ms|s|rem|em|%)?)|(@[\w-]+|(?<=^[ \t]*)[a-z-]+(?=\s*:))/gm,
};
const GROUPS = ["comment", "string", "number", "word"] as const;
function tokenize(code: string, language: CodeFile["language"]): Token[][] {
const tokens: Token[] = [];
let last = 0;
for (const match of code.matchAll(PATTERNS[language])) {
const index = match.index ?? 0;
if (index > last) tokens.push({ kind: "plain", text: code.slice(last, index) });
const group = GROUPS[match.slice(1).findIndex((g) => g !== undefined)];
const kind: Kind =
group !== "word"
? group
: language === "css" || TS_KEYWORDS.has(match[0])
? "keyword"
: "plain";
tokens.push({ kind, text: match[0] });
last = index + match[0].length;
}
if (last < code.length) tokens.push({ kind: "plain", text: code.slice(last) });
// Split into lines, cutting any token that spans one, like a block comment.
const lines: Token[][] = [[]];
for (const token of tokens) {
token.text.split("\n").forEach((part, i) => {
if (i > 0) lines.push([]);
if (part) lines[lines.length - 1].push({ kind: token.kind, text: part });
});
}
return lines;
}
export function CodeBlock({ files, className }: { files: CodeFile[]; className?: string }) {
const reduceMotion = useReducedMotion();
const id = useId();
const [active, setActive] = useState(0);
const tabs = useRef<(HTMLButtonElement | null)[]>([]);
const highlighted = useMemo(() => files.map((f) => tokenize(f.code.trim(), f.language)), [files]);
return (
<div
className={cn(
"w-[520px] max-w-full overflow-hidden rounded-2xl bg-surface shadow-raised",
className,
)}
>
<div className="flex h-11 items-stretch justify-between border-b border-border pr-2 pl-2">
<LayoutGroup id={id}>
<div role="tablist" aria-label="Files" className="flex min-w-0">
{files.map((file, i) => {
const selected = i === active;
return (
<button
key={file.name}
ref={(el) => {
tabs.current[i] = el;
}}
id={`${id}-tab-${i}`}
type="button"
role="tab"
aria-selected={selected}
aria-controls={`${id}-panel-${i}`}
tabIndex={selected ? 0 : -1}
onClick={() => setActive(i)}
onKeyDown={(e) => {
const step = { ArrowRight: 1, ArrowLeft: -1 }[e.key];
const target =
step !== undefined
? (i + step + files.length) % files.length
: e.key === "Home"
? 0
: e.key === "End"
? files.length - 1
: null;
if (target === null) return;
e.preventDefault();
setActive(target);
tabs.current[target]?.focus();
}}
className={cn(
"group relative touch-manipulation px-3 font-mono text-[13px] outline-hidden transition-[color] duration-150 ease-out select-none focus-visible:outline-2 focus-visible:outline-solid focus-visible:-outline-offset-4 focus-visible:outline-foreground",
selected ? "text-foreground" : "text-muted hover:text-foreground",
)}
>
{/* Press feedback on the label only, so the underline is
never measured mid-scale when the tab changes. */}
<span className="inline-block transition-[scale] duration-150 ease-out group-active:scale-[0.96] motion-reduce:transition-none">
{file.name}
</span>
{selected && (
<motion.span
layoutId="code-tab-underline"
transition={
reduceMotion ? { duration: 0 } : { type: "spring", duration: 0.3, bounce: 0 }
}
// Sits on the header's border so the two read as one line.
className="absolute inset-x-3 -bottom-px h-0.5 rounded-full bg-foreground"
/>
)}
</button>
);
})}
</div>
</LayoutGroup>
<div className="flex items-center">
<CopyCode value={files[active].code.trim()} />
</div>
</div>
{/* Every file keeps its own scroll box, stacked in one fixed-height
cell, so switching is a true crossfade and each file remembers
where you had scrolled to. */}
<div className="relative h-[280px]">
{files.map((file, i) => {
const selected = i === active;
return (
<div
key={file.name}
id={`${id}-panel-${i}`}
role="tabpanel"
aria-labelledby={`${id}-tab-${i}`}
tabIndex={selected ? 0 : -1}
inert={!selected}
className={cn(
"absolute inset-0 overflow-auto overscroll-contain outline-hidden focus-visible:outline-2 focus-visible:outline-solid focus-visible:-outline-offset-2 focus-visible:outline-foreground",
"transition-[opacity,filter] ease-out motion-reduce:transition-[opacity]",
selected
? "opacity-100 blur-[0px] duration-200"
: "opacity-0 blur-[4px] duration-150 motion-reduce:blur-[0px]",
)}
>
<pre className="min-w-max py-3 font-mono text-[14px] leading-6">
<code>
{highlighted[i].map((line, n) => (
<div key={n} className="flex">
{/* Sticky, so numbers stay put while long lines scroll
sideways underneath them. */}
<span
aria-hidden
className="sticky left-0 w-11 shrink-0 bg-surface pr-4 text-right text-muted/60 tabular-nums select-none"
>
{n + 1}
</span>
<span className="pr-5 whitespace-pre text-foreground">
{line.map((token, t) => (
<span key={t} style={{ color: COLORS[token.kind] }}>
{token.text}
</span>
))}
{/* Keeps empty lines one row tall. */}
{line.length === 0 && " "}
</span>
</div>
))}
</code>
</pre>
</div>
);
})}
</div>
</div>
);
}
type Status = "idle" | "copied" | "failed";
function CopyCode({ value }: { value: string }) {
const reduceMotion = useReducedMotion();
const [status, setStatus] = useState<Status>("idle");
const timer = useRef<ReturnType<typeof setTimeout>>(undefined);
const attempt = useRef(0);
useEffect(() => () => clearTimeout(timer.current), []);
const show = (next: Status) => {
setStatus(next);
clearTimeout(timer.current);
// Long enough to read the label, short enough to copy again soon.
timer.current = setTimeout(() => setStatus("idle"), 1600);
};
const copy = async () => {
const id = ++attempt.current;
// Confirms on press; the write is near instant and waiting for it makes
// the click feel ignored.
show("copied");
try {
if (!navigator.clipboard) throw new Error("Clipboard unavailable");
await navigator.clipboard.writeText(value);
} catch {
// A newer click has already taken over the label.
if (id === attempt.current) show("failed");
}
};
const labels: Record<Status, string> = { idle: "Copy", copied: "Copied", failed: "Failed" };
return (
<button
type="button"
onClick={copy}
aria-label="Copy code"
className={cn(
"relative flex h-8 touch-manipulation items-center gap-1.5 rounded-lg px-2.5 text-[13px] text-muted outline-hidden transition-[scale,color,background-color] duration-150 ease-out select-none hover:bg-background hover:text-foreground focus-visible:outline-2 focus-visible:outline-solid focus-visible:outline-foreground active:scale-[0.96] motion-reduce:transition-[color,background-color]",
// Grows the hit area to 40px tall without growing the button.
"after:absolute after:-inset-1",
status !== "idle" && "text-foreground",
)}
>
<span className="grid" aria-hidden>
<Icon visible={status !== "copied"} reduceMotion={reduceMotion}>
<rect x="5.25" y="5.25" width="8" height="8" rx="1.75" />
<path d="M10.75 5.25V4.5a1.75 1.75 0 0 0-1.75-1.75H4.5A1.75 1.75 0 0 0 2.75 4.5V9a1.75 1.75 0 0 0 1.75 1.75h.75" />
</Icon>
<Icon visible={status === "copied"} reduceMotion={reduceMotion}>
<path d="m3.5 8.5 3 3 6-7" />
</Icon>
</span>
{/* All three labels share one cell, so the button keeps the width of
the longest and never jumps. */}
<span className="grid" aria-hidden>
{(Object.keys(labels) as Status[]).map((key) => (
<span
key={key}
className={cn(
"col-start-1 row-start-1 transition-[opacity,filter] ease-out",
key === status ? "opacity-100 blur-[0px] duration-200" : "opacity-0 blur-[4px] duration-150",
)}
>
{labels[key]}
</span>
))}
</span>
<span className="sr-only" aria-live="polite">
{status === "copied" ? "Copied" : status === "failed" ? "Couldn't copy" : ""}
</span>
</button>
);
}
function Icon({
visible,
reduceMotion,
children,
}: {
visible: boolean;
reduceMotion: boolean | null;
children: React.ReactNode;
}) {
const hidden = reduceMotion ? { opacity: 0 } : { scale: 0.25, opacity: 0, filter: "blur(4px)" };
return (
<motion.svg
viewBox="0 0 16 16"
className="col-start-1 row-start-1 size-4"
fill="none"
stroke="currentColor"
strokeWidth={1.5}
strokeLinecap="round"
strokeLinejoin="round"
initial={false}
animate={visible ? { scale: 1, opacity: 1, filter: "blur(0px)" } : hidden}
transition={{ type: "spring", duration: 0.3, bounce: 0 }}
>
{children}
</motion.svg>
);
}
const FILES: CodeFile[] = [
{
name: "button.tsx",
language: "tsx",
code: `
import { cn } from "@/lib/cn";
type ButtonProps = React.ComponentProps<"button">;
// Scales to 0.96 on press, so the click feels physical.
export function Button({ className, ...props }: ButtonProps) {
return (
<button
className={cn("h-10 rounded-full px-4 transition-[scale] duration-150 active:scale-[0.96]", className)}
{...props}
/>
);
}
export default function Demo() {
return <Button onClick={() => console.log(42)}>Save</Button>;
}
`,
},
{
name: "styles.css",
language: "css",
code: `
/* Strong ease-out: responds at once, then settles. */
.button {
height: 40px;
padding: 0 16px;
border-radius: 999px;
transition: scale 150ms cubic-bezier(0.23, 1, 0.32, 1), background-color 150ms ease;
}
.button:active {
scale: 0.96;
}
@media (prefers-reduced-motion: reduce) {
.button {
transition: background-color 150ms ease;
}
}
`,
},
];
export default function CodeBlockDemo() {
return <CodeBlock files={FILES} />;
}