Skip to content
Alea0.1

Forms

MarkdownEditor

Write Markdown with a toolbar and shortcuts, preview it, and render it safely anywhere.

Ant Design: Mentions / third-party editors

import { MarkdownEditor, Markdown } from '@alea/ui';

Examples

Write and preview

Toolbar, ⌘B ⌘I ⌘K shortcuts, a countdown and a safe preview.

Markdown works: **bold**, _italic_, lists and links.
399 characters left

Render Markdown

Markdown builds React elements, so HTML and javascript: links are ignored.

Safe by design

A normal link works, a script link) does not.

<img src=x onerror=alert(1)> stays text.

npm i @alea/ui

Props

MarkdownEditor props

Write Markdown with a formatting toolbar and shortcuts, and preview it (Mantine RichTextEditor, GitHub comment box).

PropTypeDefaultDescription
labelrequiredReactNode-The visible label.
classNamestring-Extra class names for the root element.
defaultValuestring''The Markdown to start with.
descriptionReactNode-Help text shown under the field.
maxLengthnumber-Shows a character count; with maxLength it counts down.
onChange((v: string) => void)-Called with the Markdown on every change.
placeholderstring'Write in Markdown…'Shown while there is no value.
rowsnumber8Rows of the text area. Default 8.
valuestring-The Markdown (controlled).

Markdown props

PropTypeDefaultDescription
sourcerequiredstring-The Markdown text.
classNamestring-Extra class names.

Accessibility

  • A labelled text area; toolbar buttons name their shortcuts.
  • Markdown is rendered as React elements, never as an HTML string, so pasted HTML or javascript: links do nothing.

Keyboard

KeyDoes
⌘B / Ctrl+BBold
⌘I / Ctrl+IItalic
⌘K / Ctrl+KLink

Do and don't

Do

  • Show the character limit when there is one.
  • Offer the preview before people submit.

Don't

  • Don't render Markdown from people with dangerouslySetInnerHTML: use <Markdown>.
  • Don't hide the toolbar shortcuts: they are in the button names.
Tested with axe, keyboard and screen reader roles