跳至正文
获取 Pro
AI 代理A.10

代码块容器

FreeRegistry
components/linyi/code-block.tsx
lucide-react
"use client";
 
import * as React from "react";
import { Check, Copy, FileCode2 } from "lucide-react";
import { cn } from "@/lib/cn";
 
export interface CodeBlockProps {
code: string;
language?: string;
filename?: string;
showLineNumbers?: boolean;
highlightLines?: number[];
compact?: boolean;
className?: string;
}
 
export function CodeBlock({
code,
language = "tsx",
filename,
showLineNumbers = true,
highlightLines = [],
compact = false,
className,
}: CodeBlockProps) {
const [copied, setCopied] = React.useState(false);
const timerRef = React.useRef<number | null>(null);
 
React.useEffect(() => {
return () => {
if (timerRef.current !== null) window.clearTimeout(timerRef.current);
};
}, []);
 
const lines = React.useMemo(() => code.replace(/\n$/, "").split("\n"), [code]);
const highlightSet = React.useMemo(() => new Set(highlightLines), [highlightLines]);
 
const handleCopy = async () => {
try {
await navigator.clipboard?.writeText(code);
} catch {
// Ignore clipboard permission denial in restricted preview contexts
}
setCopied(true);
if (timerRef.current !== null) window.clearTimeout(timerRef.current);
timerRef.current = window.setTimeout(() => setCopied(false), 1400);
};
 
return (
<div
className={cn(
"w-full rounded-[13px] overflow-hidden bg-surface border border-line-strong shadow-panel",
className
)}
>
{/* Header */}
<div className="flex items-center justify-between gap-2 px-3 min-h-[34px] bg-surface-2 border-b border-line">
<div className="flex items-center gap-1.5 min-w-0 font-mono-num text-[11px] text-fg-secondary">
<FileCode2 className="w-3.5 h-3.5 text-fg-muted flex-shrink-0" aria-hidden="true" />
<span className="truncate">{filename ?? language}</span>
{filename && !compact && (
<span className="px-1.5 py-0.5 rounded-badge bg-well border border-line text-[10px] text-fg-muted">
{language}
</span>
)}
</div>
 
<button
type="button"
onClick={handleCopy}
className="inline-flex items-center gap-1 px-2 h-6 rounded-pill text-[10px] font-mono-num text-fg-secondary hover:text-fg hover:bg-well cursor-pointer transition-colors flex-shrink-0"
aria-label={copied ? "已复制代码" : "复制代码"}
>
{copied ? (
<Check className="w-3 h-3 text-success" aria-hidden="true" />
) : (
<Copy className="w-3 h-3" aria-hidden="true" />
)}
<span>{copied ? "已复制" : "复制"}</span>
</button>
</div>
 
{/* Scrollable code area */}
<div className="overflow-x-auto no-scrollbar bg-well/45">
<pre
className={cn(
"m-0 font-mono-num leading-[1.65] py-2",
compact ? "text-[10px]" : "text-[11px]"
)}
>
<code className="block min-w-max">
{lines.map((line, idx) => {
const lineNumber = idx + 1;
const isHighlighted = highlightSet.has(lineNumber);
return (
<div
key={lineNumber}
className={cn(
"flex items-baseline pr-3",
isHighlighted
? "bg-accent-soft border-l-2 border-accent"
: "border-l-2 border-transparent"
)}
>
{showLineNumbers && (
<span
aria-hidden="true"
className={cn(
"select-none text-right text-fg-muted flex-shrink-0",
compact ? "w-7 pr-2.5" : "w-9 pr-3"
)}
>
{lineNumber}
</span>
)}
<span
className={cn(
"whitespace-pre text-fg",
!showLineNumbers && "pl-3"
)}
>
{line || " "}
</span>
</div>
);
})}
</code>
</pre>
</div>
</div>
);
}