v0.0.2
Demos (demo panel, opens in a new tab)

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

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

Basic usage: view
BLADE

Give the initial text as HTML with value; a classic form sends the HTML under name.

blade
<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>" />
PropValues
valueHTML ("" for an empty editor)
nameThe name a classic form posts
placeholderText in the empty editor
hintHelp text below

A narrower toolbar

A narrower toolbar: view
BLADE

toolbar picks the tools to show, as a comma-separated list or an array.

blade
<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>" />
PropValues
toolbarA list of tools or an array; [] text only
input-classClasses of the editing area (min-h-64…)

Disabled

Disabled: view
BLADE

disabled locks the editor and its tools; in a classic form no value is sent.

blade
<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>" />
PropValues
disabledtrue | false (default false)

With Livewire

BLADE

wire:model writes the HTML to the property; to avoid a request on every key, use .live.debounce.500ms or .blur.

blade
{{-- 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>
PropValues
wire:modelHTML; "" 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?

PropTypeDefaultDescription
label—nullThe label above; it names the editing area and the toolbar
value—nullHTML; with wire:model the property's value, old() in a classic form
placeholder—nullText in the empty editor
hint—nullHelp text below while there is no error; linked with aria-describedby
toolbar—allThe tools (a list or an array): heading, bold, italic, underline, link, bullet-list, ordered-list, blockquote, clear, undo, redo
disabledbooleanfalseLocks the editor and the tools; the value is not posted
rowsnumber8The textarea's height without JavaScript
input-class—nullClasses 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.

bash
composer update "zubazar/*"
npm install
npm run build

Developer notes

Events, hooks and details
  • 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.js loads from next to zubazar-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 as javascript: are refused, example.com gets https:// and an e-mail address gets mailto:.
  • 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 the required rule works; a validation error shows below the field and the editing area gets aria-invalid.

Events

EventdetailDescription
inputHTMLAfter 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.
Zubazar UI · ZubazarDesigned for people.