Editable

new

Text 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.

Editable
Q4 roadmap

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 with edit, submit and cancel
  • EditableArea: the box the text and the field share; a tint on hover, the focus ring while editing
  • EditablePreview: the text, or the placeholder while the value is empty
  • EditableInput: 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.

With buttons
Design review

In a table

Each cell holds its own Editable. The cell keeps its padding; the box reaches a little past the text.

Table cells
NameRole
Ada Lovelace
Analyst
Grace Hopper
Admiral
Alan Turing
Codebreaker

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.

On double click

Double-click a folder to rename it.

  • Invoices
  • Contracts
  • Receipts

API Reference

Every part accepts class, merged over its base classes.

Data attributes

data-slotAttributesDescription
editable—The root.
editable-areadata-focus, data-empty, data-placeholder-shown, data-readonly, data-disabledThe shared box. data-focus while editing, data-placeholder-shown while the text shows, data-empty while the value is empty.
editable-previewdata-placeholder-shownThe text, or the placeholder.
editable-inputdata-readonly, data-disabledThe field.

Keyboard

KeyAction
TabMoves to the text; with activationMode="focus" it starts editing.
EnterSaves with submitMode enter or both.
EscapeCancels 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.

Changelog

  1. registry 0.22.0

    c3a7ba0 — Enter that saves an Editable inside a form no longer submits the form as well. With submitMode="blur", or with Shift or Meta held, Enter keeps the form's own behaviour.

    1a307f9 — After Enter or Escape ends an edit, Editable gives focus back to its preview instead of dropping it to <body> as the input hides, without starting a focus-activated edit again. A save on blur leaves focus where the user moved it.

    7c3b1ae — Editable sizes its field to the text with classes of its own instead of Reka's inline all: unset style: the area is an inline grid with the text and the field in one cell, and the one not in use is invisible. Width, background, font and other classes on EditableInput now apply; before, only ! classes did. The look and the sizing are unchanged, and autoResize still turns it off.

  2. registry 0.21.0

    c30241a — New Editable: text that turns into a field where it stands, over Reka UI. Four parts (Editable, EditableArea, EditablePreview, EditableInput); the text and the field take the surrounding type, autoResize is on so entering edit mode moves nothing, and edit, save and cancel buttons come from the root slot. A disabled editable leaves the tab order, which Reka's preview does not do on its own.