Components / Surfaces
Code Block
Displays code with an optional header, line numbers and diff highlighting.
pnpm add loseta
<div class="code-block" style="max-width: 32rem">
<pre dir="ltr" tabindex="0"><code>pnpm add loseta</code></pre>
</div>Installation
pnpm dlx loseta add code-blocknpx loseta add code-blockExamples
Header and actions
import { streamText } from "ai";
export async function POST(request: Request) {
const { messages } = await request.json();
const result = streamText({
model: "anthropic/claude-sonnet-4.5",
messages
});
return result.toUIMessageStreamResponse();
}
<div class="code-block">
<header>
<span data-slot="title">app/api/chat/route.ts</span>
<div data-slot="actions">
<button class="btn" data-variant="ghost" data-size="icon-xs" type="button" aria-label="Copy code"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><rect width="14" height="14" x="8" y="8" rx="2" ry="2"/><path d="M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2"/></svg></button>
</div>
</header>
<pre dir="ltr" tabindex="0"><code>import { streamText } from "ai";
export async function POST(request: Request) {
const { messages } = await request.json();
const result = streamText({
model: "anthropic/claude-sonnet-4.5",
messages
});
return result.toUIMessageStreamResponse();
}</code></pre>
</div>Line numbers
if (!session) {
redirect("/login");
}
const projects = await db.project.findMany({
where: { ownerId: session.user.id },
orderBy: { updatedAt: "desc" }
});
return <ProjectList projects={projects} />;
}
<div class="code-block">
<header>
<span data-slot="title">app/dashboard/page.tsx</span>
</header>
<pre data-line-numbers style="--line-start: 12" dir="ltr" tabindex="0"><code><span data-slot="line"><span data-slot="line-content"> if (!session) {</span></span>
<span data-slot="line"><span data-slot="line-content"> redirect("/login");</span></span>
<span data-slot="line"><span data-slot="line-content"> }</span></span>
<span data-slot="line"><span data-slot="line-content"></span></span>
<span data-slot="line"><span data-slot="line-content"> const projects = await db.project.findMany({</span></span>
<span data-slot="line"><span data-slot="line-content"> where: { ownerId: session.user.id },</span></span>
<span data-slot="line"><span data-slot="line-content"> orderBy: { updatedAt: "desc" }</span></span>
<span data-slot="line"><span data-slot="line-content"> });</span></span>
<span data-slot="line"><span data-slot="line-content"> return <ProjectList projects={projects} />;</span></span>
<span data-slot="line"><span data-slot="line-content">}</span></span></code></pre>
</div>Diff and highlighted lines
export function Greeting({ name }: { name: string }) {
return <p>Hello, {name}</p>;
return <p className="text-sm text-muted-foreground">Hello, {name}!</p>;
return <p>Hello, {name}!</p>;
const message = "Hello, " + name + "!";
}
<div class="code-block">
<header>
<span data-slot="title">components/greeting.tsx</span>
</header>
<pre dir="ltr" tabindex="0"><code><span data-slot="line"><span data-slot="line-content">export function Greeting({ name }: { name: string }) {</span></span>
<span data-slot="line"><span data-slot="line-content"> return <p>Hello, {name}</p>;</span></span>
<span data-slot="line" data-variant="removed"><span data-slot="line-content"> return <p className="text-sm text-muted-foreground">Hello, {name}!</p>;</span></span>
<span data-slot="line" data-variant="added"><span data-slot="line-content"> return <p>Hello, {name}!</p>;</span></span>
<span data-slot="line" data-variant="highlighted"><span data-slot="line-content"> const message = "Hello, " + name + "!";</span></span>
<span data-slot="line"><span data-slot="line-content">}</span></span></code></pre>
</div>Small, wrapped and destructive
TypeError: The model "anthropic/claude-sonnet-4.5" returned an error: Request too large. The prompt is 212,431 tokens but the context window is 200,000 tokens.
<div class="code-block" data-size="sm" data-variant="destructive">
<header>
<span data-slot="title">Error</span>
<div data-slot="actions">
<button class="btn" data-variant="ghost" data-size="icon-xs" type="button" aria-label="Wrap lines" aria-pressed="true"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><line x1="3" x2="21" y1="6" y2="6"/><path d="M3 12h15a3 3 0 1 1 0 6h-4"/><polyline points="16 16 14 18 16 20"/><line x1="3" x2="10" y1="18" y2="18"/></svg></button>
<button class="btn" data-variant="ghost" data-size="icon-xs" type="button" aria-label="Copy output"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><rect width="14" height="14" x="8" y="8" rx="2" ry="2"/><path d="M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2"/></svg></button>
</div>
</header>
<pre data-wrap dir="ltr" tabindex="0"><code>TypeError: The model "anthropic/claude-sonnet-4.5" returned an error: Request too large. The prompt is 212,431 tokens but the context window is 200,000 tokens.</code></pre>
</div>Floating actions
$ pnpm add ai @ai-sdk/react zod
Packages: +14
Progress: resolved 412, reused 398, downloaded 14, added 14, done
Done in 2.8s using pnpm v10.18.0
<div class="code-block">
<div data-slot="actions">
<button class="btn" data-variant="ghost" data-size="icon-xs" type="button" aria-label="Copy output"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><rect width="14" height="14" x="8" y="8" rx="2" ry="2"/><path d="M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2"/></svg></button>
</div>
<pre dir="ltr" tabindex="0"><code>$ pnpm add ai @ai-sdk/react zod
Packages: +14
Progress: resolved 412, reused 398, downloaded 14, added 14, done
Done in 2.8s using pnpm v10.18.0</code></pre>
</div>Accessibility and behavior
- Use [data-slot="actions"] for copy and wrap controls; it floats when it is a direct child of the root. Add data-line-numbers to the pre; line numbers need explicit [data-slot="line"] elements. Set style="--line-start: 42" to change the first number and data-wrap to wrap long lines. Copy and wrap behavior is consumer JavaScript; the pre is dir="ltr" and focusable. Syntax highlighting is not included; put token spans inside [data-slot="line-content"]. Use footer for controls below the code; Show More needs your own JavaScript.
Files
components/code-block.cssbasestyles/nova/code-block.cssstyleInstall contract
The CLI writes the base file under components/, the visual file for the selected style under styles/<name>/, and any primitive dependency underprimitives/. The generated index.css imports them in the right layer order.