Components
Code block
Built on Base UIServer-rendered syntax coloring, highlighted lines and file tabs with one copy control.
lib/deploy.ts
// Ship only after checks pass.
export async function deploy(project: string) {
const checks = await verify(project);
Highlighted: if (!checks.passed) return false;
Highlighted: return publish({ project, region: "fra1" });
}Installation
npx nook-ui@latest add code-blockPrompt
Paste this into your AI coding assistant. It adds this exact component, the same way the CLI does, and tells the assistant how to use it.
Short. For assistants that can run commands in your project.
Add the Nook "Code block" component to this project, exactly as Nook ships it.
Server-rendered syntax coloring, highlighted lines and file tabs with one copy control.
Steps
1. Check for `nook.json` in the project root. If it is missing, run `npx nook-ui@latest init` first (React or Next.js with Tailwind CSS v4). It adds the Nook tokens, the theme and `cn()`.
2. Run `npx nook-ui@latest add code-block`. It writes `components/ui/code-block.tsx` together with everything the component needs. Do not write the component from memory.
3. If you cannot run commands, fetch http://localhost:3000/r/code-block.json, write every entry of `files` to the path it names, install the packages in `dependencies`, and repeat for each name in `registryDependencies`.
4. Use it where I ask. Start from this example:
```tsx
import { CodeBlock } from "@/components/ui/code-block";
export default function CodeBlockDefault() {
return (
<div className="w-full max-w-xl">
<CodeBlock
filename="lib/deploy.ts"
language="ts"
lineNumbers
highlight="4-5"
code={
'// Ship only after checks pass.\nexport async function deploy(project: string) {\n const checks = await verify(project);\n if (!checks.passed) return false;\n return publish({ project, region: "fra1" });\n}'
}
/>
</div>
);
}
```
API
- `CodeBlock`: A static tokenized code viewer. Only its control shell hydrates.
- `code` (`string`): The exact source text.
- `language` (`CodeLanguage`): js/ts/jsx/tsx, json, bash/shell, css, html, python or text.
- `filename` (`string`): Header and accessible name.
- `lineNumbers` (`boolean`): Shows decorative line numbers.
- `highlight` (`string`): One-based lines and ranges, such as 2,4-6.
- `defaultWrap` (`boolean`): Initially wrap long lines.
- `CodeTabs`: File or package-manager tabs built on Tabs.
- `files` (`CodeFile[]`): Unique value, label, code and optional language, lineNumbers and highlight.
- `value / defaultValue` (`string`): Controlled or initial selected file.
- `onValueChange` (`(value: string) => void`): Selection callback.
Rules
- Colors, radii, shadows and motion come from Nook tokens and classes such as `bg-primary`, `text-muted-foreground`, `border-border`, `rounded-lg` and `h-control`. Never hard-code them.
- Do not restyle or simplify the file. Change how it looks through `className`, its props and the theme.
- It must keep working with the keyboard, in light and dark mode, and in the sharp, soft and bold styles.
- Accessibility: Code stays readable without JavaScript. Scroll regions are focusable; copy feedback is announced. Tabs use arrow keys. Comments are italic as well as muted.
Done when the component renders like the example in light and dark mode and the project type-checks.Usage
import { CodeBlock } from "@/components/ui/code-block";
export default function CodeBlockDefault() {
return (
<div className="w-full max-w-xl">
<CodeBlock
filename="lib/deploy.ts"
language="ts"
lineNumbers
highlight="4-5"
code={
'// Ship only after checks pass.\nexport async function deploy(project: string) {\n const checks = await verify(project);\n if (!checks.passed) return false;\n return publish({ project, region: "fra1" });\n}'
}
/>
</div>
);
}Examples
Package managers
npx nook-ui@latest add code-blockLine numbers and highlight
counter.tsx
import { useState } from "react";
export function Counter() {
Highlighted: const [count, setCount] = useState(0);
Highlighted: return (
<button onClick={() => setCount(count + 1)}>
Clicked {count} times
</button>
);
}Shell commands
bash
git checkout -b fix/checkout-total
pnpm install
pnpm test --filter cartScroll or wrap
scrolls sideways
curl https://api.acme.com/v1/invoices -H "Authorization: Bearer sk_live_example" -H "Content-Type: application/json" -d '{"customer":"cus_Mj3k","items":[{"sku":"team-plan","quantity":12}],"memo":"November seats"}'wraps by default
curl https://api.acme.com/v1/invoices -H "Authorization: Bearer sk_live_example" -H "Content-Type: application/json" -d '{"customer":"cus_Mj3k","items":[{"sku":"team-plan","quantity":12}],"memo":"November seats"}'CSS, JSON and Python
theme.css
.card {
border-radius: 12px;
padding: 16px;
/* matches the new spacing scale */
}config.json
{
"name": "acme-web",
"private": true,
"version": "1.4.0"
}report.py
def total(items):
return sum(i["price"] * i["qty"] for i in items)Props
CodeBlock
A static tokenized code viewer. Only its control shell hydrates.
| Prop | Type | Default | Description |
|---|---|---|---|
| code | string | – | The exact source text. |
| language | CodeLanguage | – | js/ts/jsx/tsx, json, bash/shell, css, html, python or text. |
| filename | string | – | Header and accessible name. |
| lineNumbers | boolean | – | Shows decorative line numbers. |
| highlight | string | – | One-based lines and ranges, such as 2,4-6. |
| defaultWrap | boolean | – | Initially wrap long lines. |
CodeTabs
File or package-manager tabs built on Tabs.
| Prop | Type | Default | Description |
|---|---|---|---|
| files | CodeFile[] | – | Unique value, label, code and optional language, lineNumbers and highlight. |
| value / defaultValue | string | – | Controlled or initial selected file. |
| onValueChange | (value: string) => void | – | Selection callback. |
Accessibility
Code stays readable without JavaScript. Scroll regions are focusable; copy feedback is announced. Tabs use arrow keys. Comments are italic as well as muted.
Checked against WCAG 2.1 AA with automated axe tests and keyboard tests on every release.