Prose

Slant / Content

Styles for rendered markdown or HTML, and an element map for any renderer that sends code fences to CodeBlock, tables to Table's look, keys to Kbd and rules to Separator.

Demo

Usage

@tuttn/slant
import ReactMarkdown from 'react-markdown'
import { Kbd, Prose, createProseComponents, proseComponents } from '@tuttn/slant'

// A renderer's output, with the element map: fences become CodeBlocks.
<Prose><ReactMarkdown components={proseComponents}>{answer}</ReactMarkdown></Prose>

// Smaller type for a chat answer or a side panel; larger, with the line
// length capped near 70 characters, for an article.
<Prose size="sm"><ReactMarkdown components={proseComponents}>{reply}</ReactMarkdown></Prose>
<Prose size="lg" measure><ReactMarkdown components={proseComponents}>{post}</ReactMarkdown></Prose>

// Any JSX. Take the map's table to get its scrolling wrapper.
const { table: Table } = proseComponents
<Prose size="sm">
  <h3>Shortcuts</h3>
  <p>Press <Kbd>⌘K</Kbd> to search.</p>
  <Table><thead><tr><th>Plan</th><th>Seats</th></tr></thead><tbody><tr><td>Team</td><td>10</td></tr></tbody></Table>
</Prose>

// Your own code fences: the component gets { code, language }.
const components = createProseComponents({ code: ({ code, language }) => <Highlighter code={code} language={language} /> })
<Prose><ReactMarkdown components={{ ...components, img: AssetImage }}>{text}</ReactMarkdown></Prose>

Props

PropTypeDefaultDescription
size'sm' | 'md' | 'lg''md'The type scale: `sm` for chat answers and side panels, `md` for body text, `lg` for articles. Headings, spacing, code and tables follow it.
measurebooleanfalseCaps the line length near 70 characters, for long reading.
…restHTMLAttributes<HTMLDivElement>—className, style, the ref and other attributes reach the root <div class="sl-prose">.

Keyboard

Tabmoves through the links and controls in the text, a CodeBlock’s copy button and code, and a table or plain code block while it is too wide to show
← →on a focused table or code block, scrolls it sideways
Enteron a link, follows it

Accessibility

  • Prose adds no roles: headings, lists, quotes, tables and links keep their own semantics, so a screen reader moves through rendered markdown by heading and list as it would any page.
  • A table that is too wide scrolls inside its wrapper, which joins the tab order (tabIndex 0) only while it overflows, so the keyboard can scroll it and no stop is wasted otherwise. When the table has a <caption>, the scrolling wrapper is a region named by it. A plain code block takes focus the same way; a CodeBlock’s code always does.
  • A rule (---) is Slant’s Separator with role="separator", so it is heard as a break between sections. Keys (kbd) are Slant’s Kbd.
  • A task-list box (- [x] with remark-gfm) is a disabled checkbox, since the source decides it, named by the text that follows it in its item.
  • Links opening a new tab (target="_blank") get rel="noreferrer". Links are underlined as well as coloured, and take the focus ring; every text colour holds 4.5:1 in every theme.
  • Code reads left to right under dir="rtl": inline code and code blocks are dir="ltr".

The element map

proseComponents is an element map for any renderer with a components prop, such as react-markdown or MDX. createProseComponents(options) builds one; proseComponents is createProseComponents({ code: ProseCode }). Every entry drops the node prop react-markdown passes and sends every other attribute to its element. Spread it to replace an entry, as the site does with its own images.

  • pre, a fenced block: reads the language from its code child’s language-* class and the text without its final line break, and renders options.code with { code, language }. ProseCode draws a CodeBlock with a copy button. Without options.code, a plain pre.sl-prose-pre keeps the code element, its class and any spans a highlighting plugin added.
  • code, inline: code.sl-prose-code.
  • table: inside div.sl-prose-table, which scrolls it sideways, drawn in Table’s look.
  • kbd: Kbd. hr: Separator, not decorative. img: loading="lazy" and decoding="async". input: a task box, disabled, .sl-prose-check. a: unchanged, but for rel="noreferrer" on a new tab.

Slant ships no parser, and nothing in Prose depends on one; there is no remark-gfm here either, so markdown tables and task lists need your renderer’s plugin. The map’s type, ProseComponents, is assignable to react-markdown’s components.

Styling bare elements

This is the one place Slant styles elements without its classes, since rendered markdown has none: paragraphs, headings h1 to h6, lists (nested and task lists), quotes, inline code and plain code blocks, tables, images, rules, dl, details and figure. Every such rule reaches only inside a Slant root and has no specificity, so any class of your own wins. None of them reaches into a Slant component inside the prose: an element with an sl-* class (other than Prose’s own sl-prose-*) and everything inside it keep their own look, so a CodeBlock, Kbd, Table or Separator measures the same in Prose as outside it. A component’s root still takes its place in the flow.

Blocks are spaced from their siblings, more above a heading and less under one, so the first and last have no outer margin and Prose sits flush in whatever holds it. A Prose inside a Slant component that is itself inside a Prose is left unstyled; nest Prose directly, or not at all.

Sizes

size picks the type scale: sm (13 px text) for chat answers and side panels, md (14 px) for body text, lg (18 px) for articles. Headings, spacing, code and tables follow it. measure caps the line length near 70 characters.

On the site

The site’s Markdown renders <Prose className="markdown-content"> with createProseComponents(), the plain code default: its pages sit outside a Slant root, so Prose’s rules don’t apply there and the site’s own stylesheet draws the text. Prose imports nothing but React, Kbd and Separator, so it costs the site’s first load almost nothing.

Direction

Lists and quotes indent from the inline start, so under dir="rtl" they indent from the right, the quote’s hairline moves to the right, and a task box hangs on the right. Code stays left to right.