This repository has been archived by the owner on Dec 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Code.js
149 lines (138 loc) · 3.83 KB
/
Code.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
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
import * as React from 'react'
import PropTypes from 'prop-types'
import { rgba } from 'polished'
import Highlight, { defaultProps } from 'prism-react-renderer'
import { CopyToClipboard } from 'react-copy-to-clipboard'
import { useTheme } from '@emotion/react'
import { Button } from 'chaoskit/src/components'
import useUpdateEffect from 'react-use/lib/useUpdateEffect'
import Icon from './Icon'
const CodeHeader = ({ filename, language, codeString, ...rest }) => {
const theme = useTheme()
const [isCopied, setCopied] = React.useState(false)
// Reset icon after 3 seconds
useUpdateEffect(() => {
if (isCopied) {
setTimeout(() => setCopied(false), 3000)
}
}, [isCopied])
return (
<div
className="u-contrast"
css={{
display: 'grid',
alignItems: 'center',
gap: theme.space.base,
gridTemplateColumns: filename ? '1fr auto auto' : '1fr auto',
background: theme.color.dark.base,
fontSize: theme.fontSize.small,
paddingTop: theme.space.small,
paddingBottom: theme.space.small,
paddingLeft: theme.space.base,
paddingRight: theme.space.base,
borderTopLeftRadius: theme.borderRadius.base,
borderTopRightRadius: theme.borderRadius.base,
boxShadow: `inset 0 -1px 0 0 ${rgba(theme.color.light.base, 0.15)}`,
'+ .prism-code': {
marginTop: 0,
borderTopLeftRadius: 0,
borderTopRightRadius: 0,
},
}}
{...rest}
>
{filename && (
<div
css={{
fontFamily: theme.fontFamily.code,
}}
>
{filename}
</div>
)}
{language && (
<div
css={{
fontWeight: theme.fontWeight.bold,
textTransform: 'uppercase',
textAlign: 'right',
}}
>
{language}
</div>
)}
<div>
<CopyToClipboard text={codeString} onCopy={() => setCopied(true)}>
<Button
css={{
top: 'auto', // Reset default inline offset
}}
title="Copy"
type="reset"
>
{isCopied ? <Icon icon="check" /> : <Icon icon="copy" />}
</Button>
</CopyToClipboard>
</div>
</div>
)
}
CodeHeader.propTypes = {
filename: PropTypes.string,
language: PropTypes.string,
codeString: PropTypes.string,
}
const Code = ({ codeString, language, filename }) => {
const theme = useTheme()
return (
<Highlight
{...defaultProps}
theme={undefined}
code={codeString}
language={language}
>
{({ className, tokens, getLineProps, getTokenProps }) => (
<div
css={{
boxShadow: theme.boxShadow.large,
marginTop: theme.space.base,
marginBottom: theme.space.base,
}}
>
{(filename || language) && (
<CodeHeader
filename={filename}
language={language}
codeString={codeString}
/>
)}
<pre className={className}>
{tokens.map((line, i) => (
<div {...getLineProps({ line, key: i })}>
<span
css={{
opacity: theme.opacity.base,
width: '2em',
userSelect: 'none',
display: 'inline-block',
}}
>
{i + 1}
</span>
{line.map((token, key) => (
<span {...getTokenProps({ token, key })} />
))}
</div>
))}
</pre>
</div>
)}
</Highlight>
)
}
Code.propTypes = {
codeString: PropTypes.string.isRequired,
language: PropTypes.string.isRequired,
filename: PropTypes.string,
}
export default Code