Navigation

v2.1.0
Building

Rich text

Put Tiptap in the editor frame: a fixed toolbar, the states of a field and the document as data.

On this page

Master UI has no editor of its own. Editing formatted text, the document model, the selection, the history, pasting and an input method's composition, is a large engine's work, and good ones exist. What Master UI gives such an engine is a frame: the editor contract draws the well, the toolbar band, the states and what the document holds, so a rich text field looks and behaves like every other field on the page. This guide puts Tiptap in it, from plain JavaScript.

Rich text

<div class="grid gap-lg w-full max-w(720) mx-auto" data-example="rich-text">  <div class="field">    <span id="rich-text-label">Release notes</span>    <div class="editor">      <div class="toolbar editor-toolbar" role="toolbar" aria-label="Formatting" aria-controls="rich-text-document">        <select class="select select-sm" data-command="style" aria-label="Text style">          <option value="paragraph">Paragraph</option>          <option value="heading">Heading</option>          <option value="subheading">Subheading</option>        </select>        <span class="toolbar-separator" role="separator" aria-orientation="vertical"></span>        <button type="button" class="btn btn-quiet btn-sm btn-square" data-command="bold" aria-label="Bold" aria-keyshortcuts="Control+B Meta+B" aria-pressed="false">          <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">            <path d="M4.6667 3H8.3333C12.3333 3 12.3333 7.6667 8.3333 7.6667H4.6667M4.6667 3V13H9C13.3333 13 13.3333 7.6667 9 7.6667" />          </svg>        </button>        <button type="button" class="btn btn-quiet btn-sm btn-square" data-command="italic" aria-label="Italic" aria-keyshortcuts="Control+I Meta+I" aria-pressed="false">          <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">            <path d="M6.6667 3L12.6667 3" />            <path d="M3.3333 13L9.3333 13" />            <path d="M9.6667 3L6.3333 13" stroke-linecap="butt" />          </svg>        </button>        <button type="button" class="btn btn-quiet btn-sm btn-square" data-command="underline" aria-label="Underline" aria-keyshortcuts="Control+U Meta+U" aria-pressed="false">          <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">            <path d="M4.3333 2.6667V7.6667C4.3333 12.3333 11.6667 12.3333 11.6667 7.6667V2.6667" />            <path d="M3 14L13 14" />          </svg>        </button>        <span class="toolbar-separator" role="separator" aria-orientation="vertical"></span>        <button type="button" class="btn btn-quiet btn-sm btn-square" data-command="bulletList" aria-label="Bulleted list" aria-pressed="false">          <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">            <circle cx="3" cy="4" r="0.5" fill="currentColor" stroke="none" />            <path d="M6 4L13.5 4" />            <circle cx="3" cy="8" r="0.5" fill="currentColor" stroke="none" />            <path d="M6 8L13.5 8" />            <circle cx="3" cy="12" r="0.5" fill="currentColor" stroke="none" />            <path d="M6 12L13.5 12" />          </svg>        </button>        <button type="button" class="btn btn-quiet btn-sm btn-square" data-command="orderedList" aria-label="Numbered list" aria-pressed="false">          <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">            <path d="M2 3.3333L3 2.6667V6M2 6H4" stroke-width="0.8" />            <path d="M2 9.6667C2 8.3333 4 8.3333 4 9.6667C4 10.6667 2 11 2 12.6667H4" stroke-width="0.8" />            <path d="M6.6667 4.6667L14 4.6667" />            <path d="M6.6667 11.3333L14 11.3333" />          </svg>        </button>        <button type="button" class="btn btn-quiet btn-sm btn-square" data-command="blockquote" aria-label="Quotation" aria-pressed="false">          <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">            <path d="M2.7667 3.3333H6.2333Q6.6667 3.3333 6.6667 3.7667V8.6667C6.6667 11.3333 4.6667 12.6667 2.3333 12.6667V10.6667C4 10.6667 4.3333 9.6667 4.3333 8H2.7667Q2.3333 8 2.3333 7.5667V3.7667Q2.3333 3.3333 2.7667 3.3333Z" />            <path d="M9.7667 3.3333H13.2333Q13.6667 3.3333 13.6667 3.7667V8.6667C13.6667 11.3333 11.6667 12.6667 9.3333 12.6667V10.6667C11 10.6667 11.3333 9.6667 11.3333 8H9.7667Q9.3333 8 9.3333 7.5667V3.7667Q9.3333 3.3333 9.7667 3.3333Z" />          </svg>        </button>        <span class="toolbar-separator" role="separator" aria-orientation="vertical"></span>        <button type="button" class="btn btn-quiet btn-sm btn-square" data-command="link" popovertarget="rich-text-link" aria-label="Link" aria-pressed="false">          <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">            <path d="M9 4.3333L10 3.3333C11.0125 2.3208 12.6541 2.3208 13.6667 3.3333C14.6792 4.3459 14.6792 5.9875 13.6667 7L11.6667 9C10.6541 10.0125 9.0125 10.0125 8 9" />            <path d="M9 4.3333L10 3.3333C11.0125 2.3208 12.6541 2.3208 13.6667 3.3333C14.6792 4.3459 14.6792 5.9875 13.6667 7L11.6667 9C10.6541 10.0125 9.0125 10.0125 8 9" transform="rotate(180 8 8)" />          </svg>        </button>        <span class="toolbar-separator" role="separator" aria-orientation="vertical"></span>        <button type="button" class="btn btn-quiet btn-sm btn-square" data-command="undo" aria-label="Undo" aria-keyshortcuts="Control+Z Meta+Z">          <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">            <path d="M6 3L3.1414 5.8586A0.2 0.2 0 0 0 3.1414 6.1414L6 9" />            <path d="M2.715 6.4108A0.4996 0.4996 0 0 0 3 6.5H9.5A2.9999 2.9999 0 0 1 10.6043 12.2894Q10.0722 12.5 9.5 12.5L5.9755 12.5006A0.5 0.5 0 0 0 5.9266 13.4946L6 13.5H9.5A3.9999 3.9999 0 0 0 10.9724 5.7808Q10.263 5.5 9.5 5.5L2.9621 5.5014A0.5007 0.5007 0 0 0 2.715 5.5892A0.7044 0.7044 0 0 0 2.715 6.4108Z" fill="currentColor" stroke="none" fill-rule="evenodd" />          </svg>        </button>        <button type="button" class="btn btn-quiet btn-sm btn-square" data-command="redo" aria-label="Redo" aria-keyshortcuts="Control+Shift+Z Meta+Shift+Z">          <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">            <path d="M6 3L3.1414 5.8586A0.2 0.2 0 0 0 3.1414 6.1414L6 9" transform="translate(16 0) scale(-1 1)" />            <path d="M2.715 6.4108A0.4996 0.4996 0 0 0 3 6.5H9.5A2.9999 2.9999 0 0 1 10.6043 12.2894Q10.0722 12.5 9.5 12.5L5.9755 12.5006A0.5 0.5 0 0 0 5.9266 13.4946L6 13.5H9.5A3.9999 3.9999 0 0 0 10.9724 5.7808Q10.263 5.5 9.5 5.5L2.9621 5.5014A0.5007 0.5007 0 0 0 2.715 5.5892A0.7044 0.7044 0 0 0 2.715 6.4108Z" fill="currentColor" stroke="none" fill-rule="evenodd" transform="translate(16 0) scale(-1 1)" />          </svg>        </button>      </div>      <div class="flex flex-col" data-editor></div>    </div>    <p class="field-hint" id="rich-text-hint">Headings, lists, quotations and links. Pasted text keeps its structure, not its styling.</p>    <p class="field-error" id="rich-text-error">Write at least a sentence before you publish.</p>  </div>  <form class="popover grid gap-sm w(288)" id="rich-text-link" popover aria-label="Link">    <label class="field">Address      <input class="input input-sm" type="url" name="href" required placeholder="https://example.com" autocomplete="url">    </label>    <div class="flex justify-end gap-xs">      <button type="button" class="btn btn-sm btn-quiet" data-command="unlink">Remove link</button>      <button type="submit" class="btn btn-sm btn-primary">Apply</button>    </div>  </form>  <div class="flex flex-wrap items-center gap-sm">    <label class="check">      <input type="checkbox" role="switch" class="switch" data-state="readonly">Read only</label>    <label class="check">      <input type="checkbox" role="switch" class="switch" data-state="disabled">Disabled</label>    <span class="grow(1)"></span>    <button type="button" class="btn btn-sm" data-command="replace">Load other notes</button>    <button type="button" class="btn btn-sm" data-command="reset">Reset</button>  </div>  <div class="accordion">    <details class="accordion-item">      <summary class="accordion-trigger">The value, as JSON        <svg class="icon accordion-chevron" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">          <path d="M6 3.5L10.3586 7.8586A0.2 0.2 0 0 1 10.3586 8.1414L6 12.5" transform="rotate(90 8 8)" />        </svg>      </summary>      <div class="accordion-content"> <pre class="code-block max-h(240)" tabindex="0" role="region" aria-label="The value, as JSON"><code data-value></code></pre>      </div>    </details>  </div></div>
Write, format with the toolbar or the shortcuts, add a link, switch the states and open the value.

#Who does what

Owns
TiptapThe document and its schema, the selection, undo and redo, the keys and shortcuts, pasting, input methods, and the element it makes editable
editorThe frame and its states, the toolbar band, the size of the document, and the look of its paragraphs, headings, lists, quotations, links and rules
toolbar and its behaviorOne tab stop for the controls, the arrow keys between them, and pressed buttons
Your pageConnecting the two: which command a button runs, what its state is, and where the value goes

@master/ui depends on no editor, and no React, Svelte or Vue component wraps one. The example is the site's own, with Tiptap as the site's dependency: copy it, or follow it with ProseMirror, Lexical or another engine. The contract does not change.

#Put the engine in the frame

Tiptap makes an element editable and writes the document into it. Tell it to put editor-document on that element, with the role, the name and the description a field's control has, and to write the editor's classes on the nodes it renders: the editor stylesheet never selects an element, so a heading is styled because it carries editor-heading, not because it is an h2. Inline code and a block of code use the library's own code classes.

main.js
const editor = new Editor({  element: root.querySelector('[data-editor]'),  content: notes,  extensions: [    StarterKit.configure({      heading: { levels: [2, 3] },      paragraph: { HTMLAttributes: { class: 'editor-paragraph' } },      bulletList: { HTMLAttributes: { class: 'editor-list' } },      orderedList: { HTMLAttributes: { class: 'editor-list editor-list-numbered' } },      listItem: { HTMLAttributes: { class: 'editor-item' } },      blockquote: { HTMLAttributes: { class: 'editor-quote' } },      code: { HTMLAttributes: { class: 'code' } },      codeBlock: { HTMLAttributes: { class: 'code-block' } },      horizontalRule: { HTMLAttributes: { class: 'editor-rule' } },      link: { openOnClick: false, autolink: true, defaultProtocol: 'https', HTMLAttributes: { class: 'editor-link' } },    }),    headingClass,  ],  editorProps: {    attributes: { class: 'editor-document', role: 'textbox', 'aria-multiline': 'true', 'aria-labelledby': 'rich-text-label', 'aria-describedby': 'rich-text-hint rich-text-error' },  },})

A heading renders no attribute for its level, so a small extension, headingClass in the example, gives each level its class: editor-heading for the second and editor-subheading for the third. The document grows from --editor-rows lines, six by default, to --editor-max-rows, then scrolls.

#Wire the toolbar

The toolbar is a toolbar with editor-toolbar, laid flat in the top of the well. Its behavior makes it one tab stop that the arrow keys move through, and presses a button with aria-pressed; the page decides what each button does and what it says.

  • A press runs a command and hands focus back. editor.chain().focus().toggleBold().run(): the reader returns to the document, where they were writing, with the selection Tiptap kept.
  • The selection decides what is pressed. On every transaction, each button's aria-pressed is editor.isActive(...), so Bold reads as pressed wherever the caret stands in bold text, and the text style select shows the block under the caret.
  • What cannot run is disabled. editor.can() dry-runs a command: Undo is disabled when there is nothing to undo, and every control when the editor is read only or disabled.
  • A link is a small form, not a prompt. The link button opens a popover with a URL field. The field's own validation checks the address; Apply puts it on the selection, or inserts it as text where nothing is selected, and Remove takes the link away. Tiptap refuses addresses such as javascript:, and the links take rel="noopener noreferrer nofollow".

Name each icon button and give it aria-keyshortcuts with the shortcut Tiptap already handles, so a screen reader can say both.

#States

StateMarkHow the example sets it
Focus:focus on editor-documentThe browser, when the document has focus; the frame rises with the field's halo
Invalidaria-invalid="true" on editor-documentLeft empty: the frame takes the danger edge and the field's field-error shows, until the reader writes again
Read onlyaria-readonly="true" on editor-documenteditor.setEditable(false) and the attribute; the words can be read and copied
Disabledaria-disabled="true" on editoreditor.setEditable(false), the attribute and every control disabled

The states are attributes Tiptap keeps on its element: set them with editor.setOptions({ editorProps: { attributes } }), never on the element directly, or the next render takes them away.

#The value

Tiptap's document is data. editor.getJSON() is what an application saves, and editor.commands.setContent(json) loads a saved one, as Load other notes and Reset do. The example writes the JSON with textContent: nothing the reader typed is put into the page as markup. Where the content must be shown as HTML outside the editor, render it from the JSON on the server, through a sanitizer.

Listen for update to save or to validate, and for transaction to draw the toolbar; neither changes the document. The page adds no key handler of its own to the document: an input method composing Japanese, Chinese or Korean uses Enter and the arrows, and Tiptap knows when a composition is under way. Validate on blur, not on a key.

#Destroy it

An editor holds listeners, a view and its history. mount() returns a function that detaches the toolbar behavior, removes the page's listeners and calls editor.destroy(); call it when the field leaves the page, as a framework does when its component unmounts.

#What this leaves out

  • A toolbar that follows the selection. A floating toolbar over the selected words needs to track where the selection is drawn, which is Tiptap's BubbleMenu extension and its positioning; this guide keeps the toolbar fixed.
  • Mentions, tables, images and collaboration. Each is a Tiptap extension and its own interface.
  • Markdown in and out. Convert the JSON on the server.