Editable
newText that turns into a field when you click it, and back into text when you are done. It keeps the type of wherever you put it.
Open Props in the corner of the example to try activationMode, submitMode, readonly and disabled.
Installation
Usage
<script setup lang="ts">
import { Editable, EditableArea, EditableInput, EditablePreview } from '@/components/ui/editable'
const title = ref('Q4 roadmap')
</script>
<template>
<Editable v-model="title" placeholder="Untitled" class="text-headline-sm font-semibold">
<EditableArea>
<EditablePreview />
<EditableInput />
</EditableArea>
</Editable>
</template>
Editable: the root; the value, the modes and the slot withedit,submitandcancelEditableArea: the box the text and the field share; a tint on hover, the focus ring while editingEditablePreview: the text, or the placeholder while the value is emptyEditableInput: the field, in the same type as the text
There is no size: the text and the field take the font, size and weight of the place they sit in, so a heading stays a heading while you edit it. Set the type on Editable or on its parent.
autoResize is on, so the field is exactly as wide as its text and nothing moves when editing starts: the area is a grid with the text and the field in one cell, so a width, background or font class on EditableInput applies as on any other part. Turn it off and give the area a width when the field should fill a column instead.
activationMode decides what starts editing: focus (a click or Tab), dblclick or none. submitMode decides what saves: blur, enter, both or none. Escape always cancels and brings the old value back.
Examples
Buttons
Want a pencil to start and buttons to save or cancel? Put them in the root and wire them to its slot. Clicks inside the root don't count as leaving it, so the buttons work in every submitMode.
In a table
Each cell holds its own Editable. The cell keeps its padding; the box reaches a little past the text.
Double click
With activationMode="dblclick" a single click selects the row or opens it, and a double click renames, like files in a file manager. selectOnFocus selects the old name, ready to type over.
Double-click a folder to rename it.
- Invoices
- Contracts
- Receipts
API Reference
Every part accepts class, merged over its base classes.
Editable
| Prop | Default | Type and description |
|---|---|---|
| defaultValue | — | stringThe starting value when the value is not controlled. |
| modelValue | — | string | nullThe value, for |
| placeholder | — | string | { edit: string; preview: string; }Text shown while the value is empty. A string for both states, or |
| dir | — | DirectionReading direction. Inherited from the page or a ConfigProvider when left out. |
| disabled | — | booleanEditing is off: the preview leaves the tab order and the box fades. |
| readonly | — | booleanThe value can be focused and selected but not changed, like a read-only input. |
| activationMode | "focus" | ActivationModeWhat starts editing: |
| selectOnFocus | — | booleanSelect the whole text when editing starts. |
| submitMode | "blur" | SubmitModeWhat saves the value: |
| startWithEditMode | — | booleanStart in edit mode. |
| maxLength | — | numberThe most characters the value takes. |
| autoResize | true | booleanSize the field to its text, so preview and field share one box and entering edit mode moves nothing. On by default; turn it off to size the field yourself. |
| id | — | stringThe id of the input. |
| asChild | — | booleanRender the single child instead of a wrapper, merging the root's attributes and classes onto it. |
| as | — | AsTag | ComponentThe element or component to render, a |
| name | — | stringThe name the value is submitted under in a form. |
| required | — | booleanThe value is required in a form. |
| Event | Payload and description |
|---|---|
| update:modelValue | [value: string]Fires with the new value when it is submitted. |
| submit | [value: string | null | undefined]Fires with the value when it is submitted. |
| update:state | [state: "submit" | "edit" | "cancel"]Fires with |
| Slot | Scope and description |
|---|---|
| default | { isEditing: boolean; modelValue: string | null | undefined; isEmpty: boolean; submit: () => void; cancel: () => void; edit: () => void; }The area and anything beside it. Exposes |
EditableArea
| Prop | Default | Type and description |
|---|---|---|
| asChild | — | booleanRender the single child instead of a wrapper, merging the area's attributes and classes onto it. |
| as | — | AsTag | ComponentThe element or component to render, a |
| Slot | Scope and description |
|---|---|
| default | {}The preview and the input. |
EditablePreview
| Prop | Default | Type and description |
|---|---|---|
| asChild | — | booleanRender the single child instead of a wrapper, merging the preview's attributes and classes onto it. |
| as | — | AsTag | ComponentThe element or component to render, a |
| Slot | Scope and description |
|---|---|
| default | {}The text. The value, or the placeholder while it is empty, by default. |
EditableInput
| Prop | Default | Type and description |
|---|---|---|
| asChild | — | booleanRender the single child instead of the input, merging its attributes and classes onto it. |
| as | — | AsTag | ComponentThe element or component to render, an |
Props
- defaultValue
stringThe starting value when the value is not controlled.- modelValue
string | nullThe value, forv-model.nullwhen cleared.- placeholder
string | { edit: string; preview: string; }Text shown while the value is empty. A string for both states, or{ edit, preview }for a different one while editing.- dir
DirectionReading direction. Inherited from the page or a ConfigProvider when left out.- disabled
booleanEditing is off: the preview leaves the tab order and the box fades.- readonly
booleanThe value can be focused and selected but not changed, like a read-only input.- activationMode
"focus" ActivationModeWhat starts editing:focus(the default) on click or Tab,dblclickon a double click,noneonly through the slot'sedit.- selectOnFocus
booleanSelect the whole text when editing starts.- submitMode
"blur" SubmitModeWhat saves the value:blur(the default) when focus leaves,enteron Enter,both, ornoneonly through the slot'ssubmit. Escape always cancels.- startWithEditMode
booleanStart in edit mode.- maxLength
numberThe most characters the value takes.- autoResize
true booleanSize the field to its text, so preview and field share one box and entering edit mode moves nothing. On by default; turn it off to size the field yourself.- id
stringThe id of the input.- asChild
booleanRender the single child instead of a wrapper, merging the root's attributes and classes onto it.- as
AsTag | ComponentThe element or component to render, adivby default.- name
stringThe name the value is submitted under in a form.- required
booleanThe value is required in a form.
Emits
- update:modelValue
[value: string]Fires with the new value when it is submitted.- submit
[value: string | null | undefined]Fires with the value when it is submitted.- update:state
[state: "submit" | "edit" | "cancel"]Fires withedit,submitorcancelwhen the state changes.
Slots
- default
{ isEditing: boolean; modelValue: string | null | undefined; isEmpty: boolean; submit: () => void; cancel: () => void; edit: () => void; }The area and anything beside it. ExposesisEditing,isEmpty,modelValueandedit,submitandcancelfor buttons of your own.
Props
- asChild
booleanRender the single child instead of a wrapper, merging the area's attributes and classes onto it.- as
AsTag | ComponentThe element or component to render, adivby default.
Slots
- default
{}The preview and the input.
Props
- asChild
booleanRender the single child instead of a wrapper, merging the preview's attributes and classes onto it.- as
AsTag | ComponentThe element or component to render, aspanby default.
Slots
- default
{}The text. The value, or the placeholder while it is empty, by default.
Props
- asChild
booleanRender the single child instead of the input, merging its attributes and classes onto it.- as
AsTag | ComponentThe element or component to render, aninputby default.
Data attributes
| data-slot | Attributes | Description |
|---|---|---|
| editable | — | The root. |
| editable-area | data-focus, data-empty, data-placeholder-shown, data-readonly, data-disabled | The shared box. data-focus while editing, data-placeholder-shown while the text shows, data-empty while the value is empty. |
| editable-preview | data-placeholder-shown | The text, or the placeholder. |
| editable-input | data-readonly, data-disabled | The field. |
Keyboard
| Key | Action |
|---|---|
| Tab | Moves to the text; with activationMode="focus" it starts editing. |
| Enter | Saves with submitMode enter or both. |
| Escape | Cancels and brings the old value back. |
Accessibility
The field is a text input. Give it a name with aria-label on EditableInput, or put the editable in a Field. A readonly editable can still be focused and its text selected, like a read-only input; a disabled one leaves the tab order.