Code Block

View as Markdown

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

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"),
  )

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
}

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:

API reference

Props

CodeBlock props
NameTypeDefaultDescription
coderequiredstring——
languagestring"ts""ts", "tsx", "js", "json", "sh" or "text"; "ts" by default
filenamestring—A file name over the code, which also names it for screen readers and the Copy button
lineNumbersboolean—Numbers in the margin, for a whole file
highlightnumber[]—Lines to mark, counted from 1: the ones a step talks about
wrapboolean—Wrap long lines instead of scrolling sideways
foldnumber24Lines shown before the rest folds behind "Show all"; 0 never folds, 24 by default

Installation

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

Source