Skip to content

Open-source Grok Bot alternative for named AI teammates. Local-first desktop, web, and mobile clients with per-bot tools, instructions, and memory. Independent fork of T3 Code.

Add copy button to chat markdown code blocks

Julius Marmingee8896fe02d71
diff --git a/apps/web/src/components/ChatMarkdown.tsx b/apps/web/src/components/ChatMarkdown.tsx
index 55ee44838..ae82b805d 100644
--- a/apps/web/src/components/ChatMarkdown.tsx
+++ b/apps/web/src/components/ChatMarkdown.tsx
@@ -1,10 +1,13 @@
import { getSharedHighlighter } from "@pierre/diffs";
+import { CheckIcon, CopyIcon } from "lucide-react";
import {
Children,
isValidElement,
+ useCallback,
memo,
useEffect,
useMemo,
+ useRef,
useState,
type ReactNode,
} from "react";
@@ -159,6 +162,54 @@ interface ShikiCodeBlockProps {
fallback: ReactNode;
}
+function MarkdownCodeBlock({ code, children }: { code: string; children: ReactNode }) {
+ const [copied, setCopied] = useState(false);
+ const copiedTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null);
+ const handleCopy = useCallback(() => {
+ if (typeof navigator === "undefined" || navigator.clipboard == null) {
+ return;
+ }
+ void navigator.clipboard
+ .writeText(code)
+ .then(() => {
+ if (copiedTimerRef.current != null) {
+ clearTimeout(copiedTimerRef.current);
+ }
+ setCopied(true);
+ copiedTimerRef.current = setTimeout(() => {
+ setCopied(false);
+ copiedTimerRef.current = null;
+ }, 1200);
+ })
+ .catch(() => undefined);
+ }, [code]);
+
+ useEffect(
+ () => () => {
+ if (copiedTimerRef.current != null) {
+ clearTimeout(copiedTimerRef.current);
+ copiedTimerRef.current = null;
+ }
+ },
+ [],
+ );
+
+ return (
+ <div className="chat-markdown-codeblock">
+ <button
+ type="button"
+ className="chat-markdown-copy-button"
+ onClick={handleCopy}
+ title={copied ? "Copied" : "Copy code"}
+ aria-label={copied ? "Copied" : "Copy code"}
+ >
+ {copied ? <CheckIcon className="size-3" /> : <CopyIcon className="size-3" />}
+ </button>
+ {children}
+ </div>
+ );
+}
+
function ShikiCodeBlock({ className, code, themeName, isStreaming, fallback }: ShikiCodeBlockProps) {
const language = extractFenceLanguage(className);
const cacheKey = createHighlightCacheKey(code, language, themeName);
@@ -264,13 +315,15 @@ function ChatMarkdown({ text, cwd, isStreaming = false }: ChatMarkdownProps) {
}
return (
- <ShikiCodeBlock
- className={codeBlock.className}
- code={codeBlock.code}
- themeName={diffThemeName}
- isStreaming={isStreaming}
- fallback={<pre {...props}>{children}</pre>}
- />
+ <MarkdownCodeBlock code={codeBlock.code}>
+ <ShikiCodeBlock
+ className={codeBlock.className}
+ code={codeBlock.code}
+ themeName={diffThemeName}
+ isStreaming={isStreaming}
+ fallback={<pre {...props}>{children}</pre>}
+ />
+ </MarkdownCodeBlock>
);
},
}),
diff --git a/apps/web/src/index.css b/apps/web/src/index.css
index 3cdadb01f..e752f8ef6 100644
--- a/apps/web/src/index.css
+++ b/apps/web/src/index.css
@@ -314,6 +314,48 @@ label:has(> select#reasoning-effort) select {
font-size: 0.875rem;
}
+.chat-markdown .chat-markdown-codeblock {
+ position: relative;
+ margin: 0.65rem 0;
+}
+
+.chat-markdown .chat-markdown-codeblock pre {
+ margin: 0;
+}
+
+.chat-markdown .chat-markdown-copy-button {
+ position: absolute;
+ top: 0.5rem;
+ right: 0.5rem;
+ z-index: 1;
+ display: inline-flex;
+ align-items: center;
+ justify-content: center;
+ height: 1.5rem;
+ width: 1.5rem;
+ border-radius: 0.375rem;
+ border: 1px solid var(--border);
+ background: color-mix(in srgb, var(--background) 82%, transparent);
+ color: var(--muted-foreground);
+ opacity: 0;
+ pointer-events: none;
+ transition:
+ opacity 120ms ease,
+ color 120ms ease,
+ border-color 120ms ease;
+}
+
+.chat-markdown .chat-markdown-codeblock:hover .chat-markdown-copy-button,
+.chat-markdown .chat-markdown-codeblock:focus-within .chat-markdown-copy-button {
+ opacity: 1;
+ pointer-events: auto;
+}
+
+.chat-markdown .chat-markdown-copy-button:hover {
+ color: var(--foreground);
+ border-color: color-mix(in srgb, var(--border) 70%, var(--foreground));
+}
+
.chat-markdown .chat-markdown-shiki .shiki {
background: color-mix(in srgb, var(--muted) 78%, var(--background)) !important;
}