Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 4e9a0ae

Browse files
fix: fix the code theme
1 parent d91b213 commit 4e9a0ae

File tree

1 file changed

+22
-5
lines changed

1 file changed

+22
-5
lines changed

‎src/components/CodePreview.tsx‎

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,42 @@
11
import { Prism as SyntaxHighlighter } from "react-syntax-highlighter";
2-
import { oneDark } from "react-syntax-highlighter/dist/esm/styles/prism";
2+
import { oneDark,oneLight } from "react-syntax-highlighter/dist/esm/styles/prism";
33
import CopyToClipboard from "./CopyToClipboard";
4+
import { useEffect, useState } from "react";
45

56
type Props = {
67
language: string;
78
code: string[];
89
};
910

1011
const CodePreview = ({ language = "markdown", code }: Props) => {
11-
const codeString = code.join("\n");
12+
const [theme, setTheme] = useState<"dark" | "light">("dark");
13+
14+
useEffect(() => {
15+
const handleThemeChange = () => {
16+
const newTheme = document.documentElement.getAttribute("data-theme") as "dark" | "light";
17+
setTheme(newTheme || "dark");
18+
};
19+
20+
handleThemeChange();
21+
const observer = new MutationObserver(handleThemeChange);
22+
observer.observe(document.documentElement, {
23+
attributes: true,
24+
attributeFilter: ["data-theme"],
25+
});
26+
27+
return () => observer.disconnect();
28+
}, []);
1229

1330
return (
1431
<div className="code-preview">
15-
<CopyToClipboard text={codeString} className="modal__copy" />
32+
<CopyToClipboard text={code.join("\n")} className="modal__copy" />
1633
<SyntaxHighlighter
1734
language={language}
18-
style={oneDark}
35+
style={theme==="dark" ? oneDark : oneLight}
1936
wrapLines={true}
2037
customStyle={{ margin: "0", maxHeight: "20rem" }}
2138
>
22-
{codeString}
39+
{code.join("\n")}
2340
</SyntaxHighlighter>
2441
</div>
2542
);

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /