forked from mui/material-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodeCopyButton.tsx
More file actions
34 lines (31 loc) · 981 Bytes
/
CodeCopyButton.tsx
File metadata and controls
34 lines (31 loc) · 981 Bytes
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
import useClipboardCopy from './useClipboardCopy';
export interface CodeCopyButtonProps {
code: string;
}
export function CodeCopyButton(props: CodeCopyButtonProps) {
const { code, ...other } = props;
const { copy, isCopied } = useClipboardCopy();
// This component is designed to be wrapped in NoSsr
const macOS =
typeof window !== 'undefined' && window.navigator.platform.toUpperCase().includes('MAC');
const key = macOS ? '⌘' : 'Ctrl + ';
return (
<div className="MuiCode-copy-container">
<button
{...other}
aria-label="Copy the code"
type="button"
className="MuiCode-copy"
onClick={async () => {
// event.stopPropagation();
await copy(code);
}}
>
{isCopied ? 'Copied' : 'Copy'}
<span className="MuiCode-copyKeypress" style={{ opacity: isCopied ? 0 : 1 }}>
<span>(or</span> {key}C<span>)</span>
</span>
</button>
</div>
);
}