Editable
A component that allows users to edit text in place.
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 the value and onValueChange props to control the editable state.
Root Provider
An alternative way to control the editable is to use the RootProvider component and the useEditable hook. This way
you can access the state and methods from outside the component.
Context
Use Editable.Context to access the editable state and show contextual UI like keyboard hints when editing.
Controls
In some cases, you might need to use custom controls to toggle the edit and read mode. We use the render prop pattern to provide access to the internal state of the component.
Textarea
Use the asChild prop on Editable.Input to render a textarea for multi-line editing.
Field
The Field component helps manage form-related state and accessibility attributes of an editable. It includes handling
ARIA labels, helper text, and error text to ensure proper accessibility.
Guides
Auto-resizing
To auto-grow the editable as the content changes, set the autoResize prop to true.
<Editable.Root placeholder="Placeholder" autoResize>
{/*...*/}
</Editable.Root>
Max Length
Use the maxLength prop to set a maximum number of characters that can be entered into the editable.
<Editable.Root placeholder="Placeholder" autoResize maxLength={10}>
{/*...*/}
</Editable.Root>
Double Click
The editable supports two modes of activating the "edit" state:
- when the preview part is focused (with pointer or keyboard).
- when the preview part is double-clicked.
To change the mode to double-click, pass the prop activationMode="dblclick".
<Editable.Root placeholder="Placeholder" activationMode="dblclick">
{/*...*/}
</Editable.Root>
API Reference
Props
Root
Renders a <div> element.
| Prop | Default | Type |
|---|---|---|
activationMode | 'focus' | ActivationModeThe 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 | booleanUse the provided child element as the default rendered element, combining their props and behavior. For more details, read our Composition guide. | |
autoResize | booleanWhether the editable should auto-resize to fit the content. | |
defaultEdit | booleanWhether the editable is in edit mode by default. | |
defaultValue | stringThe initial value of the editable when rendered. Use when you don't need to control the value of the editable. | |
disabled | booleanWhether the editable is disabled. | |
edit | booleanWhether the editable is in edit mode. | |
finalFocusEl | () => HTMLElement | nullThe element to receive focus when the editable is closed. | |
form | stringThe associate form of the underlying input. | |
id | stringThe unique identifier of the machine. | |
ids | Partial<{
root: string
area: string
label: string
preview: string
input: string
control: string
submitTrigger: string
cancelTrigger: string
editTrigger: string
}>The ids of the elements in the editable. Useful for composition. | |
invalid | booleanWhether the input's value is invalid. | |
maxLength | numberThe maximum number of characters allowed in the editable | |
name | stringThe name attribute of the editable component. Used for form submission. | |
onEditChange | (details: EditChangeDetails) => voidFunction to call when the edit mode changes. | |
onFocusOutside | (event: FocusOutsideEvent) => voidFunction called when the focus is moved outside the component | |
onInteractOutside | (event: InteractOutsideEvent) => voidFunction called when an interaction happens outside the component | |
onPointerDownOutside | (event: PointerDownOutsideEvent) => voidFunction called when the pointer is pressed down outside the component | |
onValueChange | (details: ValueChangeDetails) => voidFunction to call when the value changes. | |
onValueCommit | (details: ValueChangeDetails) => voidFunction to call when the value is committed. | |
onValueRevert | (details: ValueChangeDetails) => voidFunction to call when the value is reverted. | |
placeholder | string | { edit: string; preview: string }The placeholder text for the editable. | |
readOnly | booleanWhether the editable is read-only. | |
required | booleanWhether the editable is required. | |
selectOnFocus | true | booleanWhether to select the text in the input when it is focused. |
submitMode | 'both' | SubmitModeThe 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 | IntlTranslationsThe translations for the editable. | |
value | stringThe controlled value of the editable. |
Area
Renders a <div> element.
| Prop | Default | Type |
|---|---|---|
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. For more details, read our Composition guide. |
| Attribute | Description |
|---|---|
[data-scope] | editable |
[data-part] | area |
[data-focus] | Present when focused |
[data-disabled] | Present when disabled |
[data-placeholder-shown] | Present when placeholder is shown |
CancelTrigger
Renders a <button> element.
| Prop | Default | Type |
|---|---|---|
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. For more details, read our Composition guide. |
Control
Renders a <div> element.
| Prop | Default | Type |
|---|---|---|
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. For more details, read our Composition guide. |
EditTrigger
Renders a <button> element.
| Prop | Default | Type |
|---|---|---|
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. For more details, read our Composition guide. |
Input
Renders a <input> element.
| Prop | Default | Type |
|---|---|---|
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. For more details, read our Composition guide. |
| Attribute | Description |
|---|---|
[data-scope] | editable |
[data-part] | input |
[data-disabled] | Present when disabled |
[data-readonly] | Present when read-only |
[data-invalid] | Present when invalid |
[data-autoresize] |
Label
Renders a <label> element.
| Prop | Default | Type |
|---|---|---|
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. For more details, read our Composition guide. |
| Attribute | Description |
|---|---|
[data-scope] | editable |
[data-part] | label |
[data-focus] | Present when focused |
[data-invalid] | Present when invalid |
[data-required] | Present when required |
Preview
Renders a <span> element.
| Prop | Default | Type |
|---|---|---|
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. For more details, read our Composition guide. |
| Attribute | Description |
|---|---|
[data-scope] | editable |
[data-part] | preview |
[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
Renders a <div> element.
| Prop | Default | Type |
|---|---|---|
value | UseEditableReturn | |
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. For more details, read our Composition guide. |
SubmitTrigger
Renders a <button> element.
| Prop | Default | Type |
|---|---|---|
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. For more details, read our Composition guide. |
Context
API
| Property | Type |
|---|---|
editing | booleanWhether the editable is in edit mode |
empty | booleanWhether the editable value is empty |
value | stringThe current value of the editable |
valueText | stringThe current value of the editable, or the placeholder if the value is empty |
setValue | (value: string) => voidFunction to set the value of the editable |
clearValue | VoidFunctionFunction to clear the value of the editable |
edit | VoidFunctionFunction to enter edit mode |
cancel | VoidFunctionFunction to exit edit mode, and discard any changes |
submit | VoidFunctionFunction to exit edit mode, and submit any changes |
Accessibility
Keyboard Support
| Key | Description |
|---|---|
Enter | Saves the edited content and exits edit mode. |
Escape | Discards the changes and exits edit mode. |