From e30336c00e2b682420d3740776ddff4461bb95e0 Mon Sep 17 00:00:00 2001 From: Danil Shishkevich <44939683+shishkevichd@users.noreply.github.com> Date: Sat, 25 Mar 2023 17:39:31 +0700 Subject: [PATCH] feat: codeblock styling like chatgpt (#132) --- components/Markdown/CodeBlock.tsx | 24 +++++++++++++----------- styles/globals.css | 4 ++++ 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/components/Markdown/CodeBlock.tsx b/components/Markdown/CodeBlock.tsx index a0e9a5c..d7a5652 100644 --- a/components/Markdown/CodeBlock.tsx +++ b/components/Markdown/CodeBlock.tsx @@ -1,5 +1,5 @@ import { generateRandomString, programmingLanguages } from "@/utils/app/codeblock"; -import { IconDownload } from "@tabler/icons-react"; +import { IconCheck, IconClipboard, IconDownload } from "@tabler/icons-react"; import { FC, useState } from "react"; import { Prism as SyntaxHighlighter } from "react-syntax-highlighter"; import { oneDark, oneLight } from "react-syntax-highlighter/dist/cjs/styles/prism"; @@ -11,7 +11,7 @@ interface Props { } export const CodeBlock: FC = ({ language, value, lightMode }) => { - const [buttonText, setButtonText] = useState("Copy code"); + const [isCopied, setIsCopied] = useState(false); const copyToClipboard = () => { if (!navigator.clipboard || !navigator.clipboard.writeText) { @@ -19,10 +19,10 @@ export const CodeBlock: FC = ({ language, value, lightMode }) => { } navigator.clipboard.writeText(value).then(() => { - setButtonText("Copied!"); + setIsCopied(true); setTimeout(() => { - setButtonText("Copy code"); + setIsCopied(false); }, 2000); }); }; @@ -48,21 +48,22 @@ export const CodeBlock: FC = ({ language, value, lightMode }) => { URL.revokeObjectURL(url); }; return ( -
-
- {language} +
+
+ {language}
@@ -70,6 +71,7 @@ export const CodeBlock: FC = ({ language, value, lightMode }) => { {value} diff --git a/styles/globals.css b/styles/globals.css index 4dfeeef..b45ed1c 100644 --- a/styles/globals.css +++ b/styles/globals.css @@ -31,3 +31,7 @@ html { background: #202123; } + +pre:has(div.codeblock) { + padding: 0; +} \ No newline at end of file