Rich Text Editor

A rich text editor component powered by TipTap, providing customizable formatting tools, form integration, and accessible content editing.

Table of contents

Usage

The Rich Text Editor is suited for content management systems, comment sections, documentation editors, and any application requiring formatted HTML input. Common use cases include:

  • Blog post and article editors
  • User comment systems with formatting
  • Documentation and help systems
  • Email composition interfaces
  • Content management dashboards

Under the hood, this component uses TipTap.

While we have tried our best to secure our component, having a rich text editor is inherently risky. Because it operates on the frontend, input can always be bypassed or tampered with by users. Always sanitize and validate HTML input on the server side.

Once everything is configured, you may use the <z-rte> markup in your HTML code:

CSS

RTE is headless by default. This means there is no built-in typography styling inside the content area, and developers are expected to style prose elements (headings, lists, blockquotes, paragraphs) according to their design system.

You can use the following snippet as a starting point:

.z-rte-body .ProseMirror strong {
  font-weight: 700;
}

.z-rte-body .ProseMirror u {
  --z-u-font-weight: 500;
  --z-u-underline-offset: 4px;

  font-weight: var(--z-u-font-weight);
  text-decoration-line: underline;
  text-underline-offset: var(--z-u-underline-offset);
}

.z-rte-body .ProseMirror ol {
  list-style-type: decimal;
}

.z-rte-body .ProseMirror ul {
  list-style-type: disc;
}

.z-rte-body .ProseMirror ol,
.z-rte-body .ProseMirror ul {
  --z-list-indent: 32px;

  padding-inline-start: var(--z-list-indent);
}

.z-rte .ProseMirror h1 {
  font-size: calc(var(--z-global-font-size) * 3.052);
  font-weight: 800;
  letter-spacing: -0.025em;
  line-height: 1.1;
}

.z-rte .ProseMirror h2 {
  font-size: calc(var(--z-global-font-size) * 2.441);
  font-weight: 700;
  letter-spacing: -0.025em;
  line-height: 1.15;
}

.z-rte .ProseMirror h3 {
  font-size: calc(var(--z-global-font-size) * 1.953);
  font-weight: 700;
  letter-spacing: -0.025em;
  line-height: 1.2;
}

.z-rte .ProseMirror h4 {
  font-size: calc(var(--z-global-font-size) * 1.563);
  font-weight: 600;
  letter-spacing: -0.025em;
  line-height: 1.25;
}

.z-rte .ProseMirror h5 {
  font-size: calc(var(--z-global-font-size) * 1.25);
  font-weight: 600;
  letter-spacing: -0.025em;
  line-height: 1.3;
}

.z-rte .ProseMirror h6 {
  font-size: var(--z-global-font-size);
  font-weight: 600;
  letter-spacing: -0.025em;
  line-height: 1.4;
}

.z-rte .ProseMirror blockquote {
  margin-block-start: calc(var(--z-global-line-height) * 1);
  border-inline-start-width: 2px;
  border-color: color-mix(in srgb, var(--z-border) 10%, transparent);
  padding-inline-start: calc(var(--z-global-line-height) * 1);
  font-style: italic;
}

If you run multiple instances of the RTE component on a single page, you can customize the selectors as well as the cls target. Note that .z-rte targets the wrapping div of the editor, while .ProseMirror targets the actual editor area. You can customize these selectors however you want via the cls tag. Learn more about available cls targets.

Initial content

Set initial HTML content strictly using a <template data-fn="value"> element inside the component:

The template content supports full HTML and is parsed into the editor with formatting preserved. Setting initial content via a value attribute is intentionally unsupported to avoid escaping raw HTML in attributes.

Toolbar

By default, the editor displays all available tools organized into five groups:

  1. Text formatting: bold, italic, underline, strikethrough
  2. Lists: list, list-ordered
  3. Headings: h1, h2, h3, h4
  4. Alignment: align-left, align-right, align-center, align-justify
  5. Blocks: blockquote

Customizing and grouping the toolbar

You can customize which tools appear and how they are grouped using the toolbar attribute or a configuration script.

Using bracket notation

Using pipe or semicolon notation

Using a configuration script

Hiding the toolbar

To render the editor without a toolbar, set toolbar to "none", "false", or "[]":

Available toolbar items

ItemAliasesIcon KeyDescription
bold—boldToggles bold text
italic—italicToggles italic text
underline—underlineToggles underlined text
strikethroughstrikestrikethroughToggles strikethrough text
listbullet-list, bulletListlistToggles a bulleted list
list-orderedordered-list, orderedListlist-orderedToggles a numbered list
h1heading-1heading-1Toggles Heading 1
h2heading-2heading-2Toggles Heading 2
h3heading-3heading-3Toggles Heading 3
h4heading-4heading-4Toggles Heading 4
align-left—align-leftAligns text to the left
align-center—align-centerCenters text
align-right—align-rightAligns text to the right
align-justify—align-justifyJustifies text
blockquotequote, text-quotetext-quoteToggles blockquote formatting

Form integration

When a name attribute is provided, the component automatically renders and syncs a hidden <input type="hidden"> element for form submission:

<form method="POST" action="/save">
  <z-rte name="blog_content" placeholder="Write your blog post..."></z-rte>
  <button type="submit">Publish</button>
</form>

The form will receive the serialized HTML string under the blog_content field name.

Word counter

A live word counter is displayed in the footer of the editor, updating automatically as users type and using localized singular or plural labels based on the current word count.

Icon

This component allows you to customize toolbar icons through the <template data-fn="icons"> system using the icon keys listed in the Available toolbar items table:

Internationalization

The Rich Text Editor component supports internationalization through multiple methods with the following priority order (highest to lowest):

  • Component-level i18n (via i18n attribute or script tag)
  • Global component-specific namespace (via <script id="z-i18n">)
  • Default values

Using the i18n attribute

Using a configuration script

Using global i18n

Place this in your document <head> or before any editor components:

<script id="z-i18n" type="application/json">
  {
    "z-rte": {
      "word-counter-singular": "Wort",
      "word-counter-plural": "Wörter",
      "tooltip-bold": "Fett",
      "tooltip-italic": "Kursiv",
      "tooltip-underline": "Unterstrichen",
      "tooltip-strikethrough": "Durchgestrichen",
      "tooltip-list": "Aufzählung",
      "tooltip-list-ordered": "Nummerierte Liste",
      "tooltip-h1": "Überschrift 1",
      "tooltip-h2": "Überschrift 2",
      "tooltip-h3": "Überschrift 3",
      "tooltip-h4": "Überschrift 4",
      "tooltip-align-left": "Linksbündig",
      "tooltip-align-center": "Zentriert",
      "tooltip-align-right": "Rechtsbündig",
      "tooltip-align-justify": "Blocksatz",
      "tooltip-blockquote": "Zitat",
      "aria-toolbar": "Formatierungsoptionen",
      "aria-editor": "Rich-Text-Editor"
    }
  }
</script>

Available i18n options

KeyDefaultDescription
word-counter-singular'word'Label used when the word count is exactly 1.
word-counter-plural'words'Label used when the word count is 0 or > 1.
tooltip-bold'Bold'Tooltip and aria-label for the bold button.
tooltip-italic'Italic'Tooltip and aria-label for the italic button.
tooltip-underline'Underline'Tooltip and aria-label for the underline button.
tooltip-strikethrough'Strikethrough'Tooltip and aria-label for the strike button.
tooltip-list'List'Tooltip and aria-label for the bullet list.
tooltip-list-ordered'Ordered List'Tooltip and aria-label for the ordered list.
tooltip-h1'Heading 1'Tooltip and aria-label for the H1 button.
tooltip-h2'Heading 2'Tooltip and aria-label for the H2 button.
tooltip-h3'Heading 3'Tooltip and aria-label for the H3 button.
tooltip-h4'Heading 4'Tooltip and aria-label for the H4 button.
tooltip-align-left'Align Left'Tooltip and aria-label for left alignment.
tooltip-align-center'Align Center'Tooltip and aria-label for center alignment.
tooltip-align-right'Align Right'Tooltip and aria-label for right alignment.
tooltip-align-justify'Align Justify'Tooltip and aria-label for justify alignment.
tooltip-blockquote'Blockquote'Tooltip and aria-label for blockquote.
aria-toolbar'Formatting options'Accessible aria-label for the toolbar region.
aria-editor'Rich text editor'Accessible aria-label for the editable area.

Custom classes

The Rich Text Editor component supports custom CSS classes through the cls attribute. This allows you to style specific elements within the editor without modifying the component’s internal structure.

Using simple string format

Apply a class to the default element (editor):

<z-rte cls="prose min-h-48"></z-rte>

Using JSON object format

Target specific elements within the editor:

<z-rte
  cls='{
    "container": "z-rte custom-rte-wrapper",
    "header": "z-rte-header",
    "toolbar": "z-rte-toolbar",
    "toolbar-group": "z-rte-toolbar-group",
    "button": "z-btn z-btn-ghost z-btn-icon z-btn-sm",
    "body": "z-rte-body",
    "editor": "prose max-w-none min-h-40",
    "footer": "z-rte-footer text-muted"
  }'
></z-rte>

Using configuration script

<z-rte>
  <script type="application/json" data-fn="config">
    {
      "cls": {
        "button": "z-btn z-btn-ghost z-btn-icon z-btn-sm",
        "editor": "prose max-w-none min-h-40"
      }
    }
  </script>
</z-rte>

Available cls targets

TargetDefault ClassDescription
containerz-rteOuter wrapper element ([data-part="container"]).
headerz-rte-headerHeader container wrapping the toolbar ([data-part="header"]).
toolbarz-rte-toolbarScrollable toolbar container ([data-part="toolbar"]).
toolbar-groupz-rte-toolbar-groupWrapper around each group of buttons ([data-part="toolbar-group"]).
button""Individual toolbar action buttons ([data-part="button"]).
bodyz-rte-bodyMount container for the editor ([data-part="body"]).
editor""Default target. The editable .ProseMirror content element ([data-part="editor"]).
footerz-rte-footerFooter container showing the word count ([data-part="footer"]).

Custom inline styles

The Rich Text Editor component supports custom inline styles through the stl attribute. This allows you to apply specific CSS styles to individual elements within the component.

Using simple string format

Apply styles to the default element (editor):

<z-rte stl="min-height\: 200px; max-height\: 400px;"></z-rte>

Using JSON object format

Target specific elements with custom inline styles:

<z-rte
  stl='{
    "container": "max-width: 720px;",
    "editor": "min-height: 220px; padding: 1rem;",
    "footer": "text-align: right;"
  }'
></z-rte>

Using configuration script

<z-rte>
  <script type="application/json" data-fn="config">
    {
      "stl": {
        "editor": "min-height: 220px; max-height: 500px;"
      }
    }
  </script>
</z-rte>

Available stl targets

The stl attribute supports the same targets as the cls attribute. See the Available cls targets table above for a complete list of targetable elements.

Error state

To indicate an error state in the form, simply add the .z-form-danger class to the cls attribute. This will apply a “danger” state to the component, providing visual feedback to the user.

  • This field is required.

Preventing layout shift

When loading Web Components, a brief delay may cause a flash of unstyled content. To mitigate this issue, consider setting a predefined height on the parent element to prevent layout shift and ensure a smooth user experience.

<div class="[min-h]" style="--min-h: 7.5rem">
  <z-rte name="content"></z-rte>
</div>

Accessibility

  • WAI-ARIA Roles: The toolbar uses role="toolbar" with grouped role="group" containers, while the editable area uses role="textbox" with aria-multiline="true", aria-required, and aria-disabled.
  • Roving Tabindex: Toolbar buttons implement roving tabindex navigation (ArrowLeft, ArrowRight, Home, and End), allowing keyboard users to tab directly from the toolbar into the editor with a single Tab press.
  • Toggle States: Formatting buttons dynamically update aria-pressed="true|false" based on the current cursor selection.
  • Live Word Count: The footer uses aria-live="polite" and aria-atomic="true" to announce word count updates non-disruptively.

Attributes

The following attributes are available for this component:

NameTypeDefaultDescription
nameString""Form field name for the hidden <input type="hidden"> element.
placeholderString""Placeholder text displayed when the editor is empty.
disabledBooleanfalseDisables the editor and toolbar buttons, preventing content editing.
requiredBooleanfalseMarks the editor as required (aria-required="true").
toolbarString""Customizes or hides the toolbar groups (supports bracket, pipe, or JSON).
clsString""Custom CSS classes for component elements (defaults to targeting editor).
stlString""Custom inline styles for component elements (defaults to targeting editor).
i18nString""Internationalization strings for toolbar tooltips, labels, and word count.
force-prevent-rerenderBooleanfalseForcefully prevents component rerendering after initial mount.

Events

The Rich Text Editor component triggers the following events:

NameDescription
z-rte:inputFired when editor content changes, providing { value: string } in event.detail.