Last active
March 29, 2022 02:21
-
-
Save ljaviertovar/32217406cbbdf0fc2a06b5611c78b895 to your computer and use it in GitHub Desktop.
Component to render blocks of code with markdown
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useState } from "react" | |
import { CopyToClipboard } from "react-copy-to-clipboard" | |
import { PrismLight as SyntaxHighlighter } from 'react-syntax-highlighter' | |
import jsx from 'react-syntax-highlighter/dist/esm/languages/prism/jsx' | |
import { materialDark, materialLight } from 'react-syntax-highlighter/dist/esm/styles/prism' | |
import { CopyIcon, PasteIcon, SunIcon, MoonIcon } from "../assets/icons" | |
import '../styles/Code.css' | |
const Code = ({ children, language, isDark, setIsDark }) => { | |
const [isCopied, setIsCopied] = useState(false) | |
SyntaxHighlighter.registerLanguage('jsx', jsx); | |
const setCopied = () => { | |
setIsCopied(true) | |
setTimeout(() => { setIsCopied(false) }, 1000); | |
} | |
return ( | |
<div className="code"> | |
<div className="code__icons"> | |
<button onClick={() => setIsDark(!isDark)}> | |
{isDark ? <MoonIcon /> : <SunIcon />} | |
</button> | |
<CopyToClipboard text={children}> | |
<button onClick={() => setCopied()}> | |
{isCopied | |
? <span title="Copied!"><PasteIcon /></span> | |
: <span title="Copy to Clipboard"><CopyIcon /></span> | |
} | |
</button> | |
</CopyToClipboard> | |
</div> | |
<SyntaxHighlighter language={language} style={isDark ? materialDark : materialLight}> | |
{children} | |
</SyntaxHighlighter> | |
</div> | |
) | |
} | |
export default Code |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment