MarkdownEditor
BetaBeta — the props contract is close to final, but small breaking changes can still land.Compose Markdown with draggable blocks, tables, slash commands, people mentions, formatting, and application-owned image uploads. Choose a rich-text or live-preview engine.
Alternatives
Examples
Selection bubble menu
Replace the fixed toolbar with formatting that floats above selected text: text style, bold, italic, strikethrough, inline code, and links.
Read-only
readOnly locks the document, the toolbar, and task checkboxes while the content stays focusable and selectable.
Composition
Markdown composition
Markdown strings stay application-owned. Both alternatives share these parts. Content includes block drag handles (Alt+ArrowUp/ArrowDown also moves blocks) and table grips for moving, aligning, inserting, and deleting rows and columns. Suggestions adds slash commands and accepts people with persistent profile URLs; mentions save as Markdown links. Supply onUploadImage to enable image paste and drop. isUrlAllowed changes which link and image URLs stay editable (a file-based editor can accept relative links). Rich text renders a TipTap document and normalizes the Markdown it saves. Live preview edits the Markdown text itself, so what you type is what you save. Pass extensions to add editor extensions after the built-in set: TipTap extensions for Rich text, CodeMirror extensions for Live preview, where each table cell gets them too. Both props are read once, when the editor mounts.
- <MarkdownEditor>
- <MarkdownEditorToolbar />
- <MarkdownEditorContent />
- <MarkdownEditorSuggestions />
- <MarkdownEditorUploads />
Selection bubble menu
Use MarkdownEditorBubbleMenu instead of, or alongside, MarkdownEditorToolbar. It appears above non-empty text selections outside code blocks and reuses the toolbar's text style, mark, and link controls; pass children to append custom controls.
- <MarkdownEditor>
- <MarkdownEditorContent />
- <MarkdownEditorBubbleMenu />
- <MarkdownEditorSuggestions />
Installation
Install the Rich text alternative. To adopt another implementation, install its registry item and update the import in your project. Check its data requirements and options in Usage.
npx shadcn@latest add https://control-ui.dev/r/markdown-editor.jsonUsage
"use client";
import { useState } from "react";import { MarkdownEditor, MarkdownEditorContent } from "@/components/control-ui/markdown-editor";import { type MarkdownEditorMention, MarkdownEditorSuggestions } from "@/components/control-ui/markdown-editor/suggestions";import { MarkdownEditorToolbar, MarkdownEditorUploads } from "@/components/control-ui/markdown-editor/toolbar";import type { MarkdownImageUploader } from "@/components/control-ui/markdown-editor/uploads";
export default function ProjectDescription({ initialMarkdown, onChange, uploadImage, people,}: { initialMarkdown: string; onChange: (markdown: string) => void; uploadImage: MarkdownImageUploader; people: readonly MarkdownEditorMention[];}) { const [value, setValue] = useState(initialMarkdown); return ( <MarkdownEditor value={value} onValueChange={(markdown) => { setValue(markdown); onChange(markdown); }} onUploadImage={uploadImage} > <MarkdownEditorToolbar /> <MarkdownEditorContent label="Project description" placeholder="Describe the project…" /> <MarkdownEditorSuggestions mentions={people} /> <MarkdownEditorUploads /> </MarkdownEditor> );}
Dependencies
src/registry/sources/control-ui/markdown-editor/rich/blocks.ts
src/registry/sources/control-ui/markdown-editor/rich/engine.ts
src/registry/sources/control-ui/markdown-editor/rich/extensions.ts
src/registry/sources/control-ui/markdown-editor/rich/tables.ts
src/registry/sources/control-ui/markdown-editor/rich/task-item.tsx
src/registry/sources/control-ui/markdown-editor/rich/uploads.ts
src/registry/sources/control-ui/markdown-editor/bubble-menu.tsx
src/registry/sources/control-ui/markdown-editor/context.ts
src/registry/sources/control-ui/markdown-editor/drag/drag-handle.tsx
src/registry/sources/control-ui/markdown-editor/drag/drag-preview.ts
src/registry/sources/control-ui/markdown-editor/engine.ts
src/registry/sources/control-ui/markdown-editor/root.tsx
src/registry/sources/control-ui/markdown-editor/source-view.tsx
src/registry/sources/control-ui/markdown-editor/suggestions.tsx
src/registry/sources/control-ui/markdown-editor/table/table-controls.tsx
src/registry/sources/control-ui/markdown-editor/table/table-geometry.ts
src/registry/sources/control-ui/markdown-editor/table/table-grip.tsx
src/registry/sources/control-ui/markdown-editor/table/use-table-target.ts
src/registry/sources/control-ui/markdown-editor/toolbar.tsx
src/registry/sources/control-ui/markdown-editor/uploads.ts
src/registry/sources/control-ui/markdown-editor/url-policy.ts
src/registry/sources/control-ui/ui/label.tsx
checkbox
typography
Raw code
Installed source for the Rich text alternative
"use client";
import { useRender } from "@base-ui/react/use-render";import type { Editor, Extensions } from "@tiptap/core";import { EditorContent, useEditor } from "@tiptap/react";import { createContext, useContext, useEffect, useMemo, useRef, useState } from "react";import { useMarkdownEditor } from "./markdown-editor/context";import { MarkdownEditorDragHandle } from "./markdown-editor/drag/drag-handle";import { createRichEngine, pasteMarkdown } from "./markdown-editor/rich/engine";import { createMarkdownCodec, markdownExtensions, requiresMarkdownSource } from "./markdown-editor/rich/extensions";import { type MarkdownEditorContentProps, MarkdownEditorRoot, type MarkdownEditorRootProps } from "./markdown-editor/root";import { MarkdownEditorSourceView } from "./markdown-editor/source-view";import { MarkdownEditorTableControls } from "./markdown-editor/table/table-controls";import { persistentUrlPolicy } from "./markdown-editor/url-policy";
export type { MarkdownEditorContentProps, MarkdownEditorStatus } from "./markdown-editor/root";
export type MarkdownEditorProps = MarkdownEditorRootProps & { /** Extra TipTap extensions, applied after the built-in Markdown set. Read once, when the editor mounts. */ extensions?: Extensions;};
type ContentAttributes = Record<string, string>;const RichEditorContext = createContext<{ editor: Editor | null; setContentAttributes: (attributes: ContentAttributes) => void } | null>( null,);
function useRichEditor() { const context = useContext(RichEditorContext); if (!context) throw new Error("MarkdownEditorContent requires a MarkdownEditor parent from the same module."); return context;}
export function MarkdownEditor({ extensions: extraExtensions, isUrlAllowed: urlPolicy = persistentUrlPolicy, ...props}: MarkdownEditorProps) { const [isUrlAllowed] = useState(() => urlPolicy); const [options] = useState(() => ({ extensions: extraExtensions, isUrlAllowed })); const [codec] = useState(() => createMarkdownCodec(options)); const [extensions] = useState(() => markdownExtensions(options)); const [initialMarkdown] = useState(() => props.value ?? props.defaultValue ?? ""); const [initialContent] = useState(() => (requiresMarkdownSource(initialMarkdown, codec, isUrlAllowed) ? "" : initialMarkdown)); const [contentAttributes, setContentAttributes] = useState<ContentAttributes>({ role: "textbox", "aria-multiline": "true" }); const editor = useEditor({ extensions, immediatelyRender: false, shouldRerenderOnTransaction: false, content: initialContent, contentType: "markdown", editable: !props.disabled && !props.readOnly, editorProps: { attributes: contentAttributes, handlePaste: pasteMarkdown(codec, isUrlAllowed) }, }); const engine = useMemo( () => (editor ? createRichEngine(editor, codec, isUrlAllowed, initialMarkdown) : null), [editor, codec, isUrlAllowed, initialMarkdown], ); return ( <RichEditorContext value={{ editor, setContentAttributes }}> <MarkdownEditorRoot engine={engine} isUrlAllowed={isUrlAllowed} {...props} /> </RichEditorContext> );}
export function MarkdownEditorContent({ label, placeholder, autoFocus = false, ref, ...props }: MarkdownEditorContentProps) { const container = useRef<HTMLDivElement>(null); const { editor, setContentAttributes } = useRichEditor(); const { disabled, readOnly, source } = useMarkdownEditor(); const content = useRender({ ref: [container, ref ?? null], render: ( <div data-control-ui="markdown-editor" data-control-family="markdown-editor" data-slot="content" {...props}> <EditorContent editor={editor} /> <MarkdownEditorDragHandle container={container} /> <MarkdownEditorTableControls container={container} /> </div> ), }); useEffect(() => { setContentAttributes({ role: "textbox", "aria-multiline": "true", "aria-label": label, "aria-disabled": String(disabled), "aria-readonly": String(readOnly), "data-placeholder": placeholder ?? "", }); }, [setContentAttributes, label, disabled, readOnly, placeholder]);
useEffect(() => { if (editor && !editor.isDestroyed && autoFocus && !source && !disabled) editor.commands.focus("end"); }, [editor, autoFocus, source, disabled]);
if (source || !editor) return <MarkdownEditorSourceView ref={ref} label={label} placeholder={placeholder} autoFocus={autoFocus} {...props} />; return content;}
Knobs
Typed custom properties the recipe paints with. Set one on the root — style, a utility class, or a skin — and every slot inherits it.
| Knob | Syntax | Default |
|---|---|---|
--cui-markdown-editor-background | <color> | var(--background) |
--cui-markdown-editor-foreground | <color> | var(--foreground) |
--cui-markdown-editor-border-color | <color> | var(--border) |
--cui-markdown-editor-focus-border-color | <color> | var(--focus-rim) |
--cui-markdown-editor-radius | <length-percentage> | var(--radius-panel) |
--cui-markdown-editor-padding | <length-percentage> | calc(var(--spacing) * 4) |
--cui-markdown-editor-gutter-size | <length-percentage> | var(--control-h-xs) |
--cui-markdown-editor-min-height | <length-percentage> | calc(var(--spacing) * 32) |
--cui-markdown-editor-max-height | <length-percentage> | 60dvh |
--cui-markdown-editor-gap | <length-percentage> | calc(var(--spacing) * 3) |
--cui-markdown-editor-muted-foreground | <color> | var(--muted-foreground) |
--cui-markdown-editor-code-background | <color> | var(--muted) |
--cui-markdown-editor-border-width | <length> | var(--control-rim-width) |
--cui-markdown-editor-table-border-width | <length> | var(--control-rim-width) |
--cui-markdown-editor-table-border-color | <color> | var(--border) |
--cui-markdown-editor-table-selection-background | <color> | var(--active-fill) |
| Knob | Syntax | Default |
|---|---|---|
--cui-label-root-foreground | <color> | var(--foreground) |