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/uiProps
MarkdownEditor props
Write Markdown with a formatting toolbar and shortcuts, and preview it (Mantine RichTextEditor, GitHub comment box).
| Prop | Type | Default | Description |
|---|---|---|---|
labelrequired | ReactNode | - | The visible label. |
className | string | - | Extra class names for the root element. |
defaultValue | string | '' | The Markdown to start with. |
description | ReactNode | - | Help text shown under the field. |
maxLength | number | - | Shows a character count; with maxLength it counts down. |
onChange | ((v: string) => void) | - | Called with the Markdown on every change. |
placeholder | string | 'Write in Markdown…' | Shown while there is no value. |
rows | number | 8 | Rows of the text area. Default 8. |
value | string | - | The Markdown (controlled). |
Markdown props
| Prop | Type | Default | Description |
|---|---|---|---|
sourcerequired | string | - | The Markdown text. |
className | string | - | 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
| Key | Does |
|---|---|
| ⌘B / Ctrl+B | Bold |
| ⌘I / Ctrl+I | Italic |
| ⌘K / Ctrl+K | Link |
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.