A fully accessible WYSIWYG rich text editor with keyboard navigation, smart paste, and zero dependencies
Add a rich text editor to your page in seconds:
<!-- Include CSS and JS -->
<link rel="stylesheet" href="styles/text-editor.css">
<script src="javascript/text-editor.js"></script>
<!-- Add the editor -->
<div data-rsl-text-editor></div>
WCAG 2.1 AA compliant with ARIA roles, live regions, and complete keyboard navigation.
All toolbar buttons navigable via Tab/Arrow keys. Press ? to see keyboard shortcuts overlay.
Automatically cleans Word/Google Docs formatting while preserving basic styles.
Toggle between WYSIWYG and HTML source mode for advanced editing.
Optional word/character limits with visual feedback and threshold warnings.
Full dark mode support that respects system preferences and RSL theme toggle.
| Attribute | Values | Default | Description |
|---|---|---|---|
data-rsl-text-editor |
- | - | Required. Marks element as a text editor container. |
data-toolbar |
full, basic, minimal, custom |
full |
Toolbar preset configuration. |
data-toolbar-buttons |
Comma-separated list | - | Custom buttons when using data-toolbar="custom". |
data-placeholder |
String | Start typing... |
Placeholder text shown when editor is empty. |
data-min-height |
CSS value | 200px |
Minimum height of the content area. |
data-max-height |
CSS value | none |
Maximum height (enables scrolling). |
data-char-limit |
Number | - | Maximum character count. |
data-word-limit |
Number | - | Maximum word count. |
data-show-count |
true, false |
true |
Show word/character count in status bar. |
data-readonly |
true, false |
false |
Make editor read-only. |
data-autofocus |
true, false |
false |
Auto-focus editor on page load. |
| Preset | Included Buttons |
|---|---|
full |
Headings dropdown, Bold, Italic, Underline, Strikethrough | Link, Image | Unordered List, Ordered List | Align Left, Align Center, Align Right | Blockquote, Code | Undo, Redo | Clear Formatting, Source View |
basic |
Bold, Italic, Underline | Link | Unordered List, Ordered List | Undo, Redo |
minimal |
Bold, Italic | Link |
custom |
Specify buttons via data-toolbar-buttons |
<div data-rsl-text-editor
data-toolbar="custom"
data-toolbar-buttons="bold,italic,link,image,source"></div>
| Method | Parameters | Description |
|---|---|---|
RSL.TextEditor.init() |
- | Initialize all text editors on the page. |
RSL.TextEditor.create(element, options) |
Element or selector, options object | Create a new text editor instance. |
RSL.TextEditor.getInstance(element) |
Element or selector | Get existing editor instance. |
RSL.TextEditor.destroy(element) |
Element or selector | Destroy editor and clean up. |
| Method | Returns | Description |
|---|---|---|
getValue() |
String (HTML) | Get the editor content as HTML. |
setValue(html) |
- | Set the editor content. |
getText() |
String | Get plain text content without HTML. |
getWordCount() |
Number | Get current word count. |
getCharCount() |
Number | Get current character count. |
focus() |
- | Focus the editor content area. |
blur() |
- | Remove focus from editor. |
insertHTML(html) |
- | Insert HTML at cursor position. |
clear() |
- | Clear all editor content. |
execCommand(command, value) |
- | Execute a formatting command. |
toggleSource() |
- | Toggle between WYSIWYG and source view. |
| Event | Detail Properties | Description |
|---|---|---|
rsl-editor-ready |
editor |
Fired when editor is initialized and ready. |
rsl-editor-change |
editor, html, text, wordCount, charCount |
Fired when content changes. |
rsl-editor-focus |
editor |
Fired when editor receives focus. |
rsl-editor-blur |
editor |
Fired when editor loses focus. |
rsl-editor-command |
editor, command, value |
Fired when a formatting command is executed. |
document.querySelector('#my-editor').addEventListener('rsl-editor-change', function(e) {
console.log('Content changed!');
console.log('HTML:', e.detail.html);
console.log('Word count:', e.detail.wordCount);
console.log('Character count:', e.detail.charCount);
});
prefers-reduced-motion