Skip to content

shadcn.io is an independent project built on shadcn/ui

Components

Free

Editable for UI SDK

A value shown as text that turns into a field when clicked, then saves or cancels the change. Inline editing for React on UI SDK, for names and titles.

On this page9 sections

An editable shows a value as plain text and turns it into a field when someone clicks it, then saves the change or puts the old text back. It suits values that change now and then, in place, such as a display name on a profile card or the title of a document.

When a value is edited as often as it's read, an always-open Input is clearer. UI SDK has no inline-edit part, so this one is built on UI SDK's own primitives and installs nothing else.

Preview width

Installation

Sign in to get your install command
Free with an account. It carries your token, so the CLI installs everything in one go.

Anatomy

Editable anatomy: preview, edit trigger, input, cancel trigger, submit trigger

Usage

import { CheckIcon, PencilIcon, XIcon } from "lucide-react";
import { Button } from "@/components/ui/button";
import { Card, CardContent, CardHeader } from "@/components/ui/card";
import {
  Editable, EditableArea, EditableCancelTrigger, EditableContext,
  EditableControl, EditableEditTrigger, EditableInput, EditablePreview,
  EditableSubmitTrigger,
} from "@/components/ui/editable";
import { Field, FieldGroup, FieldLabel } from "@/components/ui/field";
import { Input } from "@/components/ui/input";
const EditableButtons = () => (
  <EditableContext>
    {(editable) =>
      editable.editing ? (
        <>
          <EditableCancelTrigger
            render={
              <Button aria-label="Cancel" size="icon-sm" variant="ghost" />
            }
          >
            <XIcon aria-hidden="true" />
          </EditableCancelTrigger>
          <EditableSubmitTrigger
            render={<Button aria-label="Save" size="icon-sm" variant="ghost" />}
          >
            <CheckIcon aria-hidden="true" />
          </EditableSubmitTrigger>
        </>
      ) : (
        <EditableEditTrigger
          render={<Button aria-label="Edit" size="icon-sm" variant="ghost" />}
        >
          <PencilIcon aria-hidden="true" />
        </EditableEditTrigger>
      )
    }
  </EditableContext>
);

const EditableDemo = () => (
  <Card className="w-full max-w-sm">
    <CardHeader
      description="Click the field or the edit button to change a value."
      title="Edit user"
    />
    <CardContent>
      <FieldGroup>
        <Field>
          <FieldLabel id="editable-default-name-label">Name</FieldLabel>
          <Editable
            defaultValue="Vinicius Vicentini"
            ids={{ label: "editable-default-name-label" }}
          >
            <EditableArea>
              <EditableInput render={<Input />} />
              <EditablePreview />
            </EditableArea>
            <EditableControl>
              <EditableButtons />
            </EditableControl>
          </Editable>
        </Field>
        <Field>
          <FieldLabel id="editable-default-username-label">Username</FieldLabel>
          <Editable
            defaultValue="@vinihvc"
            ids={{ label: "editable-default-username-label" }}
          >
            <EditableArea>
              <EditableInput render={<Input />} />
              <EditablePreview />
            </EditableArea>
            <EditableControl>
              <EditableButtons />
            </EditableControl>
          </Editable>
        </Field>
      </FieldGroup>
    </CardContent>
  </Card>
);

Accessibility

The text is a button in the tab order whenever editing is allowed and activationMode isn't none, and the field is a real input. Give the FieldLabel an id and pass the same id to Editable in ids as label, as the demos do: the input takes its name from the label, and the text reads the label before the value, such as "Name, Vinicius Vicentini". With no label at all, the input is called "editable input". Enter, Space or F2 on the text opens the field in every mode but none, where an EditableEditTrigger is the only way in.

Enter
On the text, opens the field, unless activationMode is none. In the field, saves the edit and shows the text again, unless submitMode is blur or none.
Space
On the text, opens the field, unless activationMode is none.
F2
On the text, opens the field, as Space does.
Escape
Puts the old text back and shows the text again, without closing a dialog the editable sits in.
Tab
Moves focus onto the text, which opens the field when activationMode is focus, the default, and skips it when activationMode is none. Once focus leaves an open field and its buttons, the edit is saved, or discarded when submitMode is enter or none.
Ctrl+Enter
In a textarea, where Enter adds a line, saves the edit. On a Mac, use Cmd + Enter.

Demos

Activation click

Name and Username open for editing on a single click, and tabbing past the text doesn't start an edit, which helps in a form people move through by keyboard. Enter, Space or F2 on the text opens it from the keyboard, and each field also has an edit button, an EditableEditTrigger, that shows the way in. Cancel and save take its place while the field is open.

Preview width

Activation focus

activationMode is focus, the default, so the field opens as soon as the text gets focus, by Tab or by a click. It's the quickest way through a short profile form of two or three values. The flip side is that tabbing through the page opens every field on the way and saves each one as focus leaves it.

Preview width

Activation none

activationMode is none, so clicking or focusing the text does nothing, and the edit button, an EditableEditTrigger, is the only way in. Cancel and save take its place while the field is open. Use it when editing is rare enough to deserve a button, or when people should be able to select the text without opening it.

Preview width

Controlled

An Edit button in the card header opens Name and Username together and turns into Save while they're open. Both fields set activationMode to none and take edit from the page's state, so only the header button changes them. Use it when several values enter and leave edit mode as one, such as the fields of a profile card.

Preview width

Dblclick

activationMode set to dblclick leaves a single click alone and opens the field on the second, the way renaming a file works on a desktop, where the first click selects it. Cancel and save show while the field is open. Enter, Space or F2 on the text opens it from the keyboard, and each field also has an edit button, an EditableEditTrigger, for people who don't use a mouse.

Preview width

Orientation horizontal

One line of text, with cancel and save buttons that appear on the same row while it's being edited. orientation is horizontal, the default, which suits a short value such as a name or a URL slug when the row has room for the field and two icon buttons.

Preview width

Orientation vertical

orientation set to vertical stacks a textarea above the buttons, which sit at the end of the row underneath. Use it for a value that runs to several lines, or in a narrow card where a single row would squeeze the text. In the textarea, Enter adds a line, and Ctrl + Enter or Cmd + Enter saves.

Preview width

Size lg

size set to lg on EditablePreview, a 36-pixel input and large icon buttons make a taller row. Only the height changes, not the size of the text, so use it in a row of other large inputs and buttons, where the default row would sit short.

Preview width

Size md

md is the default, a 32-pixel row with medium icon buttons, written out here to compare with the other two. It matches the default Input, so leave size out in a settings form and the editable lines up with the fields around it.

Preview width

Size sm

size set to sm on EditablePreview, a 28-pixel input and small icon buttons make a shorter row for a table cell, a sidebar or a dense list of settings. Only the height changes, and the value is edited the same way.

Preview width

With textarea

A Description field holds a two-sentence paragraph, edited in a textarea with the buttons underneath because orientation is vertical. Use it for a bio, a note or a product description. It keeps the default focus mode, so a click on the text or Tab opens the field.

Preview width

Without controls

Name and Email have no buttons at all: click the text to edit, press Enter to save and Escape to put the old value back, and clicking or tabbing away saves too. Use it in a list where buttons on every row would be clutter. Show the buttons when people need to see how to finish.

Preview width

FAQ

How do I control the text of an Editable in React?

Hold the text in state, pass it as value and update it in onValueChange, which receives details.value. That callback fires on every keystroke while the field is open, so your state holds the draft. Save to your server from onValueCommit instead, which fires once, when the edit is saved.

Use defaultValue when the text only needs a starting value, as most of the demos do.

If you control edit as well, every way out, from Enter and Escape to the buttons and a click outside, only asks through onEditChange, and the edit ends when you set edit to false: saved, or put back after Escape or the cancel button. Set it to false from anywhere else, such as a keyboard shortcut, and the typed text is kept.

How do I save an Editable value only when the person confirms it?

Save from onValueCommit, which fires with the final value when the edit is saved by Enter, by the save button or by focus leaving the field. Escape and the cancel button call onValueRevert with the old value instead.

submitMode decides what counts as saving. both, the default, saves on Enter and when focus leaves, enter saves on Enter and discards the edit when focus leaves, blur saves when focus leaves and ignores Enter, and none saves only from an EditableSubmitTrigger.

How do I start editing an Editable on click instead of focus?

Set activationMode on Editable. focus, the default, opens the field when the text gets focus, click on a click, dblclick on a double-click, and none only from an EditableEditTrigger or the edit prop.

With click or dblclick, the text still takes focus, and Enter, Space or F2 opens it, so the keyboard has a way in without a button. An EditableEditTrigger still shows people that the value can change, as the Activation click and Dblclick demos show.

Can I submit an Editable value with a form?

Yes. Give Editable a name, and the component puts it on the input, which stays in the form while hidden, so the form submits the current text. While the field is open that's the draft, saved or not.

Setting required, maxLength, disabled or readOnly on Editable reaches that input too. A disabled editable sends nothing, so use readOnly for a value that must be submitted but not changed.

How do I show a placeholder when an Editable is empty?

Pass placeholder to Editable. A string shows in the text while the value is empty and in the input while editing, and an object with preview and edit keys sets the two apart, such as Add a title in the text and Title in the input.

While the placeholder shows, the text carries data-placeholder-shown, which the component already uses to gray it out.

How do I make an Editable with several lines of text?

Pass a Textarea to EditableInput as render in place of the Input, give EditablePreview the same minimum height, and set orientation to vertical so the buttons sit underneath, as the With textarea demo does.

In a textarea, Enter adds a line, so Ctrl + Enter, or Cmd + Enter on a Mac, saves. The text keeps line breaks only once you add a class such as whitespace-pre-wrap to EditablePreview.

How do I style an Editable to look like a page title?

Put the same text classes on EditablePreview and on the Input you pass to EditableInput as render, such as text-2xl font-semibold, so the text doesn't jump when the field opens.

EditableArea is a fixed 32 pixels tall, or 28 and 36 with size set to sm or lg on the preview, so give it a taller height class as well, such as h-12. The component is your file once installed, so a title style you reuse can live in components/ui/editable.

Is the Editable accessible to keyboard and screen reader users?

Yes, once you name it. Give the FieldLabel an id and pass it to Editable in ids as label, as the demos do, and the input takes its name from the label, while the text, a button, reads the label and then the value. With no label at all, the input is called "editable input".

The text is in the tab order unless activationMode is none, and in every other mode Enter, Space or F2 opens it. In the field, Enter saves and Escape cancels, and with none an EditableEditTrigger is the way in, as the Activation none demo shows.

API Reference

Editable

PropType
edit?boolean

Whether the editable is in edit mode.

defaultEdit?boolean

Whether the editable is in edit mode by default.

onEditChange?(details: { edit: boolean }) → void

Function to call when the edit mode changes.

Type
(details: { edit: boolean }) → void
Parameters

details – { edit: boolean }

Returns
void
value?string

The value. Pair with onValueChange to control it.

defaultValue?string

The value it starts with, when you don't control value.

onValueChange?(details: { value: string }) → void

Called when the value changes.

Type
(details: { value: string }) → void
Parameters

details – { value: string }

Returns
void
activationMode?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.

Default
"focus"
disabled?boolean

Whether it's disabled.

finalFocusEl?() → HTMLElement | null

The element to receive focus when the editable is closed.

Type
() → HTMLElement | null
Returns
HTMLElement | null
form?string

The id of the form it submits with, when it isn't inside one.

ids?object

Element ids. label names the input and the preview from a label of your own.

Type
{ area?: string; control?: string; input?: string; label?: string; preview?: string; root?: string }
invalid?boolean

Whether it's invalid.

maxLength?number

The maximum number of characters allowed in the editable.

name?string

The name it submits with its form.

onValueCommit?(details: { value: string }) → void

Called when an interaction ends with a new value: the pointer is released, or a key sets it.

Type
(details: { value: string }) → void
Parameters

details – { value: string }

Returns
void
onValueRevert?(details: { value: string }) → void

Function to call when the value is reverted.

Type
(details: { value: string }) → void
Parameters

details – { value: string }

Returns
void
orientation?"horizontal" | "vertical"

The orientation of the editable.

Type
"horizontal" | "vertical"
Default
"horizontal"
placeholder?string | { edit: string; preview: string }

The placeholder text for the editable.

Type
string | { edit: string; preview: string }
readOnly?boolean

Whether the editable is read-only.

required?boolean

Whether a value is required to submit its form.

selectOnFocus?boolean

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

Default
true
submitMode?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.

Default
"both"
translations?object

The translations for the editable.

Type
{ cancel?: string; edit?: string; input?: string; submit?: string }
render?ReactElement | ((props: object, state: object) => ReactElement)

Replace the host element or compose it with another component.

Type
ReactElement | ((props: object, state: object) => ReactElement)
className?string

Merged with the part's classes.

EditableArea

PropType
render?ReactElement | ((props: object, state: object) => ReactElement)

Replace the host element or compose it with another component.

Type
ReactElement | ((props: object, state: object) => ReactElement)
className?string

Merged with the part's classes.

EditableInput

PropType
render?ReactElement | ((props: object, state: object) => ReactElement)

Replace the host element or compose it with another component.

Type
ReactElement | ((props: object, state: object) => ReactElement)
className?string

Merged with the part's classes.

EditablePreview

PropType
size?"sm" | "md" | "lg"

The size.

Type
"sm" | "md" | "lg"
Default
"md"
render?ReactElement | ((props: object, state: object) => ReactElement)

Replace the host element or compose it with another component.

Type
ReactElement | ((props: object, state: object) => ReactElement)
className?string

Merged with the part's classes.

AttributeType
data-size?string

Mirrors size.

EditableControl

PropType
render?ReactElement | ((props: object, state: object) => ReactElement)

Replace the host element or compose it with another component.

Type
ReactElement | ((props: object, state: object) => ReactElement)
className?string

Merged with the part's classes.

EditableEditTrigger

EditableCancelTrigger

EditableSubmitTrigger

useEditable

EditableContext