2026-04-20 00:17:22 +08:00
|
|
|
import { useCallback, useEffect, useState } from "react";
|
2026-04-18 18:51:53 +00:00
|
|
|
import { Check, Copy } from "lucide-react";
|
2026-04-19 06:39:06 +00:00
|
|
|
import { useTranslation } from "react-i18next";
|
2026-04-18 18:51:53 +00:00
|
|
|
import { Prism as SyntaxHighlighter } from "react-syntax-highlighter";
|
|
|
|
|
import {
|
|
|
|
|
oneDark,
|
|
|
|
|
oneLight,
|
|
|
|
|
} from "react-syntax-highlighter/dist/esm/styles/prism";
|
|
|
|
|
|
|
|
|
|
import { cn } from "@/lib/utils";
|
|
|
|
|
|
|
|
|
|
interface CodeBlockProps {
|
|
|
|
|
language?: string;
|
|
|
|
|
code: string;
|
|
|
|
|
className?: string;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-20 00:17:22 +08:00
|
|
|
/** Read dark mode straight from the DOM — stays in sync with Tailwind's `dark:`. */
|
|
|
|
|
function useIsDark() {
|
|
|
|
|
const [isDark, setIsDark] = useState(() =>
|
|
|
|
|
typeof document !== "undefined"
|
|
|
|
|
? document.documentElement.classList.contains("dark")
|
|
|
|
|
: true,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
const el = document.documentElement;
|
|
|
|
|
const observer = new MutationObserver(() => {
|
|
|
|
|
setIsDark(el.classList.contains("dark"));
|
|
|
|
|
});
|
|
|
|
|
observer.observe(el, { attributeFilter: ["class"] });
|
|
|
|
|
return () => observer.disconnect();
|
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
return isDark;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-18 18:51:53 +00:00
|
|
|
export function CodeBlock({ language, code, className }: CodeBlockProps) {
|
2026-04-19 06:39:06 +00:00
|
|
|
const { t } = useTranslation();
|
2026-04-18 18:51:53 +00:00
|
|
|
const [copied, setCopied] = useState(false);
|
2026-04-20 00:17:22 +08:00
|
|
|
const isDark = useIsDark();
|
2026-04-18 18:51:53 +00:00
|
|
|
|
2026-04-20 00:17:22 +08:00
|
|
|
const onCopy = useCallback(() => {
|
2026-04-18 18:51:53 +00:00
|
|
|
if (!navigator.clipboard) return;
|
|
|
|
|
navigator.clipboard.writeText(code).then(() => {
|
|
|
|
|
setCopied(true);
|
|
|
|
|
setTimeout(() => setCopied(false), 1_500);
|
|
|
|
|
});
|
2026-04-20 00:17:22 +08:00
|
|
|
}, [code]);
|
2026-04-18 18:51:53 +00:00
|
|
|
|
|
|
|
|
return (
|
2026-04-20 00:17:22 +08:00
|
|
|
<div
|
|
|
|
|
className={cn(
|
|
|
|
|
"overflow-hidden rounded-lg border",
|
|
|
|
|
isDark ? "border-white/10" : "border-black/10",
|
|
|
|
|
className,
|
|
|
|
|
)}
|
|
|
|
|
>
|
|
|
|
|
<div
|
|
|
|
|
className={cn(
|
|
|
|
|
"flex items-center justify-between px-4 py-1.5 text-xs font-medium",
|
|
|
|
|
isDark
|
|
|
|
|
? "bg-zinc-800 text-zinc-300"
|
|
|
|
|
: "bg-zinc-100 text-zinc-600",
|
|
|
|
|
)}
|
|
|
|
|
>
|
|
|
|
|
<span className="lowercase font-mono">
|
2026-04-19 06:39:06 +00:00
|
|
|
{language || t("code.fallbackLanguage")}
|
|
|
|
|
</span>
|
2026-04-18 18:51:53 +00:00
|
|
|
<button
|
|
|
|
|
type="button"
|
|
|
|
|
onClick={onCopy}
|
2026-04-20 00:17:22 +08:00
|
|
|
className={cn(
|
|
|
|
|
"inline-flex items-center gap-1 rounded px-1.5 py-0.5 font-mono transition-colors",
|
|
|
|
|
isDark
|
|
|
|
|
? "text-zinc-400 hover:bg-zinc-700 hover:text-zinc-200"
|
|
|
|
|
: "text-zinc-500 hover:bg-zinc-200 hover:text-zinc-700",
|
|
|
|
|
)}
|
2026-04-19 06:39:06 +00:00
|
|
|
aria-label={t("code.copyAria")}
|
2026-04-18 18:51:53 +00:00
|
|
|
>
|
|
|
|
|
{copied ? (
|
|
|
|
|
<Check className="h-3.5 w-3.5" />
|
|
|
|
|
) : (
|
|
|
|
|
<Copy className="h-3.5 w-3.5" />
|
|
|
|
|
)}
|
2026-04-19 06:39:06 +00:00
|
|
|
<span>{copied ? t("code.copied") : t("code.copy")}</span>
|
2026-04-18 18:51:53 +00:00
|
|
|
</button>
|
|
|
|
|
</div>
|
|
|
|
|
<SyntaxHighlighter
|
|
|
|
|
language={language}
|
|
|
|
|
style={isDark ? oneDark : oneLight}
|
|
|
|
|
customStyle={{
|
|
|
|
|
margin: 0,
|
|
|
|
|
padding: "1rem",
|
2026-04-20 00:17:22 +08:00
|
|
|
fontSize: "0.875rem",
|
|
|
|
|
lineHeight: 1.6,
|
2026-04-18 18:51:53 +00:00
|
|
|
}}
|
|
|
|
|
PreTag="pre"
|
|
|
|
|
wrapLongLines
|
|
|
|
|
>
|
|
|
|
|
{code}
|
|
|
|
|
</SyntaxHighlighter>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|