This repository has been archived by the owner on Feb 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 132
/
CodeExample.js
334 lines (289 loc) · 7.91 KB
/
CodeExample.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
import React from "react";
import styled from "styled-components";
import PropTypes from "prop-types";
import { CopyToClipboard } from "react-copy-to-clipboard";
import { BasicButton } from "basics/Buttons";
import { CopyIcon, ExternalLinkIcon } from "basics/Icons";
import { Code } from "basics/Text";
import { PALETTE, FONT_FAMILY, FONT_WEIGHT } from "constants/styles";
import { getCookie } from "helpers/getCookie";
import { extractStringChildren } from "helpers/extractStringChildren";
import { useHighlighting } from "helpers/useHighlighting";
import { Link } from "basics/Links";
import { Select } from "basics/Inputs";
import { Tooltip } from "components/Tooltip";
const CODE_LANG_CHANGE_EVENT = "codeLangChange";
const CODE_LANGS = {
bash: "bash",
cpp: "C++",
curl: "cURL",
go: "Go",
html: "html",
java: "Java",
js: "JavaScript",
json: "JSON",
python: "Python",
scss: "SCSS",
toml: "TOML",
ts: "TypeScript",
tsx: "TSX",
yaml: "YAML",
};
const LangSelect = styled(Select)`
width: 100%;
display: inline-block;
margin: 0;
select {
display: inline-block;
padding: 0.5rem;
font-size: 0.75rem;
border: none;
background: transparent;
color: ${PALETTE.white};
font-weight: 500;
}
`;
const LangOptionEl = styled.div`
font-family: ${FONT_FAMILY.monospace};
font-weight: 500;
font-size: 0.75rem;
line-height: 1.15;
`;
const OptionsContainerEl = styled.div`
display: flex;
position: relative;
align-items: center;
`;
const MethodContentEl = styled.div`
position: relative;
overflow: auto;
border-radius: 4px;
`;
const ContentEl = styled.div`
padding: 1rem;
overflow: auto;
-ms-overflow-style: none;
&::-webkit-scrollbar {
display: none;
}
`;
const CodeExampleEl = styled.div`
& ${Code} {
color: ${PALETTE.light};
}
display: flex;
flex-direction: column;
margin-top: 0.5rem;
margin-bottom: 2rem;
div {
background: #292d3e;
}
thead {
display: none;
}
td {
border: none;
padding: 0.5rem 0;
&:first-child {
color: ${PALETTE.purple};
font-weight: ${FONT_WEIGHT.bold};
padding-right: 1rem;
}
}
`;
const CopyButtonEl = styled(BasicButton).attrs({
"aria-label": "Copy snippet",
})`
padding: 0.75rem 1rem;
`;
const TitleEl = styled.div`
display: flex;
padding-left: 1rem;
justify-content: ${(props) =>
props.hasTitle ? "space-between" : "flex-end"};
position: relative;
align-items: center;
color: ${PALETTE.white};
border-bottom: 1px solid rgba(255, 255, 255, 0.1);
font-size: 0.875rem;
font-weight: ${FONT_WEIGHT.bold};
`;
const DividerEl = styled.span`
display: inline-block;
width: 0.0625rem;
height: 1.5rem;
margin-left: 1rem;
background-color: rgba(255, 255, 255, 0.1);
`;
const mdxJsxToString = (jsx) => {
const { props } = jsx;
const { children } = props;
let str;
if (children.props.children.length > 0) {
if (typeof children.props.children === "string") {
str = children.props.children;
return str;
}
str = children.props.children.map((codeSnippet) =>
extractStringChildren(codeSnippet),
);
}
return str.join("");
};
const CodeSnippet = ({ codeSnippets, title, href }) => {
const SelectRef = React.createRef(null);
const availableLangs = React.Children.map(
codeSnippets,
(eachLang) => eachLang.props["data-language"],
);
const [activeLang, setActiveLang] = React.useState("");
const [isCopied, setCopy] = React.useState(false);
const [isHovered, setHover] = React.useState(false);
const contentsRef = React.useRef();
useHighlighting(contentsRef);
const codeSnippetsArr = React.Children.toArray(codeSnippets);
const selectedSnippet =
codeSnippetsArr.find(
(snippet) => snippet.props["data-language"] === activeLang,
) || codeSnippetsArr[0];
const SelectedSnippetStr = mdxJsxToString(selectedSnippet);
const onChange = React.useCallback((e) => {
const { value } = e.target;
document.cookie = `lang=${value}`;
document.dispatchEvent(
new CustomEvent(CODE_LANG_CHANGE_EVENT, {
detail: {
lang: value,
},
}),
);
}, []);
React.useEffect(() => {
const onLangChangeEvent = (e) => {
setActiveLang(e.detail.lang);
};
document.addEventListener(CODE_LANG_CHANGE_EVENT, onLangChangeEvent);
return document.removeEventListener(
CODE_LANG_CHANGE_EVENT,
onLangChangeEvent,
true,
);
}, []);
React.useEffect(() => {
const setHoverFalse = () => {
if (isHovered) setHover(false);
};
/* Setting 'isHovered' to false while scrolling is necessary to
prevent a tooltip to be displayed and its position to be scrolled
together when a user scrolls while hovering on an icon */
window.addEventListener("scroll", setHoverFalse);
return () => {
window.removeEventListener("scroll", setHoverFalse);
};
}, [isHovered]);
React.useEffect(() => {
const langCookie = getCookie("lang");
let timer;
if (langCookie) {
setActiveLang(langCookie);
} else {
setActiveLang(availableLangs[0]);
}
if (isCopied) {
timer = setTimeout(() => {
setCopy(false);
}, 6000);
}
return () => {
if (timer) {
clearTimeout(timer);
}
};
}, [availableLangs, isCopied]);
return (
<MethodContentEl>
<TitleEl hasTitle={title}>
{title}
<OptionsContainerEl>
<LangOptionEl>
{availableLangs.length > 1 ? (
<LangSelect
ref={SelectRef}
onChange={onChange}
name="langSelect"
label=""
value={activeLang}
>
{availableLangs.map((langValue) => (
<option key={langValue} value={langValue}>
{CODE_LANGS[langValue]}
</option>
))}
</LangSelect>
) : (
CODE_LANGS[availableLangs[0]]
)}
</LangOptionEl>
<DividerEl />
<Tooltip message={isCopied ? "Copied" : "Click to copy"}>
<CopyToClipboard
text={SelectedSnippetStr}
onCopy={() => !isCopied && setCopy(true)}
>
<CopyButtonEl>
<CopyIcon />
</CopyButtonEl>
</CopyToClipboard>
</Tooltip>
{href && (
<Link href={href} newTab>
<ExternalLinkIcon />
</Link>
)}
</OptionsContainerEl>
</TitleEl>
<ContentEl className="line-numbers" ref={contentsRef}>
{selectedSnippet}
</ContentEl>
</MethodContentEl>
);
};
CodeSnippet.propTypes = {
codeSnippets: PropTypes.node.isRequired,
title: PropTypes.node,
href: PropTypes.string,
};
/**
* Note: This exports a React component instead of a styled-component.
* [Design Mockup](https://zpl.io/V1DGqJ5)
*/
export const CodeExample = React.forwardRef(function CodeExample(
{ title, children, href, ...props },
ref,
) {
const codeSnippets = React.useMemo(
() =>
React.Children.map(children, (child) => {
// The language is present in the css className under `language-xx`.
// Swipe it, then put it on `data-language` like `CodeSnippet` expects.
// If the code snippet has no language specified, there won't be a
// className at all.
const { className = "" } = child.props.children.props;
const langClass =
className.split(" ").find((x) => x.startsWith("language-")) || "";
const lang = langClass.split("-")[1];
return React.cloneElement(child, { "data-language": lang });
}),
[children],
);
return (
<CodeExampleEl ref={ref} {...props}>
<CodeSnippet codeSnippets={codeSnippets} title={title} href={href} />
</CodeExampleEl>
);
});
CodeExample.propTypes = {
title: PropTypes.node,
children: PropTypes.node.isRequired,
href: PropTypes.string,
};