litellm/ui/litellm-dashboard/src/components/CodeBlock.tsx
Yuneng Jiang 03f64200c2
fix(ui): stop CodeBlock painting a hardcoded light background in dark mode
The customStyle background only reaches the <pre>, so the prism theme's own
background stayed on the inner <code>. In dark mode that left a white card
with a dark box hugging every line. Let the block inherit the themed surface
from its wrapper instead.
2026-08-29 10:15:05 -07:00

50 lines
1.5 KiB
TypeScript

import { useState } from "react";
import { CheckIcon, ClipboardIcon } from "lucide-react";
import { Prism as SyntaxHighlighter } from "react-syntax-highlighter";
import { oneLight } from "react-syntax-highlighter/dist/esm/styles/prism";
import { useSyntaxTheme } from "@/hooks/useSyntaxTheme";
interface CodeBlockProps {
code: string;
language: string;
}
const CodeBlock = ({ code, language }: CodeBlockProps) => {
const syntaxTheme = useSyntaxTheme(oneLight);
const [copied, setCopied] = useState(false);
const copyToClipboard = () => {
navigator.clipboard.writeText(code);
setCopied(true);
setTimeout(() => setCopied(false), 2000);
};
return (
<div className="relative rounded-lg border border-border bg-muted overflow-hidden">
<button
onClick={copyToClipboard}
className="absolute top-3 right-3 p-2 rounded-md border border-border bg-background text-muted-foreground hover:bg-accent hover:text-foreground z-raised"
aria-label="Copy code"
>
{copied ? <CheckIcon size={16} /> : <ClipboardIcon size={16} />}
</button>
<SyntaxHighlighter
language={language}
style={syntaxTheme}
customStyle={{
margin: 0,
padding: "1.5rem",
borderRadius: "0.5rem",
fontSize: "0.9rem",
background: "transparent",
}}
codeTagProps={{ style: { background: "transparent" } }}
showLineNumbers
>
{code}
</SyntaxHighlighter>
</div>
);
};
export default CodeBlock;