# Document Blocks

> Read-only rendering of a document's blocks.

Page: https://uicontext.com/docs/components/document-blocks

Group: Documents.

## When to use

Read-only rendering of a document's blocks. Use [Editor](https://uicontext.com/docs/components/editor.md) instead when people edit the document.

## Usage

```ts
import type { Block } from "./ui/uicontext/components/Blocks.ts"
import { BlocksView } from "./ui/uicontext/components/EditorBlocks.ts"

export const PublishedPage = <M>(h: Html.H<M>, page: { readonly title: string; readonly blocks: ReadonlyArray<Block> }) =>
	BlocksView(h, { blocks: page.blocks, label: page.title })
```

## Examples

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

### A published page

The example the page opens with: Document Blocks in “A published page”.

````ts title="src/demo.ts" source="www/src/examples/document-blocks/demo.ts"
import type { Html } from "effect/ui/html"
import * as stylex from "@stylexjs/stylex"
import { fromMarkdown } from "./ui/uicontext/components/Blocks.ts"
import { BlocksView } from "./ui/uicontext/components/EditorBlocks.ts"
import { sx } from "./ui/uicontext/components/Sx.ts"
import { color, corner, space } from "./ui/uicontext/components/tokens.stylex.ts"

// A published page, read-only: the same blocks the editor writes, drawn as plain elements with no
// editing. Use it for a shared page, a printout, a message preview or a version in history.

const styles = stylex.create({
	page: {
		inlineSize: "min(100%, 40rem)",
		paddingBlock: space.xl,
		paddingInline: { default: space.xxl, "@media (max-width: 600px)": space.lg },
		borderRadius: corner.card,
		borderWidth: 1,
		borderStyle: "solid",
		borderColor: color.line,
		backgroundColor: color.panel,
	},
})

let next = 0
const releaseNotes = fromMarkdown(
	[
		"# Release notes 2.4",
		"",
		"Shared pages load **twice as fast**, and comments now stay on the sentence they're about.",
		"",
		"> [!NOTE]",
		"> Workspaces update over the next week. Nothing to do on your side.",
		"",
		"## What's new",
		"",
		"- Comments anchored to a range of text",
		"- Inline databases with saved views",
		"  - Table, board and list layouts",
		"- Faster search across every page",
		"",
		"## Before you upgrade",
		"",
		"- [x] Back up custom templates",
		"- [ ] Tell your team about the new shortcuts",
		"",
		"```ts",
		"const ready = checks.every((check) => check.passed)",
		"```",
	].join("\n"),
	() => `note-${(next += 1)}`,
)

export const ReleaseNotes = <M>(h: Html.H<M>) =>
	BlocksView(h, { blocks: releaseNotes, label: "Release notes 2.4", style: styles.page })
````

### Toggles, pictures and code

Readers open and close toggles with a click or the keyboard; pictures keep their place while they load; code names its language.

```ts title="src/toggles-and-pictures.ts" source="www/src/examples/document-blocks/toggles-and-pictures.ts"
import type { Html } from "effect/ui/html"
import * as stylex from "@stylexjs/stylex"
import { type Block, makeBlock } from "./ui/uicontext/components/Blocks.ts"
import { BlocksView } from "./ui/uicontext/components/EditorBlocks.ts"
import { space } from "./ui/uicontext/components/tokens.stylex.ts"

// An onboarding page someone shared: readers open the toggles themselves, pictures keep their size
// while they load, and code says its language.

const styles = stylex.create({ page: { inlineSize: "min(100%, 38rem)", paddingBlock: space.md } })

const text = (value: string) => ({ text: value })
const screenshot =
	"data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 600 240'><rect width='600' height='240' fill='%23f4f4f5'/><rect x='24' y='24' width='160' height='192' rx='10' fill='%23e4e4e7'/><rect x='208' y='24' width='368' height='40' rx='8' fill='%23e4e4e7'/><rect x='208' y='84' width='368' height='132' rx='10' fill='%23c7d2fe'/></svg>"

const blocks: ReadonlyArray<Block> = [
	makeBlock("o1", "heading2", [text("Your first week")]),
	makeBlock("o2", "paragraph", [text("Everything you need to ship your first change by Friday.")]),
	makeBlock("o3", "toggle", [text("Get access")], { collapsed: true }),
	makeBlock("o4", "todo", [text("Sign in to the workspace with your work email")], { indent: 1, checked: true }),
	makeBlock("o5", "todo", [text("Ask Priya Nair for the billing project")], { indent: 1, checked: false }),
	makeBlock("o6", "toggle", [text("Run the app locally")]),
	makeBlock("o7", "code", [text("bun install\nbun run dev --port 5400")], { indent: 1, language: "bash" }),
	makeBlock("o8", "image", [text("The dashboard you'll land on")], { image: { src: screenshot, alt: "The dashboard: a sidebar and a list of projects", width: 600, height: 240 } }),
	makeBlock("o9", "callout", [text("Stuck for more than an hour? Post in the team channel. Someone will pair with you.")], { color: "blue" }),
]

export const Onboarding = <M>(h: Html.H<M>) => BlocksView(h, { blocks, label: "Your first week", style: styles.page })
```

## Keyboard and accessibility

- Headings are real `h2` to `h4`, code is `pre` and `code`, quotes are `blockquote`, and links are real links.
- A to-do's box is a disabled checkbox named Done, so screen readers say whether it's checked. Nothing in it is editable or focusable except its links.

## Installation

```sh
pnpm dlx shadcn@latest add checkfu/uicontext/document-blocks
```

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

It writes these files:

- `src/ui/uicontext/components/Blocks.ts`
- `src/ui/uicontext/components/EditorBlocks.ts`
- `src/ui/uicontext/components/Glyph.ts`
- `src/ui/uicontext/components/IconStroke.ts`
- `src/ui/uicontext/components/InlineNotice.ts`
- `src/ui/uicontext/components/Sx.ts`
- `src/ui/uicontext/components/domain/Format.ts`
- `src/ui/uicontext/components/domain/Glyph.ts`
- `src/ui/uicontext/components/domain/Record.ts`
- `src/ui/uicontext/components/editor.stylex.ts`
- `src/ui/uicontext/components/tokens.stylex.ts`
- `src/ui/uicontext/components/touch.stylex.ts`

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

## Source

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

## Used in

- [Task manager](https://uicontext.com/blocks/issue-tracker.md)
- [Company brain](https://uicontext.com/blocks/doc-workspace.md)
- [Email](https://uicontext.com/blocks/email.md)
- [Team chat](https://uicontext.com/blocks/team-chat.md)

## Related

- [Editor](https://uicontext.com/docs/components/editor.md) (`editor`): A block editor with a slash menu for headings, lists, to-dos and more.
- [Markdown](https://uicontext.com/docs/components/markdown.md) (`markdown`): Renders Markdown with readable type.
- [Code Block](https://uicontext.com/docs/components/code-block.md) (`code-block`): Code to read and copy, with highlighting, a file name and line numbers.
- [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.
