-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add button to copy code in markdown (#1845)
Resolves #1814 ![image](https://user-images.githubusercontent.com/54318514/221346522-07b94f93-533f-4370-a8ec-3e5230c96418.png) ![image](https://user-images.githubusercontent.com/54318514/221346515-f1ba9a43-1143-4d4f-82db-18a28a2c5778.png) Not sure if using Toast is a bit too much but I think it fits the style
- Loading branch information
1 parent
61e4b38
commit 188010c
Showing
2 changed files
with
81 additions
and
15 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { Button, Flex, useToast } from "@chakra-ui/react"; | ||
import { Copy, Check } from "lucide-react"; | ||
import { useState, MouseEvent } from "react"; | ||
import { Prism as SyntaxHighlighter } from "react-syntax-highlighter"; | ||
import { oneDark } from "react-syntax-highlighter/dist/cjs/styles/prism"; | ||
|
||
export const RenderedCodeblock = ({ node, inline, className, children, style, ...props }) => { | ||
const match = /language-(\w+)/.exec(className || ""); | ||
const lang = match ? match[1] : ""; | ||
|
||
const [isCopied, setIsCopied] = useState(false); | ||
const toast = useToast(); | ||
|
||
const handleCopyClick = async (event: MouseEvent) => { | ||
event.stopPropagation(); | ||
toast.closeAll(); | ||
|
||
try { | ||
await navigator.clipboard.writeText(String(children)); | ||
|
||
toast({ | ||
title: "Copied to clipboard", | ||
status: "info", | ||
duration: 2000, | ||
isClosable: true, | ||
}); | ||
|
||
setIsCopied(true); | ||
setTimeout(() => setIsCopied(false), 2000); | ||
} catch { | ||
toast({ | ||
title: "Failed to copy", | ||
status: "error", | ||
duration: 2000, | ||
isClosable: true, | ||
}); | ||
} | ||
}; | ||
|
||
return !inline ? ( | ||
<Flex pos="relative" flexDir="row" role="group"> | ||
<SyntaxHighlighter style={oneDark} language={lang} {...props}> | ||
{String(children).replace(/\n$/, "")} | ||
</SyntaxHighlighter> | ||
<Button | ||
onClick={handleCopyClick} | ||
top="0" | ||
pos="absolute" | ||
display="none" | ||
_rtl={{ | ||
left: "0", | ||
transform: "translate(10px,40%)", | ||
}} | ||
_ltr={{ | ||
right: "0", | ||
transform: "translate(-10px,40%)", | ||
}} | ||
_groupHover={{ | ||
display: "flex", | ||
}} | ||
colorScheme={isCopied ? "blue" : "gray"} | ||
> | ||
{isCopied ? <Check /> : <Copy />} | ||
</Button> | ||
</Flex> | ||
) : ( | ||
<code className={className} {...props}> | ||
{children} | ||
</code> | ||
); | ||
}; |
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