File tree Expand file tree Collapse file tree 1 file changed +22
-5
lines changed Expand file tree Collapse file tree 1 file changed +22
-5
lines changed Original file line number Diff line number Diff line change 11import { 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" ;
33import CopyToClipboard from "./CopyToClipboard" ;
4+ import { useEffect , useState } from "react" ;
45
56type Props = {
67 language : string ;
78 code : string [ ] ;
89} ;
910
1011const 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 ) ;
You can’t perform that action at this time.
0 commit comments