Code Block
Syntax-highlighted code for assistant answers, with highlighted lines and multi-file tabs.
Installation
01
Install with the shadcn CLI. The NiceUI theme comes along the first time.
$ npx shadcn@latest add https://niceui.dev/r/ai-code-block.json02
Or copy the source below (2 files). Dependencies: lucide-react @upstash/ratelimit.
Source
components/niceui/ai/AICodeBlock.tsx
import { useState } from 'react';
import { Bot, Check, FileText, Terminal } from 'lucide-react';
import { CodeBlock } from '@/components/niceui/ui/CodeBlock';
import { cn } from '@/lib/cn';
export type CodeFile = { name: string; code: string; highlight?: number[] };
export function CodeTabs({ files, className }: { files: CodeFile[]; className?: string }) {
const [active, setActive] = useState(0);
const file = files[active];
return (
<div className={cn('overflow-hidden rounded-xl bg-ink-950 shadow-card', className)}>
<div className="flex items-center gap-1 overflow-x-auto border-b border-white/5 bg-ink-900/60 px-2 pt-2">
{files.map((f, i) => (
<button
key={f.name}
onClick={() => setActive(i)}
className={cn(
'focus-ring relative flex shrink-0 items-center gap-1.5 rounded-t-lg px-3 py-2 font-mono text-xs transition-colors',
i === active ? 'bg-ink-950 text-ink-50' : 'text-ink-500 hover:text-ink-200',
)}
>
{f.name.endsWith('.sh') ? <Terminal className="h-3.5 w-3.5" /> : <FileText className="h-3.5 w-3.5" />}
{f.name}
<span
className={cn(
'absolute inset-x-2 top-0 h-px origin-center bg-brand-400 transition-transform duration-300 ease-spring',
i === active ? 'scale-x-100' : 'scale-x-0',
)}
/>
</button>
))}
</div>
<div key={file.name} className="animate-fade-in">
<CodeBlock code={file.code} filename={file.name} highlightLines={file.highlight} className="rounded-none shadow-none" maxHeight={300} />
</div>
</div>
);
}
const RATE_LIMIT = `import { Ratelimit } from '@upstash/ratelimit';
import { redis } from '@/lib/redis';
const limiter = new Ratelimit({
redis,
limiter: Ratelimit.slidingWindow(20, '10 s'),
});
export async function POST(req: Request) {
const ip = req.headers.get('x-forwarded-for') ?? 'anon';
const { success } = await limiter.limit(ip);
if (!success) return new Response('Too many requests', { status: 429 });
return handleChat(req);
}`;
export function CodeHighlightDemo() {
const [applied, setApplied] = useState(false);
return (
<div className="mx-auto flex w-full max-w-2xl gap-3">
<span className="mt-0.5 flex h-7 w-7 shrink-0 items-center justify-center rounded-lg bg-gradient-to-br from-brand-400 to-brand-700 text-white">
<Bot className="h-4 w-4" />
</span>
<div className="min-w-0 flex-1 space-y-3">
<p className="text-sm leading-relaxed text-ink-300">
Add a sliding-window limiter before the handler. The <span className="text-brand-300">highlighted lines</span> are the only changes to <code className="font-mono text-ink-100">route.ts</code>:
</p>
<CodeBlock code={RATE_LIMIT} filename="app/api/chat/route.ts" highlightLines={[4, 5, 6, 7, 11, 12]} maxHeight={320} />
<div className="flex items-center gap-2">
<button
onClick={() => setApplied((a) => !a)}
className={cn(
'focus-ring flex h-8 items-center gap-1.5 rounded-lg px-3 text-xs font-medium transition-all duration-300 ease-spring active:scale-95',
applied ? 'bg-emerald-500/15 text-emerald-300' : 'bg-ink-50 text-ink-950 hover:bg-white',
)}
>
{applied && <Check className="h-3.5 w-3.5 animate-scale-in" />}
{applied ? 'Applied to route.ts' : 'Apply change'}
</button>
<span className="text-xs text-ink-500">+6 lines</span>
</div>
</div>
</div>
);
}
const FILES: CodeFile[] = [
{
name: 'useChat.ts',
highlight: [6],
code: `import { useState } from 'react';
export function useChat() {
const [messages, setMessages] = useState<Message[]>([]);
const send = async (text: string) => {
const res = await fetch('/api/chat', { method: 'POST', body: text });
setMessages((m) => [...m, await res.json()]);
};
return { messages, send };
}`,
},
{
name: 'Chat.tsx',
highlight: [4, 5],
code: `import { useChat } from './useChat';
export function Chat() {
const { messages, send } = useChat();
return <Thread messages={messages} onSend={send} />;
}`,
},
{
name: 'setup.sh',
code: `# install and run locally
npm install @upstash/ratelimit
cp .env.example .env.local
npm run dev`,
},
];
export function CodeTabsDemo() {
return (
<div className="mx-auto w-full max-w-2xl">
<CodeTabs files={FILES} />
</div>
);
}components/niceui/ui/CodeBlock.tsx
import { useState } from 'react';
import { Check, Copy } from 'lucide-react';
import { cn } from '@/lib/cn';
const KEYWORDS =
/\b(import|from|export|default|const|let|var|function|return|if|else|for|while|async|await|new|type|interface|extends|true|false|null|undefined|class|def|print)\b/;
type Token = { text: string; cls: string };
function tokenize(line: string): Token[] {
const tokens: Token[] = [];
const re =
/(\/\/.*$|#.*$)|("(?:[^"\\]|\\.)*"|'(?:[^'\\]|\\.)*'|`(?:[^`\\]|\\.)*`)|(<\/?[A-Za-z][\w.]*)|(\b\d+(?:\.\d+)?\b)|(\b[A-Za-z_$][\w$]*\b)|(\s+)|([^\sA-Za-z0-9_$"'`]+)/g;
let m: RegExpExecArray | null;
while ((m = re.exec(line))) {
const [text, comment, str, tag, num, word] = m;
let cls = 'text-ink-300';
if (comment) cls = 'text-ink-500 italic';
else if (str) cls = 'text-emerald-300';
else if (tag) cls = 'text-brand-300';
else if (num) cls = 'text-amber-300';
else if (word && KEYWORDS.test(word)) cls = 'text-rose-300';
else if (word && /^[A-Z]/.test(word)) cls = 'text-sky-200';
tokens.push({ text, cls });
}
return tokens;
}
type CodeBlockProps = {
code: string;
filename?: string;
language?: string;
showLineNumbers?: boolean;
highlightLines?: number[];
className?: string;
maxHeight?: number;
};
export function CodeBlock({
code,
filename,
language = 'tsx',
showLineNumbers = true,
highlightLines = [],
className,
maxHeight = 480,
}: CodeBlockProps) {
const [copied, setCopied] = useState(false);
const lines = code.replace(/\n$/, '').split('\n');
const copy = async () => {
try {
await navigator.clipboard.writeText(code);
setCopied(true);
setTimeout(() => setCopied(false), 1600);
} catch {
setCopied(false);
}
};
return (
<div className={cn('group relative overflow-hidden rounded-xl bg-ink-950 shadow-hairline', className)}>
<div className="flex items-center justify-between border-b border-white/5 px-4 py-2.5">
<div className="flex items-center gap-2 text-xs text-ink-400">
<span className="flex gap-1.5">
<span className="h-2.5 w-2.5 rounded-full bg-ink-700" />
<span className="h-2.5 w-2.5 rounded-full bg-ink-700" />
<span className="h-2.5 w-2.5 rounded-full bg-ink-700" />
</span>
<span className="ml-2 font-mono">{filename ?? language}</span>
</div>
<button
onClick={copy}
className="focus-ring relative flex h-7 items-center gap-1.5 rounded-md px-2 text-xs text-ink-400 transition-colors hover:bg-white/5 hover:text-ink-100"
aria-label="Copy code"
>
<span className="relative h-3.5 w-3.5">
<Copy className={cn('absolute inset-0 h-3.5 w-3.5 transition-all duration-300', copied ? 'scale-50 opacity-0' : 'scale-100 opacity-100')} />
<Check className={cn('absolute inset-0 h-3.5 w-3.5 text-emerald-400 transition-all duration-300 ease-spring', copied ? 'scale-100 opacity-100' : 'scale-50 opacity-0')} />
</span>
{copied ? 'Copied' : 'Copy'}
</button>
</div>
<pre className="overflow-auto py-3 font-mono text-[13px] leading-6" style={{ maxHeight }}>
<code>
{lines.map((line, i) => (
<div
key={i}
className={cn(
'flex px-4',
highlightLines.includes(i + 1) && 'bg-brand-500/10 shadow-[inset_2px_0_0_theme(colors.brand.400)]',
)}
>
{showLineNumbers && <span className="mr-5 w-6 shrink-0 select-none text-right text-ink-600">{i + 1}</span>}
<span className="whitespace-pre">
{tokenize(line).map((t, j) => (
<span key={j} className={t.cls}>
{t.text}
</span>
))}
</span>
</div>
))}
</code>
</pre>
</div>
);
}