-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCodeBlock.js
73 lines (71 loc) · 1.84 KB
/
CodeBlock.js
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
import React from 'react';
import Paper from '@mui/material/Paper';
import Box from '@mui/material/Box';
import Icon from '@mui/material/Icon';
export default function CodeBlock({
text = 'npx create-next-app --example https://github.com/zesty-io/nextjs-starter',
fontSize = '16px',
bgcolor = 'black',
border = 'none',
color = '#fff',
}) {
const [showCopy, setShowCopy] = React.useState(false);
const [copyWords, setCopyWords] = React.useState('Click to Copy');
const [icon, setIcon] = React.useState('content_copy');
return (
<Paper
onMouseEnter={() => setShowCopy(true)}
onMouseLeave={() => {
setShowCopy(false);
setCopyWords('Click to Copy');
setIcon('content_copy');
}}
onClick={() => {
navigator.clipboard.writeText(text);
setCopyWords('Copied!');
setIcon('check_circle');
dataLayer.push({ event: 'codeLineCopied', value: '1' });
}}
sx={{
textAlign: 'center',
height: 60,
lineHeight: '60px',
elevation: 2,
cursor: 'pointer',
position: 'relative',
bgcolor,
color,
fontSize,
border,
overflow: 'hidden',
}}
>
{text}
{showCopy && (
<Box
sx={{
height: '100%',
position: 'absolute',
left: '0px',
top: '-10px',
padding: '0 10px',
borderRadius: '5px',
top: '0px',
zIndex: '10',
display: 'flex',
alignItems: 'center',
overflow: 'hidden',
background: bgcolor,
color,
}}
color="info"
>
<Icon>{icon}</Icon>
<span style={{ marginLeft: '5px', fontSize: '12px' }}>
{copyWords}
</span>
</Box>
)}
</Paper>
);
}