Repository navigation
Expand file tree
/
Copy pathCopyButton.tsx
More file actions
74 lines (64 loc) · 1.97 KB
/
Copy pathCopyButton.tsx
File metadata and controls
74 lines (64 loc) · 1.97 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
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*/
import {useEffect, useState} from 'react';
import {useSandpack} from '@codesandbox/sandpack-react/unstyled';
import {IconCopy} from '../../Icon/IconCopy';
type CopyStatus = 'idle' | 'copied' | 'error';
export function CopyButton() {
const {sandpack} = useSandpack();
const [copyStatus, setCopyStatus] = useState<CopyStatus>('idle');
useEffect(() => {
if (copyStatus === 'idle') {
return;
}
const timeout = setTimeout(() => setCopyStatus('idle'), 2000);
return () => clearTimeout(timeout);
}, [copyStatus]);
async function handleCopy() {
const code = sandpack.files[sandpack.activeFile]?.code;
if (typeof code !== 'string') {
console.error('Unable to copy the active sandbox file: no code found.');
setCopyStatus('error');
return;
}
try {
await navigator.clipboard.writeText(code);
setCopyStatus('copied');
} catch (error) {
console.error('Failed to copy the active sandbox file.', error);
setCopyStatus('error');
}
}
const statusText =
copyStatus === 'copied'
? 'Copied!'
: copyStatus === 'error'
? 'Copy failed'
: 'Copy';
const accessibleLabel =
copyStatus === 'copied'
? 'Code copied'
: copyStatus === 'error'
? 'Copy failed'
: 'Copy active file';
return (
<button
aria-label={accessibleLabel}
className="text-sm text-primary dark:text-primary-dark inline-flex items-center hover:text-link duration-100 ease-in transition mx-1"
onClick={handleCopy}
title={accessibleLabel}
type="button">
<IconCopy className="inline mx-1 relative" />
<span className="hidden md:block" aria-live="polite">
{statusText}
</span>
</button>
);
}