# Rich Text Editor

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

## 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](https://tiptap.dev).

Note: 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:

```html
<!-- Basic editor -->
<z-rte name="content" placeholder="Start writing..."></z-rte>

<!-- With form integration -->
<form>
  <z-rte name="description" placeholder="Enter description..."></z-rte>
  <button type="submit">Save</button>
</form>

```

## 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:

```css
.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;
}
```

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](#available-cls-targets).

## Initial content

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

```html
<z-rte name="article">
  <template data-fn="value">
    <h2>Welcome to the Editor</h2>
    <p>This is some <strong>initial content</strong> with formatting.</p>
    <ul>
      <li>Bulleted lists work</li>
      <li>So do <em>italic</em> and other formatting</li>
    </ul>
  </template>
</z-rte>

```

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

```html
<z-rte toolbar="[[bold, italic, underline], [list, list-ordered], [blockquote]]"></z-rte>

```

#### Using pipe or semicolon notation

```html
<z-rte toolbar="bold, italic | h1, h2 | list, list-ordered"></z-rte>

```

#### Using a configuration script

```html
<z-rte>
  <script type="application/json" data-fn="config">
    {
      "toolbar": [
        ["bold", "italic"],
        ["h1", "h2", "blockquote"]
      ]
    }
  </script>
</z-rte>

```

#### Hiding the toolbar

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

```html
<z-rte toolbar="none" placeholder="Write a comment..."></z-rte>
```

### Available toolbar items

| Item | Aliases | Icon Key | Description |
| --- | --- | --- | --- |
| `bold` | — | `bold` | Toggles bold text |
| `italic` | — | `italic` | Toggles italic text |
| `underline` | — | `underline` | Toggles underlined text |
| `strikethrough` | `strike` | `strikethrough` | Toggles strikethrough text |
| `list` | `bullet-list`, `bulletList` | `list` | Toggles a bulleted list |
| `list-ordered` | `ordered-list`, `orderedList` | `list-ordered` | Toggles a numbered list |
| `h1` | `heading-1` | `heading-1` | Toggles Heading 1 |
| `h2` | `heading-2` | `heading-2` | Toggles Heading 2 |
| `h3` | `heading-3` | `heading-3` | Toggles Heading 3 |
| `h4` | `heading-4` | `heading-4` | Toggles Heading 4 |
| `align-left` | — | `align-left` | Aligns text to the left |
| `align-center` | — | `align-center` | Centers text |
| `align-right` | — | `align-right` | Aligns text to the right |
| `align-justify` | — | `align-justify` | Justifies text |
| `blockquote` | `quote`, `text-quote` | `text-quote` | Toggles 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:

```html
<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](#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:

```html
<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

| Key | Default | Description |
| --- | --- | --- |
| `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`):

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

### Using JSON object format

Target specific elements within the editor:

```html
<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-button z-button-ghost z-button-icon z-button-sm",
    "body": "z-rte-body",
    "editor": "prose max-w-none min-h-40",
    "footer": "z-rte-footer text-muted"
  }'
></z-rte>
```

### Using configuration script

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

### Available cls targets

| Target | Default Class | Description |
| --- | --- | --- |
| `container` | `z-rte` | Outer wrapper element (`[data-part="container"]`). |
| `header` | `z-rte-header` | Header container wrapping the toolbar (`[data-part="header"]`). |
| `toolbar` | `z-rte-toolbar` | Scrollable toolbar container (`[data-part="toolbar"]`). |
| `toolbar-group` | `z-rte-toolbar-group` | Wrapper around each group of buttons (`[data-part="toolbar-group"]`). |
| `button` | `""` | Individual toolbar action buttons (`[data-part="button"]`). |
| `body` | `z-rte-body` | Mount container for the editor (`[data-part="body"]`). |
| `editor` | `""` | **Default target.** The editable `.ProseMirror` content element (`[data-part="editor"]`). |
| `footer` | `z-rte-footer` | Footer 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`):

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

### Using JSON object format

Target specific elements with custom inline styles:

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

### Using configuration script

```html
<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](#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.

## 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.

```html
<div style="min-height: 250px;">
  <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:

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `name` | String | `""` | Form field name for the hidden `<input type="hidden">` element. |
| `placeholder` | String | `""` | Placeholder text displayed when the editor is empty. |
| `disabled` | Boolean | `false` | Disables the editor and toolbar buttons, preventing content editing. |
| `required` | Boolean | `false` | Marks the editor as required (`aria-required="true"`). |
| `toolbar` | String | `""` | Customizes or hides the toolbar groups (supports bracket, pipe, or JSON). |
| `cls` | String | `""` | Custom CSS classes for component elements (defaults to targeting `editor`). |
| `stl` | String | `""` | Custom inline styles for component elements (defaults to targeting `editor`). |
| `i18n` | String | `""` | Internationalization strings for toolbar tooltips, labels, and word count. |
| `force-prevent-rerender` | Boolean | `false` | Forcefully prevents component rerendering after initial mount. |

## Events

The Rich Text Editor component triggers the following events:

| Name | Description |
| --- | --- |
| `z-rte:input` | Fired when editor content changes, providing `{ value: string }` in `event.detail`. |