Markdown Editor
React markdown editor with a toolbar that wraps the selection, shortcuts, write, split and preview modes, a live preview without innerHTML and a word count.
Installation
npx shadcn@latest add @minidev/markdown-editor
MiniDev UI is in the shadcn registry directory, so the short form works with no setup. Older CLI versions can use the full URL: npx shadcn@latest add https://ui.minidev.pro/r/markdown-editor.json
Usage
import { MarkdownEditor } from "@/components/ui/markdown-editor"
export function Example() {
return (
<MarkdownEditor />
)
}
Exports MarkdownEditor MarkdownPreview
API reference
| Prop | Type | Default |
|---|---|---|
| valueMarkdown text (controlled). | string | - |
| defaultValueInitial markdown when uncontrolled. Defaults to sample release notes. | string | SAMPLE |
| onChangeCalled with the full text on every edit and toolbar action. | (value: string) => void | - |
| modeView (controlled): write shows the textarea, preview the rendered result, split both. | MarkdownEditorMode | - |
| defaultModeInitial view when uncontrolled. | MarkdownEditorMode | "split" |
| onModeChangeCalled when the view changes. | (mode: MarkdownEditorMode) => void | - |
| labelAccessible name for the textarea. | string | "Release notes" |
| placeholderShown in the empty textarea. | string | "Write in markdown…" |
| minHeightEditor and preview height in px before the textarea is resized. | number | 300 |
| disabled | boolean | - |
| className | string | - |
| nameSubmits the markdown under this name in a hidden input, so it is sent in every mode, including preview. No name by default. | string | - |
When to use
Use it for release notes, comments, descriptions and docs fields. For rendering markdown only use MarkdownRenderer (or the exported MarkdownPreview), for rich text without markdown syntax use RichTextToolbar, and for chat input use PromptInput. Works controlled (value + onChange) or uncontrolled (defaultValue).
Keyboard interactions
| Key | Action |
|---|---|
| CmdBorCtrlB | In the textarea, wraps or unwraps the selection in bold. |
| CmdIorCtrlI | In the textarea, wraps or unwraps the selection in italics. |
| CmdEorCtrlE | In the textarea, wraps or unwraps the selection as inline code. |
| CmdKorCtrlK | In the textarea, inserts a link and selects the part to edit. The key does not reach a global command palette. |
| ArrowLeftorArrowRight | In the formatting toolbar, moves between tools and wraps at the ends. |
| HomeorEnd | In the formatting toolbar, moves to the first or last tool. |
Accessibility
- The tools sit in a
role="toolbar"named "Formatting" with one Tab stop. Each tool's name includes its shortcut, such as "Bold (⌘B)" on a Mac or "Bold (Ctrl+B)" elsewhere. - Bold, Italic and Code get
aria-pressed="true"when the selection is already wrapped in that mark. - The textarea is named by a visually hidden
<label>fromlabel(default "Release notes"). - The preview is a
role="region"named "Preview". It is not a live region, so it is not re-read on every keystroke.
Frequently asked questions
Why does the editor start with release notes in it?
defaultValue defaults to sample text. Pass defaultValue="", or control it with value and onChange.
Is the preview safe from XSS?
The preview builds React elements and never sets innerHTML, so raw HTML shows as text. Links are kept only for http(s), mailto:, /, # and ./ or ../ paths; any other target, such as javascript:, keeps its text but loses the link. External links open in a new tab with rel="noopener noreferrer".
Which Markdown does it support?
Headings, paragraphs, bold, italic, strikethrough, inline code, fenced code blocks, quotes, bulleted and numbered lists, horizontal rules and links. Tables, images and task lists are not parsed, and indented list items are flattened into one level.
Does it submit with a form?
Pass name and the markdown is submitted under it. The value travels in a hidden input rather than on the textarea, so it is sent in preview mode too, when the textarea is not rendered.
Dependencies
npm
Built from
Examples
Controlled value
value and onChange make the text yours, so save and discard buttons can compare it with what is stored.
Preview only
MarkdownPreview is exported on its own for rendering stored markdown, built from React elements and never from HTML strings.
Empty, write mode
An empty defaultValue with a placeholder is the starting point for a comment box; the preview is one click away.