Code block
<CodeBlock />Code in a card, with its language or file name and a copy button. It also shows diffs and terminal output, numbers and wraps lines, and comes small for a tool's output.
cart.ts
export const total = (items: Item[]) =>
items.reduce((sum, item) => sum + item.cents * item.qty, 0)diff
@@ -1,2 +1,2 @@
export const total = (items: Item[]) =>
- items.reduce((sum, item) => sum + item.price * item.qty, 0)
+ items.reduce((sum, item) => sum + item.cents * item.qty, 0)terminal
npm test -- cart
PASS cart.test.ts
✓ sums one item (2 ms)
✓ sums two items (1 ms)
Tests: 2 passed, 2 totalInstall
npx shadcn@latest add https://orbeez-kit.vercel.app/r/code-block.jsonUsage
tsx
<CodeBlock code={source} filename="cart.ts" lineNumbers />
<CodeBlock code={patch} language="diff" />
<CodeBlock code={session} language="terminal" size="sm" maxLines={8} />
<CodeBlock code={log} language="log" wrap />Good to know
- language="diff" tints added and removed lines across the full width, even scrolled sideways.
- language="terminal" reads lines starting with "$ " as commands and the rest as output. Copy gives back the commands alone, ready to run.
- wrap breaks long lines, the way logs read best. With lineNumbers, a wrapped line hangs under its text, clear of the number.
- size="sm" is for a tool's output, inside a Tool or a step.
Behaviour
- With maxLines it stops at that many lines and scrolls inside, so a long log never pushes the conversation down.
- The header is the same height whatever the language or file name, which truncates.
- Copying a terminal never copies its "$" signs or its output.