# Code Block

> Code to read and copy, with highlighting, a file name and line numbers.

Page: https://uicontext.com/docs/components/code-block

Group: Documents.

## When to use

Code to read and copy, with highlighting, a file name and line numbers. Use [Diff](https://uicontext.com/docs/components/diff.md) instead when the code is a change.

## Usage

```ts
import { CodeBlock } from "./ui/uicontext/components/CodeBlock.ts"

export const Install = <M>(h: Html.H<M>) => CodeBlock(h, { props: { code: "npm install effect", language: "sh" } })
```

## Examples

Each example is a complete file. Its imports point where the CLI installs the source.

### A file, with lines marked

The example the page opens with: Code Block in “A file, with lines marked”.

```ts title="src/demo.ts" source="www/src/examples/code-block/demo.ts"
import type { Html } from "effect/ui/html"
import * as stylex from "@stylexjs/stylex"
import { CodeBlock } from "./ui/uicontext/components/CodeBlock.ts"
import { sx } from "./ui/uicontext/components/Sx.ts"

const styles = stylex.create({ wrap: { inlineSize: "min(100%, 40rem)" } })

const source = `import { Effect, Schedule } from "effect"

// retry a webhook delivery: three tries, a second apart, then give up
export const deliver = (url: string, body: unknown) =>
  Effect.tryPromise(() => fetch(url, { method: "POST", body: JSON.stringify(body) })).pipe(
    Effect.retry(Schedule.spaced("1 second").pipe(Schedule.compose(Schedule.recurs(3)))),
    Effect.timeout("10 seconds"),
  )
`

// an API guide's example: a file name, numbered lines and the two lines the text talks about
export const WebhookExample = <M>(h: Html.H<M>) =>
	h.div.attrs(sx(styles.wrap)).children(
		CodeBlock(h, { props: { code: source, language: "ts", filename: "deliver.ts", lineNumbers: true, highlight: [6, 7] } }),
	)
```

### Commands and JSON

Without a file name, Copy sits in the corner. Long lines scroll sideways; `wrap` wraps them instead.

```ts title="src/command.ts" source="www/src/examples/code-block/command.ts"
import type { Html } from "effect/ui/html"
import * as stylex from "@stylexjs/stylex"
import { CodeBlock } from "./ui/uicontext/components/CodeBlock.ts"
import { sx } from "./ui/uicontext/components/Sx.ts"

const styles = stylex.create({ wrap: { display: "grid", gap: 12, inlineSize: "min(100%, 40rem)" } })

// a setup guide's steps: commands to paste, and the JSON they print
export const SetupCommands = <M>(h: Html.H<M>) =>
	h.div.attrs(sx(styles.wrap)).children(
		CodeBlock(h, { key: "cmd", props: { code: "npx acme login --workspace northwind --region eu-central-1", language: "sh" } }),
		CodeBlock(h, {
			key: "json",
			props: {
				code: `{\n  "workspace": "northwind",\n  "region": "eu-central-1",\n  "seats": 48,\n  "sso": true\n}`,
				language: "json",
				filename: "acme.config.json",
			},
		}),
	)
```

### A long file, folded

`fold` shows the first lines and folds the rest behind Show all. Copy still takes all of it.

```ts title="src/long.ts" source="www/src/examples/code-block/long.ts"
import type { Html } from "effect/ui/html"
import * as stylex from "@stylexjs/stylex"
import { CodeBlock } from "./ui/uicontext/components/CodeBlock.ts"
import { sx } from "./ui/uicontext/components/Sx.ts"

const styles = stylex.create({ wrap: { inlineSize: "min(100%, 40rem)" } })

const regions = ["us-east-1", "us-west-2", "eu-central-1", "eu-west-1", "ap-southeast-1", "ap-northeast-1", "sa-east-1"]
const source = [
	"# Deploys the API to every region, one at a time, and stops at the first failure.",
	"name: deploy",
	"on:",
	"  push:",
	"    branches: [main]",
	"jobs:",
	...regions.flatMap((region) => [
		`  ${region}:`,
		"    runs-on: ubuntu-latest",
		"    steps:",
		"      - uses: actions/checkout@v4",
		`      - run: npx acme deploy --region ${region} --wait`,
	]),
].join("\n")

// a long workflow file: the first lines, and Show all for the rest
export const DeployWorkflow = <M>(h: Html.H<M>) =>
	h.div.attrs(sx(styles.wrap)).children(CodeBlock(h, { props: { code: source, language: "text", filename: ".github/workflows/deploy.yml", lineNumbers: true, fold: 12 } }))
```

## API reference

**Props**

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `code` (required) | `string` | — | — |
| `language` | `string` | `"ts"` | "ts", "tsx", "js", "json", "sh" or "text"; "ts" by default |
| `filename` | `string` | — | A file name over the code, which also names it for screen readers and the Copy button |
| `lineNumbers` | `boolean` | — | Numbers in the margin, for a whole file |
| `highlight` | `number[]` | — | Lines to mark, counted from 1: the ones a step talks about |
| `wrap` | `boolean` | — | Wrap long lines instead of scrolling sideways |
| `fold` | `number` | `24` | Lines shown before the rest folds behind "Show all"; 0 never folds, 24 by default |

## Installation

```sh
pnpm dlx shadcn@latest add checkfu/uicontext/code-block
```

With npm: `npx shadcn@latest add checkfu/uicontext/code-block`. With yarn: `yarn dlx shadcn@latest add checkfu/uicontext/code-block`. With bun: `bunx --bun shadcn@latest add checkfu/uicontext/code-block`.

It writes these files:

- `src/ui/uicontext/components/Button.ts`
- `src/ui/uicontext/components/CodeBlock.ts`
- `src/ui/uicontext/components/CopyButton.ts`
- `src/ui/uicontext/components/Sx.ts`
- `src/ui/uicontext/components/button.stylex.ts`
- `src/ui/uicontext/components/tokens.stylex.ts`
- `src/ui/uicontext/components/variants.ts`

And installs the npm packages `@stylexjs/stylex`, `effect`.

## Source

- [src/CodeBlock.ts](https://github.com/checkfu/uicontext/blob/main/src/CodeBlock.ts)

## Related

- [Editor](https://uicontext.com/docs/components/editor.md) (`editor`): A block editor with a slash menu for headings, lists, to-dos and more.
- [Document Blocks](https://uicontext.com/docs/components/document-blocks.md) (`document-blocks`): Read-only rendering of a document's blocks.
- [Markdown](https://uicontext.com/docs/components/markdown.md) (`markdown`): Renders Markdown with readable type.
- [JSON Viewer](https://uicontext.com/docs/components/json-viewer.md) (`json-viewer`): A JSON value as a tree to open, search and walk with the arrow keys, such as a webhook's payload or a tool call's result.
- [Log Viewer](https://uicontext.com/docs/components/log-viewer.md) (`log-viewer`): A log that follows its newest line, with a filter, level buttons and Wrap, such as a deploy's or a server's.
- [Diff](https://uicontext.com/docs/components/diff.md) (`diff`): Added and removed lines in a change, side by side or inline.
