Repository navigation
Expand file tree
/
Copy pathcode-block.tsx
More file actions
153 lines (142 loc) · 6.35 KB
/
Copy pathcode-block.tsx
File metadata and controls
153 lines (142 loc) · 6.35 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
import { Check, Copy, WrapText } from 'lucide-react';
import { useEffect, useRef, useState } from 'react';
import { toast } from 'sonner';
import { useClipboard } from '@/hooks/use-clipboard';
import { withCertificateHashes } from '@/lib/certificate-hashes';
import { cn } from '@/lib/utils';
export function CodeBlock({
html,
fullHtml,
language,
filename,
isCopyable = true,
}: {
html: string;
fullHtml?: string;
language: string | null;
filename: string | null;
isCopyable?: boolean;
}) {
const codeRef = useRef<HTMLPreElement>(null);
const [, copy] = useClipboard();
const [isCopied, setIsCopied] = useState(false);
const [isWrapped, setIsWrapped] = useState(false);
useEffect(() => {
if (!isCopied) {
return;
}
const timeout = setTimeout(() => setIsCopied(false), 2000);
return () => clearTimeout(timeout);
}, [isCopied]);
const copyCode = async () => {
if (!codeRef.current) {
return;
}
let code = codeRef.current.textContent ?? '';
if (fullHtml !== undefined) {
// The server escapes the code while highlighting it, and a template runs nothing.
const template = document.createElement('template');
template.innerHTML = fullHtml;
code = template.content.textContent ?? '';
}
if (await copy(code)) {
setIsCopied(true);
return;
}
setIsCopied(false);
toast.error(
<span className="font-mono text-[13px]">
Could not copy code. Select it and copy manually.
</span>,
);
};
return (
<div
className={cn(
'relative isolate my-2 rounded-md border border-transparent before:pointer-events-none before:absolute before:-inset-px before:-z-10 before:rounded-md before:border before:border-border before:bg-muted/60',
fullHtml !== undefined &&
fullHtml !== html &&
'before:mask-b-from-[calc(100%-3rem)] before:mask-b-to-100%',
)}
>
{(filename || language || isCopyable) && (
<div className="flex h-8 items-center justify-between gap-2 border-b pr-1 pl-3">
<span
className="min-w-0 truncate font-mono text-[11px] text-muted-foreground select-none"
title={filename ?? undefined}
>
{filename ?? language}
</span>
{isCopyable && (
<div className="flex shrink-0 items-center gap-0.5">
<button
type="button"
onClick={() => setIsWrapped(!isWrapped)}
aria-label="Wrap lines"
aria-pressed={isWrapped}
title="Wrap lines"
className={cn(
'grid size-6 place-items-center rounded-md text-muted-foreground transition-colors hover:bg-muted hover:text-foreground focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-ring',
isWrapped && 'bg-muted text-foreground',
)}
>
<WrapText className="size-3.5" aria-hidden />
</button>
<button
type="button"
onClick={copyCode}
aria-label="Copy code"
title="Copy code"
className={cn(
'relative grid size-6 place-items-center rounded-md text-muted-foreground transition-colors hover:bg-muted hover:text-foreground focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-ring',
isCopied && 'text-foreground',
)}
>
<Copy
className={cn(
'absolute size-3.5 transition-[opacity,scale,filter] duration-200 motion-reduce:transition-none',
isCopied &&
'scale-50 opacity-0 blur-[2px]',
)}
aria-hidden
/>
<Check
className={cn(
'absolute size-3.5 transition-[opacity,scale,filter] duration-200 motion-reduce:transition-none',
!isCopied &&
'scale-50 opacity-0 blur-[2px]',
)}
aria-hidden
/>
</button>
<span role="status" className="sr-only">
{isCopied ? 'Code copied to clipboard' : ''}
</span>
</div>
)}
</div>
)}
<div
className={isWrapped ? 'overflow-x-hidden' : 'overflow-x-auto'}
>
{/* The server escapes the code while highlighting it. */}
<pre
ref={codeRef}
tabIndex={isCopyable ? undefined : -1}
className={cn(
'px-3 py-2.5 font-mono text-[13px] leading-normal',
isWrapped
? 'wrap-anywhere whitespace-pre-wrap'
: 'w-max min-w-full wrap-normal whitespace-pre',
fullHtml !== undefined &&
fullHtml !== html &&
'mask-b-from-[calc(100%-3rem)] mask-b-to-100%',
)}
dangerouslySetInnerHTML={{
__html: withCertificateHashes(html),
}}
/>
</div>
</div>
);
}