Skip to content
Focsd homeFocsd homeFocsd
Forms

Rich text editor

A formatting text editor for notes, descriptions and messages: headings, lists, links and inline styles.

  • Tag: focsd-rich-text
  • Pattern: Textbox (multi-line, contenteditable) + toolbar
  • Category: Forms

Example

HTML
<div class="full">
<focsd-rich-text
label="Release notes"
name="notes"
hint="Shown to customers on the changelog"
value="<h2>What's new</h2><p>Faster <strong>search</strong> and a new <a href='https://example.com'>export</a> option.</p><ul><li>Dark mode</li><li>Keyboard shortcuts</li></ul>"></focsd-rich-text>
</div>

Usage

Import the module once, then use the tag like any HTML element. Every framework below renders the same element; see Frameworks for setup.

<script type="module">
import '@focsd-dev/editor';
</script>
<focsd-rich-text label="Release notes" name="notes" hint="Shown to customers on the changelog" value="<h2>…</focsd-rich-text>

API

Attributes

AttributePropertyTypeDefault
labellabelstring—
hinthintstring—
placeholderplaceholderstring—
valuevalue——
namenamestring—
disableddisabledbooleanfalse
requiredrequiredbooleanfalse
readonlyreadOnlybooleanfalse
aria-labelhostAriaLabelstring | nullnull
aria-descriptionhostAriaDescriptionstring | nullnull
aria-labelledbyhostAriaLabelledBystring | nullnull
aria-describedbyhostAriaDescribedBystring | nullnull
label-positionlabelPosition'top' | 'start' | 'end' | undefined—
errorerror——
warningwarningstring | undefined—
successsuccessstring | undefined—
error-messageerrorMessagestring | undefined—
warning-messagewarningMessagestring | undefined—
warning-patternwarningPatternstring | undefined—

Methods

MethodDescription
focus(options)Focus the editable area.
openLinkForm()Open the link form for the selection (prefilled with the current link).
describedByIds()IDs to add to the inner control's aria-describedby: the hint, then the shown message.
checkValidity()True when the value is valid; otherwise fires invalid and returns false, like native.
reportValidity()Like checkValidity(), and also shows the browser's validation message when invalid.
setCustomValidity(message)Set (non-empty) or clear (empty string) a custom validation error, like native controls.
setValidityState(flags, message, anchor)For subclasses: set the validity flags and message. anchor is the element the browser points its message at.
validate()Show (or clear) the error from the control's own checks: required, type, pattern, min, max and the like. Uses error-message, else the browser's localized message. An error the author set (a server error, say) is left as it is.
clearValidation()Clear the error and warning that validate() and warning-pattern showed (a form reset does).
emitValue(commit)For subclasses: fire the value events, as a native control does on a user edit: input (bubbles, composed), then with commit also change (bubbles, stays in its tree).
idrefText(ids)For subclasses: the text of the elements an ID reference list names, resolved in the host's tree (ID references cannot cross a shadow root), joined by spaces.

Events

EventDescription
inputFired on every edit.
changeFired when the editor loses focus after its value changed.

Slots

SlotDescription
contentCreated by the element: the editable area (do not supply your own).

CSS parts

Style the inside of the component with ::part(), for example focsd-rich-text::part(label).

PartDescription
labelThe label.
hintThe hint.
errorThe error message.
fieldThe frame around the toolbar and the editable area.
toolbarThe formatting toolbar.
toolEach toolbar button (also `tool-active` when pressed).
separatorA divider between toolbar groups.
link-formThe link address form.

Accessibility

  • A multi-line role="textbox" named by its label, with the hint or error as its description.
  • The toolbar is an APG toolbar: one tab stop, arrow keys between buttons, aria-pressed for formats, and aria-keyshortcuts for each shortcut.
  • The value is HTML limited to the toolbar's formats: scripts, event handlers and unsafe links are removed, and links only accept web, mail, phone or same-site addresses.
  • Form-associated: name, required, disabled, readonly, reset and setCustomValidity work like a textarea.
  • Separate package (@focsd-dev/editor, about 141 KB gzip with Tiptap and ProseMirror): load it only on pages that need it, for example with import('@focsd-dev/editor').

Keyboard

KeyAction
Ctrl/Cmd + B, I, UBold, italic, underline
Ctrl/Cmd + KAdd or edit a link
Ctrl/Cmd + Alt + 2 / 3Heading 2 / 3
Ctrl/Cmd + Shift + 7 / 8Numbered / bulleted list
Ctrl/Cmd + Z, Shift + ZUndo, redo
Tab / Shift + Tab (in a list)Indent / outdent the item; elsewhere Tab leaves the editor
Arrow keys (toolbar)Move between buttons