Components
Diff
NativeShows what changed between two texts, line by line.
lib/cart.ts+3 added, −2 removed
| Old line | New line | Change | Text |
|---|---|---|---|
| 1 | 1 | Unchanged | export function total(cart: Cart) { |
| 2 | Removed | const subtotal = cart.items.reduce((sum, item) => sum + item.price, 0); | |
| 2 | Added | const subtotal = cart.items.reduce((sum, item) => sum + item.price * item.quantity, 0); | |
| 3 | 3 | Unchanged | const tax = subtotal * cart.taxRate; |
| 4 | 4 | Unchanged | const total = subtotal + tax; |
| 5 | Removed | return Math.floor(total * 100) / 100; | |
| 5 | Added | // Round to the nearest cent: cutting it short lost a cent on some carts. | |
| 6 | Added | return Math.round(total * 100) / 100; | |
| 6 | 7 | Unchanged | } |
| 7 | 8 | Unchanged | |
| 5 unchanged lines | |||
Installation
npx nook-ui@latest add diffPrompt
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 "Diff" component to this project, exactly as Nook ships it.
Shows what changed between two texts, line by line.
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 diff`. It writes `components/ui/diff.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/diff.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 { Diff } from "@/components/ui/diff";
const BEFORE = `export function total(cart: Cart) {
const subtotal = cart.items.reduce((sum, item) => sum + item.price, 0);
const tax = subtotal * cart.taxRate;
const total = subtotal + tax;
return Math.floor(total * 100) / 100;
}
export function format(amount: number) {
return "$" + amount.toFixed(2);
}
export const EMPTY: Cart = { items: [], taxRate: 0 };`;
const AFTER = `export function total(cart: Cart) {
const subtotal = cart.items.reduce((sum, item) => sum + item.price * item.quantity, 0);
const tax = subtotal * cart.taxRate;
const total = subtotal + tax;
// Round to the nearest cent: cutting it short lost a cent on some carts.
return Math.round(total * 100) / 100;
}
export function format(amount: number) {
return "$" + amount.toFixed(2);
}
export const EMPTY: Cart = { items: [], taxRate: 0 };`;
export default function DiffDefault() {
return <Diff filename="lib/cart.ts" before={BEFORE} after={AFTER} context={2} />;
}
```
API
- `Diff`: The comparison. `diffLines(before, after)` is exported if you only need the data.
- `before` (`string`): The old text.
- `after` (`string`): The new text.
- `filename` (`string`): Shown in the header.
- `context` (`number`, default 3): Unchanged lines kept around each change.
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: A table with old and new line numbers. Added and removed lines carry a + or − and a word for screen readers, never a tint alone. Long unchanged runs are folded and counted.
Done when the component renders like the example in light and dark mode and the project type-checks.Usage
import { Diff } from "@/components/ui/diff";
const BEFORE = `export function total(cart: Cart) {
const subtotal = cart.items.reduce((sum, item) => sum + item.price, 0);
const tax = subtotal * cart.taxRate;
const total = subtotal + tax;
return Math.floor(total * 100) / 100;
}
export function format(amount: number) {
return "$" + amount.toFixed(2);
}
export const EMPTY: Cart = { items: [], taxRate: 0 };`;
const AFTER = `export function total(cart: Cart) {
const subtotal = cart.items.reduce((sum, item) => sum + item.price * item.quantity, 0);
const tax = subtotal * cart.taxRate;
const total = subtotal + tax;
// Round to the nearest cent: cutting it short lost a cent on some carts.
return Math.round(total * 100) / 100;
}
export function format(amount: number) {
return "$" + amount.toFixed(2);
}
export const EMPTY: Cart = { items: [], taxRate: 0 };`;
export default function DiffDefault() {
return <Diff filename="lib/cart.ts" before={BEFORE} after={AFTER} context={2} />;
}Examples
Config change
nginx.conf+3 added, −1 removed
| Old line | New line | Change | Text |
|---|---|---|---|
| 1 | 1 | Unchanged | server { |
| 2 | Removed | listen 80; | |
| 2 | Added | listen 443 ssl; | |
| 3 | 3 | Unchanged | server_name acme.com; |
| 4 | Added | ssl_certificate /etc/ssl/acme.pem; | |
| 4 | 5 | Unchanged | location / { |
| 5 | 6 | Unchanged | proxy_pass http://127.0.0.1:3000; |
| 7 | Added | proxy_read_timeout 60s; | |
| 6 | 8 | Unchanged | } |
| 7 | 9 | Unchanged | } |
Few lines of context
pipeline.ts+1 added, −1 removed
| Old line | New line | Change | Text |
|---|---|---|---|
| 6 unchanged lines | |||
| 7 | 7 | Unchanged | const step7 = run(7); |
| 8 | Removed | const step8 = run(8); | |
| 8 | Added | const step8 = runSafely(8); | |
| 9 | 9 | Unchanged | const step9 = run(9); |
| 5 unchanged lines | |||
No changes
CHANGELOG.md+0 added, −0 removed
| Old line | New line | Change | Text |
|---|---|---|---|
| 3 unchanged lines | |||
Props
Diff
The comparison. `diffLines(before, after)` is exported if you only need the data.
| Prop | Type | Default | Description |
|---|---|---|---|
| before | string | – | The old text. |
| after | string | – | The new text. |
| filename | string | – | Shown in the header. |
| context | number | 3 | Unchanged lines kept around each change. |
Accessibility
A table with old and new line numbers. Added and removed lines carry a + or − and a word for screen readers, never a tint alone. Long unchanged runs are folded and counted.
Checked against WCAG 2.1 AA with automated axe tests and keyboard tests on every release.