Code Block

A line-numbered listing and a unified diff.

Installation

pnpm dlx shadcn@latest add @ward/code-block

Add the Ward registry to components.json once:

"registries": {
  "@ward": "https://ward.so/r/{name}.json"
}

Or install from the URL with no configuration: npx shadcn@latest add https://ward.so/r/code-block.json

Usage

import CodeBlock from "@/components/ward/CodeBlock";
<CodeBlock
  variant="Code"
  filename="greet.ts"
  lines={[
    "export function greet(name: string) {",
    '  return "Hello, " + name;',
    "}",
  ]}
  labels={{ copy: "Copy", copied: "Copied" }}
  onCopy={(text) => console.log(text)}
/>

Props

PropTypeDefaultDescription
variant"Code" | "Diff""Code"Shows either a line-numbered listing or a unified diff.
linesstring[]CODE_LINESLines of code shown in the Code view.
codestring-Text copied to the clipboard by the Copy button, which falls back to the lines joined with new lines.
diffDiffRow[]DIFFUnified diff rows shown in the Diff view, each with old and new line numbers, a row type and its text pieces.
filenamestring"plan.ts"File name shown in the header.
labelsPartial<CodeBlockLabels>-Overrides for the Copy button text before and after copying.
onCopy(text: string) => void-Called with the copied text after a successful copy.

Ward · Version 1.1.0 · Registry manifest