|
| 1 | +--- |
| 2 | +componentId: markdown_editor |
| 3 | +title: MarkdownEditor |
| 4 | +status: Draft |
| 5 | +description: Full-featured Markdown input. |
| 6 | +storybook: '/react/storybook?path=/story/forms-markdowneditor--default' |
| 7 | +--- |
| 8 | + |
| 9 | +```js |
| 10 | +import {MarkdownEditor} from '@primer/react/drafts' |
| 11 | +``` |
| 12 | + |
| 13 | +`MarkdownEditor` is a full-featured editor for GitHub Flavored Markdown, with support for: |
| 14 | + |
| 15 | +- Formatting (keyboard shortcuts & toolbar buttons) |
| 16 | +- File uploads (drag & drop, paste, click to upload) |
| 17 | +- Inline suggestions (emojis, `@` mentions, and `#` references) |
| 18 | +- Saved replies |
| 19 | +- Markdown pasting (ie, paste URL onto selected text to create a link) |
| 20 | +- List editing (create a new list item on `Enter`) |
| 21 | +- Indenting selected text |
| 22 | + |
| 23 | +## Examples |
| 24 | + |
| 25 | +### Minimal Example |
| 26 | + |
| 27 | +A `Label` is always required for accessibility: |
| 28 | + |
| 29 | +```javascript live noinline drafts |
| 30 | +const renderMarkdown = async (markdown) => { |
| 31 | + // In production code, this would make a query to some external API endpoint to render |
| 32 | + return "Rendered Markdown." |
| 33 | +} |
| 34 | + |
| 35 | +const MinimalExample = () => { |
| 36 | + const [value, setValue] = React.useState('') |
| 37 | + |
| 38 | + return ( |
| 39 | + <MarkdownEditor |
| 40 | + value={value} |
| 41 | + onChange={setValue} |
| 42 | + onRenderPreview={renderMarkdown} |
| 43 | + > |
| 44 | + <MarkdownEditor.Label>Minimal Example</MarkdownEditor.Label> |
| 45 | + </MarkdownEditor> |
| 46 | + ) |
| 47 | +} |
| 48 | + |
| 49 | +render(MinimalExample) |
| 50 | +``` |
| 51 | + |
| 52 | +### Suggestions, File Uploads, and Saved Replies |
| 53 | + |
| 54 | +```javascript live noinline drafts |
| 55 | +const renderMarkdown = async (markdown) => "Rendered Markdown." |
| 56 | + |
| 57 | +const uploadFile = async (file) => ({ |
| 58 | + url: `https://example.com/${encodeURIComponent(file.name)}`, |
| 59 | + file |
| 60 | +}) |
| 61 | + |
| 62 | +const emojis = [ |
| 63 | + {name: '+1', character: '👍'}, |
| 64 | + {name: '-1', character: '👎'}, |
| 65 | + {name: 'heart', character: '❤️'}, |
| 66 | + {name: 'wave', character: '👋'}, |
| 67 | + {name: 'raised_hands', character: '🙌'}, |
| 68 | + {name: 'pray', character: '🙏'}, |
| 69 | + {name: 'clap', character: '👏'}, |
| 70 | + {name: 'ok_hand', character: '👌'}, |
| 71 | + {name: 'point_up', character: '☝️'}, |
| 72 | + {name: 'point_down', character: '👇'}, |
| 73 | + {name: 'point_left', character: '👈'}, |
| 74 | + {name: 'point_right', character: '👉'}, |
| 75 | + {name: 'raised_hand', character: '✋'}, |
| 76 | + {name: 'thumbsup', character: '👍'}, |
| 77 | + {name: 'thumbsdown', character: '👎'} |
| 78 | +] |
| 79 | + |
| 80 | +const references = [ |
| 81 | + {id: '1', titleText: 'Add logging functionality', titleHtml: 'Add logging functionality'}, |
| 82 | + { |
| 83 | + id: '2', |
| 84 | + titleText: 'Error: `Failed to install` when installing', |
| 85 | + titleHtml: 'Error: <code>Failed to install</code> when installing' |
| 86 | + }, |
| 87 | + {id: '3', titleText: 'Add error-handling functionality', titleHtml: 'Add error-handling functionality'} |
| 88 | +] |
| 89 | + |
| 90 | +const mentionables = [ |
| 91 | + {identifier: 'monalisa', description: 'Monalisa Octocat'}, |
| 92 | + {identifier: 'github', description: 'GitHub'}, |
| 93 | + {identifier: 'primer', description: 'Primer'} |
| 94 | +] |
| 95 | + |
| 96 | +const savedReplies = [ |
| 97 | + {name: 'Duplicate', content: 'Duplicate of #'}, |
| 98 | + {name: 'Welcome', content: 'Welcome to the project!\n\nPlease be sure to read the contributor guidelines.'}, |
| 99 | + {name: 'Thanks', content: 'Thanks for your contribution!'} |
| 100 | +] |
| 101 | + |
| 102 | +const MinimalExample = () => { |
| 103 | + const [value, setValue] = React.useState('') |
| 104 | + |
| 105 | + return ( |
| 106 | + <MarkdownEditor |
| 107 | + value={value} |
| 108 | + onChange={setValue} |
| 109 | + onRenderPreview={renderMarkdown} |
| 110 | + |
| 111 | + onUploadFile={uploadFile} |
| 112 | + |
| 113 | + emojiSuggestions={emojis} |
| 114 | + referenceSuggestions={references} |
| 115 | + mentionSuggestions={mentionables} |
| 116 | + |
| 117 | + savedReplies={savedReplies} |
| 118 | + > |
| 119 | + <MarkdownEditor.Label>Suggestions, File Uploads, and Saved Replies Example</MarkdownEditor.Label> |
| 120 | + </MarkdownEditor> |
| 121 | + ) |
| 122 | +} |
| 123 | + |
| 124 | +render(MinimalExample) |
| 125 | +``` |
| 126 | + |
| 127 | +### Custom Buttons |
| 128 | + |
| 129 | +```javascript live noinline drafts |
| 130 | +const renderMarkdown = async (markdown) => "Rendered Markdown." |
| 131 | + |
| 132 | +const MinimalExample = () => { |
| 133 | + const [value, setValue] = React.useState('') |
| 134 | + |
| 135 | + return ( |
| 136 | + <MarkdownEditor |
| 137 | + value={value} |
| 138 | + onChange={setValue} |
| 139 | + onRenderPreview={renderMarkdown} |
| 140 | + > |
| 141 | + <MarkdownEditor.Label visuallyHidden>Custom Buttons</MarkdownEditor.Label> |
| 142 | + |
| 143 | + <MarkdownEditor.Toolbar> |
| 144 | + <MarkdownEditor.ToolbarButton icon={SquirrelIcon} aria-label="Custom button 1" /> |
| 145 | + <MarkdownEditor.DefaultToolbarButtons /> |
| 146 | + <MarkdownEditor.ToolbarButton icon={BugIcon} aria-label="Custom button 2" /> |
| 147 | + </MarkdownEditor.Toolbar> |
| 148 | + |
| 149 | + <MarkdownEditor.Actions> |
| 150 | + <MarkdownEditor.ActionButton variant="danger"> |
| 151 | + Cancel |
| 152 | + </MarkdownEditor.ActionButton> |
| 153 | + <MarkdownEditor.ActionButton variant="primary"> |
| 154 | + Submit |
| 155 | + </MarkdownEditor.ActionButton> |
| 156 | + </MarkdownEditor.Actions> |
| 157 | + </MarkdownEditor> |
| 158 | + ) |
| 159 | +} |
| 160 | + |
| 161 | +render(MinimalExample) |
| 162 | +``` |
0 commit comments