Code to read and copy, with highlighting, a file name and line numbers.
- 4.5 kB JS 4.3 kB CSS gzipped
- No serious axe issues
- Server-rendered, hydrates
- 44px touch targets
- Right-to-left
- 12 tests
deliver.ts
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"),
)
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 upexport 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 aboutexport 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] } }), )When to use
Code to read and copy, with highlighting, a file name and line numbers. Use Diff instead when the code is a change.
Usage
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
Commands and JSON
Without a file name, Copy sits in the corner. Long lines scroll sideways; wrap wraps them instead.
npx acme login --workspace northwind --region eu-central-1
acme.config.json
{
"workspace": "northwind",
"region": "eu-central-1",
"seats": 48,
"sso": true
}
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 printexport 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.
.github/workflows/deploy.yml
# Deploys the API to every region, one at a time, and stops at the first failure.
name: deploy
on:
push:
branches: [main]
jobs:
us-east-1:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: npx acme deploy --region us-east-1 --wait
us-west-2:
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 restexport 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 |
|---|---|---|---|
coderequired | 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
pnpm dlx shadcn@latest add checkfu/uicontext/code-block