Rich text
An editor for formatted text such as product descriptions or announcements: headings, bold, italic, links, lists and quotes.
zb:rich-text zubazar/ui-pro
Clean the editor's HTML on the server before you store it or print it with {!! !!} (e.g. clean($html) with mews/purifier).
This component is in Zubazar UI Pro; Zubazar UI Pro brings Zubazar UI with it. Installing Zubazar UI Pro →
Basic usage
Give the initial text as HTML with value; a classic form sends the HTML under name.
<zb:rich-text name="description" label="Product description" placeholder="Material, size, care…" hint="You can add headings, lists and links." value="<h2>A soft texture for everyday wear</h2><p>Made of 100% organic cotton in a <strong>breathable</strong> fabric.</p><ul><li><p>Machine washable at 30 °C</p></li></ul>" />
| Prop | Values |
|---|---|
value | HTML ("" for an empty editor) |
name | The name a classic form posts |
placeholder | Text in the empty editor |
hint | Help text below |
A narrower toolbar
toolbar picks the tools to show, as a comma-separated list or an array.
<zb:rich-text name="brand_summary" label="Brand summary" toolbar="bold,italic,link,bullet-list" input-class="min-h-24" value="<p>Our workshop has made <em>sustainable</em> clothing since 1998.</p>" />
| Prop | Values |
|---|---|
toolbar | A list of tools or an array; [] text only |
input-class | Classes of the editing area (min-h-64…) |
Links
The link button (or Ctrl/⌘+K) opens the address dialog; with no selected text the address becomes the link text.
<zb:rich-text name="announcement" label="Announcement" toolbar="bold,link" value="<p>See the <a href="/shipping">delivery page</a> for shipping costs.</p>" />
| Prop | Values |
|---|---|
link | Ctrl/⌘+K opens it too |
Disabled
disabled locks the editor and its tools; in a classic form no value is sent.
<zb:rich-text name="archive_note" label="Archive note" disabled :toolbar="['bold', 'italic']" value="<p>This product was discontinued at the end of the season.</p>" />
| Prop | Values |
|---|---|
disabled | true | false (default false) |
With Livewire
wire:model writes the HTML to the property; to avoid a request on every key, use .live.debounce.500ms or .blur.
{{-- Component: public string $description = ''; when saving: $this->description = clean($this->description); (mews/purifier) --}}
<form wire:submit="save" class="space-y-4">
<zb:rich-text wire:model="description" label="Product description" placeholder="Describe the product" hint="Cleaned on the server before saving." required />
<zb:button type="submit">Save</zb:button>
</form>
| Prop | Values |
|---|---|
wire:model | HTML; "" for an empty editor |
required | * by the label, aria-required on the editing area |
About
The value is an HTML string with only the toolbar's formats. The kit does nothing on the server: the editor produces only its own tags, but a request can carry any HTML, so cleaning it is the application's job.
Props
The values the component takes. What are props and slots?
| Prop | Type | Default | Description |
|---|---|---|---|
label | — | null | The label above; it names the editing area and the toolbar |
value | — | null | HTML; with wire:model the property's value, old() in a classic form |
placeholder | — | null | Text in the empty editor |
hint | — | null | Help text below while there is no error; linked with aria-describedby |
toolbar | — | all | The tools (a list or an array): heading, bold, italic, underline, link, bullet-list, ordered-list, blockquote, clear, undo, redo |
disabled | boolean | false | Locks the editor and the tools; the value is not posted |
rows | number | 8 | The textarea's height without JavaScript |
input-class | — | null | Classes of the editing area; class places the whole field |
wire:model / x-model | — | — | Livewire or Alpine binding (HTML); stays on the outer element |
Slots: name and id go to the textarea (the wire:model name without name); required, autofocus, aria-* and data-* are passed on to the editing area. wire:model, x-model, class, style, x-show, wire:key and events stay on the outer wrapper.
Installation
Tiptap is an npm dependency of Zubazar UI Pro: run npm install after installing or updating the package; the editor is downloaded only on a page that uses it.
composer update "zubazar/*"
npm install
npm run build
Developer notes
- The editor is built on Tiptap (MIT); its code loads only on a page with an editor, and on a page with
@zubazarUiScripts,dist/zubazar-ui-pro-rich-text.jsloads from next tozubazar-ui-pro.js. - If Zubazar UI Pro's JavaScript is missing from the page, the console says so.
- The server prints the value escaped in a
<textarea>: without JavaScript that is the field; with it, the editor starts from it and keeps it up to date. - The toolbar is one Tab stop, with the arrows, Home and End inside it; the formats at the cursor carry
aria-pressed, and the Ctrl/⌘+B, I, U, K and Z shortcuts work. - The tools also decide what the HTML may hold: without the heading tool a pasted heading becomes a paragraph; an unknown tool name is an error.
- Only http, https, mailto, tel and on-site addresses (
/products,#details) become links; addresses such asjavascript:are refused,example.comgetshttps://and an e-mail address getsmailto:. - Livewire updates keep the editor (
wire:ignore); a value the server writes (a template, a reset) reaches the editor and the cursor stays in place. - An empty editor's value is
"", so therequiredrule works; a validation error shows below the field and the editing area getsaria-invalid.
Events
| Event | detail | Description |
|---|---|---|
input | HTML | After every edit, from the root (detail: the HTML, "" without text); wire:model and x-model read it. |
change | — | After every edit; bubbles (the unsaved-changes guard and the form bar listen to it). |
blur | — | When the focus leaves the field (the editor, the toolbar, the link dialog); wire:model.blur reads it. |