Olddevs UI4.14.0
ReferenceRichTextView

Overview

Release 2.4

Search is twice as fast and finds words in attachments. See the migration notes.

  • New filters by owner and date

  • Saved searches

Before you upgrade: rebuild the index.

Query

Before

After

Title

120 ms

55 ms

TypeScript
const hits = await search("invoice", { owner: "me" });

When to use

  • The page shows a Document saved by RichTextEditor: an article, a description, a policy.
  • You need the component in a server component, or where pulling in Tiptap just to read is not acceptable.

When not to use

  • The HTML came from a user and the server has not sanitized it — sanitize first (section "Sanitization on the consumer's server"), then show. The component puts everything it receives into the page.
  • For editing, use RichTextEditor. Its readOnly keeps the engine alive for text that does not change; for a page that only displays the Document, use RichTextView.
  • Arbitrary HTML outside the Schema (style other than the author's color, thead, caption, images without figure): the typography is built for the Schema's tags, and anything else looks like the browser default.
  • For short single-line text, use Text.

Minimal example

In Next.js, the @olddevs/ui/editor entry point must be listed in optimizePackageImports on its own line (getting-started.md): otherwise the whole Editor with its engine ships to the browser along with the View — 435 kB of First Load JS instead of 137 kB.

// Server component: no directive needed.
import { RichTextView } from "@olddevs/ui/editor";

export default async function ArticlePage({ id }: { id: string }) {
  const article = await loadArticle(id); // article.html was sanitized on save
  return <RichTextView html={article.html} className="max-w-prose" />;
}

Public API

  • html — the saved Document, an HTML string. Not sanitized: it goes into the markup through dangerouslySetInnerHTML as is. An empty string renders an empty container.
  • className — added after the typography classes and can override them.
  • surface — "theme" (default) or "paper": Paper, a light area in any theme, see "Surface". The type is RichTextSurface.
  • tools — the Document's own Tools, the same array as for RichTextEditor and sanitizeRichText. The View reads from it the Islands of custom Blocks (blocks[name].island, type CustomIslandProps), the formula options tool("mathematics", { macros }) and the video option tool("youtube", { autoload }) (section "Video"); without tools the View stays as before (section "Custom Block's Island").
  • Everything else is regular div props: id, aria-*, data-*, ref. children and dangerouslySetInnerHTML are not accepted.
  • highlightCodeBlocks(html) — server-side highlighting of code blocks, the @olddevs/ui/editor/highlight entry point (section "Code block highlighting"). RichTextView has no prop for this, on purpose.
  • sanitizeRichText(html, purify, toolsOrOptions?) sanitizes HTML against the Schema using the consumer's DOMPurify instance (RichTextPurifier). Pass the Editor's tools array or a RichTextSanitizeOptions object with tools and allowRelativeLinks. The hook event type is RichTextAttributeHookEvent.
  • isAllowedLinkHref(href, options?) checks a single link destination using the same policy. The RichTextLinkOptions argument supports allowRelativeLinks, which defaults to false.
  • richTextSchema(tools?) — the Schema as data for a set of tools: the RICH_TEXT_SCHEMA fields plus customBlocks and customMarks — the rules of custom Blocks and Marks (RichTextCustomBlockRules, RichTextCustomMarkRules). The type is RichTextSchema. The RICH_TEXT_SCHEMA constant itself is still the kit's Schema.
  • richTextSanitizeConfig() — a restrictive sanitizing config for DOMPurify without hooks: safe on its own, but it loses part of the Editor's markup. The type is RichTextSanitizeConfig.
  • sanitizeRichText, isAllowedLinkHref, richTextSchema and richTextSanitizeConfig are data and functions only, with no dependencies; they are safe in a server module. The lightweight entry point is @olddevs/ui/editor/schema; they are also available from @olddevs/ui/editor.

The typography comes from the same src/editor/styles/prose.ts module that attaches classes to the Editor's content, so text looks the same in the Editor and on the page. The classes sit on the container and find tags by name, so the Document's HTML has no classes or style. Colors come from the kit's tokens, and the dark, light and dim themes switch on their own. A separate Tailwind @source is not needed: the line from getting-started.md covers the classes.

Paper: surface

surface="paper" is Paper, a light area in any app theme, like RichTextEditor: text, links, palettes, highlights, tables, captions and code blocks (GitHub Light) look as in the light theme, and Paper is set off from the page by the app theme's border and shadow. By default ("theme") the Document follows the app theme and does not set a background.

<RichTextView html={cleanHtml} surface="paper" />

When to use Paper: a print or PDF preview, displaying an email or a document that will leave the app in its light look. A Document that is only read inside the app stays on "theme". Everything around Paper — page headings, buttons, menus — stays in the app theme: Paper covers only the RichTextView container.

The component stays a server component: Paper is classes and attributes on the container (data-theme="light", data-surface="paper"), with no style and no client code. The same Document on the Paper looks identical in the Editor and here — a test checks this.

Accessibility

The component adds no roles: it is a regular div with semantic HTML inside — headings, lists, a quote. A Details Block is a native details and summary: opening from the keyboard (Tab, Enter, Space) and announcing the state are the browser's job. The Document's headings start with H2 if the Editor worked without heading1: the page keeps the H1 itself. A Document with heading1 contains its own h1, and then the page does not put in a second one. An H1 in RichTextView is styled with the same typography as in the Editor. If the Document is a separate area of the page, give the container a name through aria-label and role="region".

Anti-patterns

  • Putting a string into html that has not passed server-side sanitizing.
  • Sanitizing only in the browser before sending: the server does not see it.
  • Using RichTextEditor readOnly for a page that only displays the Document: a 295–339 kB gzip engine just to display text (size table).
  • Adding "use client" to a file where RichTextView is the only component: it is not needed and pulls its neighbors into the client graph (the Island with the "Copy" button is a separate file with its own directive, and RichTextView already has it).
  • Passing a server component or a function with a dynamic import to island: only a reference to a client component from a module with "use client" crosses the RSC boundary.
  • Building your own copy button on top of RichTextView or reading text from the Block's DOM: the header and the button are already there, and what gets copied is the code's textContent, not the markup.
  • Source: src/editor/RichTextView.tsx; header and button — src/editor/features/code/codeHeader.ts (header markup), src/editor/features/code/CodeCopyIsland.tsx (client Island), src/editor/features/code/CodeCopyButton.tsx; the Island of a custom Block — src/editor/core/viewIsland.ts (choosing Islands by the Document), src/editor/ui/BlockIsland.tsx and src/editor/ui/BlockIslandSlots.tsx (client Island); Paper (surface="paper") — src/editor/styles/surface.ts and src/editor/styles/tokens.ts; typography — src/editor/styles/prose.ts, highlight theme — src/editor/styles/codeTheme.ts; server highlighting — src/editor/features/code/highlight.ts, entry point — src/editor/highlight-entry.ts; the Schema as data, sanitizeRichText and the sanitizing config — src/editor/core/schema.ts, lightweight entry point — src/editor/schema-entry.ts.
  • Tests: tests/rich-text-view.test.tsx (it also holds the module graph and the table-to-Schema check), tests/rich-text-view-footnotes.test.tsx (footnotes as plain anchors), tests/rich-text-view-code-copy.test.tsx (header, Island, copying), tests/rich-text-view-island.test.tsx (the Island of a custom Block), tests/rich-text-paper.test.tsx and tests/rich-text-paper-css.test.tsx (Paper, surface="paper"), tests/editor-highlight-entry.test.ts (highlight helper, escaping, entry point graph), tests/editor-sanitize.test.tsx (sanitizing with the real DOMPurify: the Editor's markup unchanged, forged input, the bare config), tests/editor-sanitize-tools.test.tsx (sanitizing and richTextSchema with custom Tools), tests/rsc-gate.test.ts.
  • Storybook: Content/RichTextView.
  • The Editor — RichTextEditor; custom Tools, the Island and the semver boundary — Editor extension API; decisions — ADR-0050, highlighting — ADR-0051, extension — ADR-0058.

TypeScript types

RichTextViewProps

Prop

Type

Plus DOM attributes, such as onChange, onClick, className — 277 in total; the table does not repeat them.

On this page