Content editable
NewOverview
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.
<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.
<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.
<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>
Accessibility
Each editor needs a concise label. The hook moves focus into the native input or textarea and restores it to the pencil after saving or cancelling. Every action has an accessible name, saved and discarded changes are announced, field errors are associated with the form control and announced when they change, and the global focus outline remains visible for every control.
Add this module
mix dizzy.add content_editable
Component API
ContentEditable.content_editable
Attributes
| Name | Type | Default | 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
| Name | Required |
|---|---|
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.