A rich text editor that posts like a textarea.
Put <rich-text-editor> in a server-rendered form. It submits its HTML under its own name, with no glue code.
Details
Not every application has a front-end framework. A Laravel, Django or Rails view, an htmx page or a plain HTML form needs a field, not a component tree. The <rich-text-editor> element is that field: it joins the surrounding form, posts its HTML under its name, resets with the form, and supports required.
The things a textarea does.
name, with no hidden input to copy into.form.reset() restores the starting content, and required blocks an empty editor.Add it to a form
Copy the richtexteditor folder from the download or the npm package to your site, load the scripts, and use the element inside any form. Serve the page over HTTP; opening it from a file:// path does not work. The download includes web-component.html, a working form with preview, reset and disable buttons.
<link rel="stylesheet" href="/richtexteditor/rte_theme_default.min.css"> <link rel="stylesheet" href="/richtexteditor/rte_mobile.min.css"> <script src="/richtexteditor/rte.js"></script> <!-- Optional: the plugins for the full toolbar. --> <script src="/richtexteditor/plugins/all_plugins.min.js"></script> <script src="/richtexteditor/rte-webcomponent.js"></script> <form method="post" action="/articles"> <rich-text-editor name="body" required value="<p>Hello</p>"></rich-text-editor> <button>Save</button> </form>
On the server
The editor arrives as an ordinary form field holding HTML. Read it the way your framework reads any text field: $request->input('body') in Laravel, request.POST["body"] in Django, params[:body] in Rails. Then sanitize it before you store it, because a request can be sent without the browser. The stored XSS article covers what to check.
From your own script
Set the .value property to change the content from code, and wait for .ready before calling editor methods through .editor.
const field = document.querySelector("rich-text-editor");
field.value = "<p>Application content</p>"; // works before the element connects
await field.ready; // a Promise: the editor is built
console.log(field.value); // current HTML
const payload = new FormData(field.form); // name="body" is includedAttributes, properties and events
- Attributes:
value,name,toolbar,height,width,readonly,disabled,required,shadow, andconfigas JSON. The starting content can also go between the tags. - Properties:
.value(the current HTML),.editor(the full editor API) and.ready(a Promise). - Events:
inputwhile the user edits, andchange. disabledandreadonly: both stop editing. A disabled field, including one inside a disabled<fieldset>, is left out of the submission; a read-only one is still submitted.- Light DOM by default. Add
shadowto render inside a shadow root; that needs theshadowdomplugin for styling.
Before you rely on it
- Older browsers get submission, not full validation. The element uses form-associated custom elements. Where a browser lacks them, a hidden input still submits the value, but native
requiredchecks do not run. - A filled field is not a finished upload. The emptiness check counts images as content. Wait for pending image uploads before saving, and set up a persistent upload endpoint for production.
- Use
requestSubmit(), notsubmit(), when you submit from code:form.submit()skips validation and submit listeners. - It is in every license, from $129 one-time. See pricing, and the PHP and ASP.NET Core pages for server examples with upload handlers.