Code block
The dark navy code panel with language tabs, copy and a status line.
const image = await client.take({
url: "https://northwind.io",
format: "webp",
});200 OK · image/webp · 1.2s
Install
$ npx shadcn@latest add @nudgekit/code-blockOr with no config: npx shadcn@latest add https://ui.nudgekit.app/r/code-block.json
Brings in lucide-react, and the theme.
Usage
usage.tsx
import { CodeBlock } from "@/components/ui/code-block"
<CodeBlock
samples={[
{ label: "Node.js", code: nodeSample },
{ label: "cURL", code: curlSample },
]}
footer="200 OK · image/webp · 1.2s"
/>Do
- Show the real request a developer will copy.
- Add a footer with the response when it helps.
Don't
- Use a light code panel; code is always on navy.
Code
What the CLI writes into your project. It is yours to change.
components/ui/code-block.tsx
"use client";
import * as React from "react";
import { Check, Copy } from "lucide-react";
import { cn } from "@/lib/utils";
export interface CodeSample {
/** Tab label, for example "Node.js". */
label: string;
code: string;
}
export interface CodeBlockProps {
/** One sample shows no tabs; two or more show a tab per sample. */
samples: CodeSample[];
/** An optional status line under the code, for example "200 OK · image/webp · 1.2s". */
footer?: React.ReactNode;
className?: string;
}
/** The dark navy code panel with language tabs and a copy button. */
function CodeBlock({ samples, footer, className }: CodeBlockProps) {
const [active, setActive] = React.useState(0);
const [copied, setCopied] = React.useState(false);
const sample = samples[active];
return (
<div data-slot="code-block" className={cn("overflow-hidden rounded-card bg-ink text-[#C7D3E0]", className)}>
<div className="flex items-center gap-1 border-b border-white/10 px-3 py-2.5">
{samples.length > 1 ? (
<div role="tablist" aria-label="Language" className="flex gap-1">
{samples.map((s, i) => (
<button
key={s.label}
type="button"
role="tab"
aria-selected={i === active}
onClick={() => setActive(i)}
className={cn(
"rounded-md px-3 py-1.5 text-[13px] transition-colors duration-150 focus-visible:outline-2 focus-visible:outline-white/60",
i === active ? "bg-white/10 text-white" : "text-[#8FA2B8] hover:text-white",
)}
>
{s.label}
</button>
))}
</div>
) : (
<span className="px-2 text-[13px] text-[#8FA2B8]">{sample.label}</span>
)}
<button
type="button"
onClick={async () => {
await navigator.clipboard.writeText(sample.code);
setCopied(true);
setTimeout(() => setCopied(false), 1500);
}}
className="ml-auto inline-flex items-center gap-1.5 rounded-md px-2 py-1.5 text-xs text-[#8FA2B8] transition-colors hover:text-white focus-visible:outline-2 focus-visible:outline-white/60"
aria-label={copied ? "Copied" : "Copy code"}
>
{copied ? <Check className="size-3.5" /> : <Copy className="size-3.5" />}
{copied ? "Copied" : "Copy"}
</button>
</div>
<pre className="overflow-x-auto p-5 font-mono text-[13px] leading-[1.8]">
<code>{sample.code}</code>
</pre>
{footer && <div className="border-t border-white/10 px-5 py-3 font-mono text-xs text-[#8FA2B8]">{footer}</div>}
</div>
);
}
export { CodeBlock };