Rich Text Editor

Material Design 3 rich-text editor web component (lit-material-rich-text-editor): a formatting toolbar over a sanitized, schema-bound contenteditable, form-associated, with its own undo history.

  • 19.2 KB gzipped
  • @lit-material/rich-text 0.0.0
  • Renders on the server

Example

Install

npm install @lit-material/rich-text @lit-material/tokens
import "@lit-material/rich-text";

Link @lit-material/tokens/css/index.css once per page for the colour, type and shape tokens. Install guide

API

<lit-material-rich-text-editor>

Material Design 3 rich-text editor: a formatting toolbar over a contenteditable surface whose content is held to a small schema — paragraphs, two heading levels, bulleted and numbered lists (nested), quotes, code blocks, bold, italic, underline, strikethrough, inline code and links. Every edit goes through the editor's own document model (not document.execCommand): commands, Enter, Backspace across blocks, paste and undo/redo are model operations, and anything the browser changes natively (typing, spellcheck) is read back and normalized. Pasted HTML and the value are parsed with a built-in tokenizer and sanitized to the schema — no HTML string is ever assigned to the DOM, so the editor needs no Trusted Types policy.

Properties

PropertyAttributeTypeDefaultDescription
variantvariantRichTextVariant"outlined"
labellabelstring""
placeholderplaceholderstring""
supportingTextsupporting-textstring""
errorTexterror-textstring""
errorerrorbooleanfalse
disableddisabledbooleanfalse
readonlyreadonlybooleanfalse
requiredrequiredbooleanfalse
maxlengthmaxlengthnumber | undefinednoneShows a character counter and makes the field invalid (tooLong) past this many characters of text.
namenamestring""
valuevaluestringnoneThe content as HTML. Whatever is set is sanitized to the schema; reading it back always gives the normalized form ("" when empty). Setting it resets the undo history.
canUndoproperty onlybooleannoneWhether undo would do anything.
canRedoproperty onlybooleannoneWhether redo would do anything.
validityproperty onlyValidityStatenone
validationMessageproperty onlystringnone
formproperty onlyHTMLFormElement | nullnone

Events

EventDescription
inputBubbles (composed) after every change to the content.
changeBubbles when focus leaves the editor after the content changed.

CSS parts

PartDescription
containerThe outlined/filled box around toolbar and content.
toolbarThe formatting toolbar (role="toolbar").
contentThe editable surface (role="textbox").
labelThe label above the box.
supportingThe supporting-text / character-counter row.
link-panelThe link editing panel.

CSS custom properties

PropertyDescription
--lit-material-rich-text-min-heightSmallest height of the editable surface (default 8rem).
--lit-material-rich-text-max-heightHeight after which the surface scrolls (default none).

Source on GitHub npm