Editable

A component that allows users to edit text in place.

Loading...

Anatomy

<Editable.Root>
  <Editable.Label />
  <Editable.Area>
    <Editable.Input />
    <Editable.Preview />
  </Editable.Area>
  <Editable.Control>
    <Editable.EditTrigger />
    <Editable.SubmitTrigger />
    <Editable.CancelTrigger />
  </Editable.Control>
</Editable.Root>

Examples

Controlled

Use value and onValueChange to control the value.

Root Provider

Use RootProvider with the useEditable hook to access the state and methods from outside the component.

Context

Read the state with Editable.Context or the useEditableContext hook, for example to show keyboard hints while editing.

Controls

Build custom controls to toggle edit and read mode. A render prop exposes the internal state.

Textarea

Use asChild on Editable.Input to render a textarea for multi-line editing.

Field

Wrap the editable in Field to wire up the label, helper text, error text, and their ARIA attributes.

Guides

Auto-resizing

Set autoResize to grow the editable with its content.

<Editable.Root placeholder="Placeholder" autoResize>
  {/*...*/}
</Editable.Root>

Max Length

Use maxLength to cap the number of characters.

<Editable.Root placeholder="Placeholder" autoResize maxLength={10}>
  {/*...*/}
</Editable.Root>

Double Click

By default, edit mode starts when the preview is focused (by pointer or keyboard). Set activationMode="dblclick" to start it on double-click instead.

<Editable.Root placeholder="Placeholder" activationMode="dblclick">
  {/*...*/}
</Editable.Root>

API Reference

Props

Root

Defines the <div> element.

AttributeDefaultType
activationMode'focus'
ActivationMode

The activation mode for the preview element. - "focus" - Enter edit mode when the preview is focused - "dblclick" - Enter edit mode when the preview is double-clicked - "click" - Enter edit mode when the preview is clicked - "none" - Edit can be triggered programmatically only

asChild
boolean

Use the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`.

For more details, read our Composition guide.
autoResize
boolean

Whether the editable should auto-resize to fit the content.

defaultEdit
boolean

Whether the editable is in edit mode by default.

defaultValue
string

The initial value of the editable when rendered. Use when you don't need to control the value of the editable.

disabled
boolean

Whether the editable is disabled.

edit
boolean

Whether the editable is in edit mode.

finalFocusEl
() => HTMLElement | null

The element to receive focus when the editable is closed.

form
string

The associate form of the underlying input.

id
string

The unique identifier of the machine.

ids
ElementIds

The ids of the elements in the editable. Useful for composition.

invalid
boolean

Whether the input's value is invalid.

maxLength
number

The maximum number of characters allowed in the editable

name
string

The name attribute of the editable component. Used for form submission.

onEditChange
(details: EditChangeDetails) => void

Function to call when the edit mode changes.

onFocusOutside
(event: FocusOutsideEvent) => void

Function called when the focus is moved outside the component

onInteractOutside
(event: InteractOutsideEvent) => void

Function called when an interaction happens outside the component

onPointerDownOutside
(event: PointerDownOutsideEvent) => void

Function called when the pointer is pressed down outside the component

onValueChange
(details: ValueChangeDetails) => void

Function to call when the value changes.

onValueCommit
(details: ValueChangeDetails) => void

Function to call when the value is committed.

onValueRevert
(details: ValueChangeDetails) => void

Function to call when the value is reverted.

placeholder
string | { edit: string; preview: string }

The placeholder text for the editable.

readOnly
boolean

Whether the editable is read-only.

render
ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>

Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state.

required
boolean

Whether the editable is required.

selectOnFocustrue
boolean

Whether to select the text in the input when it is focused.

submitMode'both'
SubmitMode

The action that triggers submit in the edit mode: - "enter" - Trigger submit when the enter key is pressed - "blur" - Trigger submit when the editable is blurred - "none" - No action will trigger submit. You need to use the submit button - "both" - Pressing `Enter` and blurring the input will trigger submit

translations
IntlTranslations

The translations for the editable.

value
string

The controlled value of the editable.

AttributeDescription
[data-editable-root]<uid>
[data-focus]Present when focused
[data-disabled]Present when disabled
[data-readonly]Present when read-only
[data-invalid]Present when invalid

Area

Defines the <div> element.

AttributeDefaultType
asChild
boolean

Use the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`.

For more details, read our Composition guide.
render
ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EditableAreaState>

Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state.

AttributeDescription
[data-editable-area]<uid>
[data-focus]Present when focused
[data-disabled]Present when disabled
[data-placeholder-shown]Present when placeholder is shown

CancelTrigger

Defines the <button> element.

AttributeDefaultType
asChild
boolean

Use the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`.

For more details, read our Composition guide.
render
ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>

Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state.

Control

Defines the <div> element.

AttributeDefaultType
asChild
boolean

Use the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`.

For more details, read our Composition guide.
render
ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>

Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state.

EditTrigger

Defines the <button> element.

AttributeDefaultType
asChild
boolean

Use the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`.

For more details, read our Composition guide.
render
ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>

Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state.

Input

Defines the <input> element.

AttributeDefaultType
asChild
boolean

Use the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`.

For more details, read our Composition guide.
render
ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EditableInputState>

Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state.

AttributeDescription
[data-editable-input]<uid>
[data-disabled]Present when disabled
[data-readonly]Present when read-only
[data-invalid]Present when invalid
[data-autoresize]

Label

Defines the <label> element.

AttributeDefaultType
asChild
boolean

Use the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`.

For more details, read our Composition guide.
render
ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EditableLabelState>

Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state.

AttributeDescription
[data-editable-label]<uid>
[data-focus]Present when focused
[data-invalid]Present when invalid
[data-required]Present when required

Preview

Defines the <span> element.

AttributeDefaultType
asChild
boolean

Use the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`.

For more details, read our Composition guide.
render
ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EditablePreviewState>

Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state.

AttributeDescription
[data-editable-preview]<uid>
[data-placeholder-shown]Present when placeholder is shown
[data-readonly]Present when read-only
[data-disabled]Present when disabled
[data-invalid]Present when invalid
[data-autoresize]

RootProvider

Defines the <div> element.

AttributeDefaultType
value
UseEditableReturn

asChild
boolean

Use the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`.

For more details, read our Composition guide.
render
ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>

Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state.

SubmitTrigger

Defines the <button> element.

AttributeDefaultType
asChild
boolean

Use the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`.

For more details, read our Composition guide.
render
ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>

Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state.

Context

API

PropertyType
editing
boolean

Whether the editable is in edit mode

empty
boolean

Whether the editable value is empty

value
string

The current value of the editable

valueText
string

The current value of the editable, or the placeholder if the value is empty

setValue
(value: string) => void

Function to set the value of the editable

clearValue
VoidFunction

Function to clear the value of the editable

edit
VoidFunction

Function to enter edit mode

cancel
VoidFunction

Function to exit edit mode, and discard any changes

submit
VoidFunction

Function to exit edit mode, and submit any changes

getRootState
() => RootState

Returns the state of the root

Accessibility

Keyboard Support

KeyDescription
Enter
Saves the edited content and exits edit mode.
Escape
Discards the changes and exits edit mode.