-
-
Notifications
You must be signed in to change notification settings - Fork 135
/
Copy pathCodePreview.tsx
63 lines (54 loc) · 1.64 KB
/
CodePreview.tsx
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
import { useEffect, useState } from "react";
import { Prism as SyntaxHighlighter } from "react-syntax-highlighter";
import {
oneDark,
oneLight,
} from "react-syntax-highlighter/dist/esm/styles/prism";
import { slugify } from "@utils/slugify";
import CopyToClipboard from "./CopyToClipboard";
import CopyURLButton from "./CopyURLButton";
type Props = {
extension: string;
languageName: string;
code: string;
};
const CodePreview = ({ extension = "markdown", languageName, code }: Props) => {
const [theme, setTheme] = useState<"dark" | "light">("dark");
useEffect(() => {
const handleThemeChange = () => {
const newTheme = document.documentElement.getAttribute("data-theme") as
| "dark"
| "light";
setTheme(newTheme || "dark");
};
handleThemeChange();
const observer = new MutationObserver(handleThemeChange);
observer.observe(document.documentElement, {
attributes: true,
attributeFilter: ["data-theme"],
});
return () => observer.disconnect();
}, []);
return (
<div className="code-preview">
<div className="code-preview__header">
<p>{slugify(languageName)}</p>
<div className="code-preview__buttons">
<CopyToClipboard text={code} />
<CopyURLButton />
</div>
</div>
<div className="code-preview__body">
<SyntaxHighlighter
language={extension}
style={theme === "dark" ? oneDark : oneLight}
wrapLines={true}
customStyle={{ margin: "0", maxHeight: "22rem" }}
>
{code}
</SyntaxHighlighter>
</div>
</div>
);
};
export default CodePreview;