Content editable

New

Overview

Inline editing with slotted display content, an internal Phoenix form, native input or textarea controls, and save and cancel actions.

The component switches between read-only text and a native text input or textarea while editing. The native control is the Phoenix form field, so input changes, submissions, browser behavior, and validation errors use the normal form lifecycle without a hidden proxy.

Pass a field, set on_submit, and place the read-only display content inside the component. ContentEditable owns the form. Enter or the checkmark commits the input before the form submits; Escape or the X restores the value from when editing began.

Quarterly planning
HEEx
<ContentEditable.content_editable
  field={@form[:name]}
  label="project name"
  on_submit="save-project-name"
  class="text-lg font-semibold"
>
  {@form[:name].value}
</ContentEditable.content_editable>

Multiline content

Set as={:textarea} to preserve line breaks. Enter inserts a new line; Command+Enter on macOS or Control+Enter on other platforms saves.

Review open questions Confirm owners and next steps
HEEx
<ContentEditable.content_editable
  field={@form[:summary]}
  label="project summary"
  as={:textarea}
  on_submit="save-project-summary"
  class="max-w-md text-sm leading-6"
>
  {@form[:summary].value}
</ContentEditable.content_editable>

Errors

Errors from the bound field render beneath the editor and set aria-invalid and aria-describedby on the native form control. This example requires at least five characters.

Plan

must be at least 5 characters

HEEx
<ContentEditable.content_editable
  field={@form[:title]}
  label="project title"
  on_change="validate-project-title"
  on_submit="save-project-title"
  class="text-lg font-semibold"
>
  {@form[:title].value}
</ContentEditable.content_editable>

Add this module

terminal
mix dizzy.add content_editable

Component API

ContentEditable.content_editable

Attributes

Attributes for ContentEditable.content_editable
NameTypeDefault Values
as :atom :input:input, :textarea
class :any nilโ€”
disabled :boolean falseโ€”
errors :list []โ€”
field {:struct, Phoenix.HTML.FormField} nilโ€”
id :string nilโ€”
label :string Requiredโ€”
name :string nilโ€”
on_change :string nilโ€”
on_save :any %Phoenix.LiveView.JS{ops: []}โ€”
on_submit :string Requiredโ€”
rest :global nilโ€”
value :any nilโ€”

Slots

Slots for ContentEditable.content_editable
NameRequired
inner_block Required

JavaScript hook

DizzyContentEditable

Switches between slotted display content and a native input or textarea inside its own form, manages focus and keyboard commands, restores cancelled edits, and normalizes committed text before form submission and the optional on_save command.

mix dizzy.add includes the hook and performs any required asset setup automatically.

Explore Dizzy