# Editable

Text that turns into a field when you edit it.

Text that turns into a field when you edit it: a title, a name, a value in a row. The field is the real Input; the preview takes the Input's box without its fill (same height, padding and type), so the text does not move when the field takes its place. inline takes the type of the text around it, for a value in a TextRow. Enter or a click away saves, Escape cancels; optional edit, save and cancel buttons. Behaviour, focus and ARIA come from the Zag editable machine, through Ark UI.

Kind: component · Category: Inputs · Status: beta · Targets: React, Vue, Svelte · Behaviour: Zag `editable` machine, through Ark UI

## Add

```bash
npx fndn add editable
```

It writes the folder `editable/` into the project's ui folder (`index.ts`, `editable.tsx`, `Editable.vue`, `Editable.svelte`, `editable.recipe.ts`), and what it is built from: [Input](/components/input.md), [Button](/components/button.md), [Tooltip](/components/tooltip.md). First time: [Installation](/installation.md).

## Usage

```tsx
import { Editable } from "@/components/ui/editable";
```

```vue
import { Editable } from "@/components/ui/editable";
```

```svelte
import { Editable } from "$lib/components/ui/editable";
```

## Parts

In the DOM the root carries `data-slot="editable"` and each part `data-slot="editable-<part>"`; in the recipe the root is the `base` slot and each part a slot named after it in camelCase.

| Part | Element | data-slot | Flags | Notes |
| --- | --- | --- | --- | --- |
| `root` | `div` | `editable` |  | Label above; the area and the controls share a row |
| `label` | `label` | `editable-label` | optional |  |
| `area` | `div` | `editable-area` |  | Holds the preview and the input; one shows at a time (hidden attributes from the engine) |
| `preview` | `div` | `editable-preview` |  | The text at rest; a tab stop (tabindex=0). field: the Input's box without the fill (the size's height, padding and type), its line box placed where the input centres its text. inline: the surrounding text, 6px padding taken back by a negative margin. Hover fill (pointer); ink-3 placeholder while empty; one line, cut at the end with a title. multiline: at least the size's height, the textarea's padding and 1.5 leading, wrapped and shown whole. |
| `input` | `input` | `editable-input` |  | The real Input (asChild), marked data-editable="input": its box, ring and sizes. inline: the surrounding text's size and line height, chip radius. multiline: the Input's textarea (multiline, rows 1, autoresize), at least the size's height. |
| `control` | `div` | `editable-control` | optional | With controls: the edit button at rest, the save and cancel buttons while editing (the engine hides the others). |
| `edit-trigger` | `button` | `editable-edit-trigger` | optional | The real Button (ghost, xs, square, muted) in the real Tooltip, marked data-editable="edit-trigger"; pencil icon |
| `submit-trigger` | `button` | `editable-submit-trigger` | optional | The real Button in the real Tooltip, marked data-editable="submit-trigger"; check icon |
| `cancel-trigger` | `button` | `editable-cancel-trigger` | optional | The real Button in the real Tooltip, marked data-editable="cancel-trigger"; x icon |

## Props

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `value` | `string` |  | Bindable (React: `defaultValue` and `onValueChange`; Vue: `v-model:value`; Svelte: `bind:value`). The text. |
| `placeholder` | `string` | `"Empty"` | Shown while the text is empty, in the preview and in the field. |
| `label` | `string` |  | Visible label; otherwise pass aria-label (it names the preview and the field). |
| `variant` | `"field" \| "inline"` | `"field"` | field: the Input's box and sizes. inline: the type of the text around it (a value in a TextRow). |
| `size` | `"xs" \| "sm" \| "md" \| "lg"` | `"md"` | field only: the Input's sizes (`lib/control.ts`): xs 24px, sm 28px, md 32px, lg 36px. |
| `activationMode` | `"focus" \| "click" \| "dblclick"` | `"focus"` | What starts an edit: focus (Tab or a click), a click, or a double click. |
| `controls` | `boolean` | `false` | Edit, save and cancel buttons beside the text. |
| `multiline` | `boolean` | `false` | Several lines (a description): the field is the Input's textarea, one line high and growing with its text (autoresize, no handle); the preview takes the same box and shows the text whole, wrapped (pre-wrap), so nothing moves when the field takes its place. Enter is a new line; Cmd/Ctrl+Enter or a click away saves; Escape cancels. field variant only. |
| `maxLength` | `number` |  |  |
| `disabled` | `boolean` | `false` |  |
| `invalid` | `boolean` | `false` |  |
| `name` | `string` |  | Form field name; the input carries it. |

Other attributes and `class` go to the root; `class` is merged through the recipe.

## Events

| Event | Payload |
| --- | --- |
| `valueChange` | (value: string) on every change while editing |
| `valueCommit` | (value: string) when the text is saved |
| `valueRevert` | (value: string) when an edit is cancelled |

## States

`hover`, `focus-visible`, `editing`, `empty`, `disabled`, `invalid`

## Keyboard and accessibility

Keyboard:

- Tab reaches the preview; with activationMode focus it starts the edit at once, otherwise Enter does
- Enter saves; Escape cancels and puts the text back
- Tab, or a click away, saves
- Focus returns to the preview after saving or cancelling
- multiline: Enter adds a line; Cmd+Enter (Ctrl+Enter off Apple) saves; Escape cancels

Notes:

- Give it a visible label or an aria-label: it names both the preview and the field.
- The preview is one line and cut at the end; its title holds the whole text.
- The edit, save and cancel buttons are icon-only: each is named by its Tooltip and its aria-label.

## Motion

- preview: hover fill over duration.fast (pointer only)
- the swap between preview and field is instant: nothing moves, the text stays where it is
- field: the Input's ring over duration.base
- buttons: the Button's press (motion.press-scale over duration.press)

## Tokens

`color.field`, `color.surface`, `color.hover`, `color.ink`, `color.ink-3`, `color.accent`, `color.accent-tint`, `color.red`, `color.red-tint`, `shadow.hairline`, `radius.control`, `radius.chip`, `text.xs`, `text.sm`, `duration.fast`, `duration.base`

## Dependencies

- [Input](/components/input.md)
- [Button](/components/button.md)
- [Tooltip](/components/tooltip.md)

## Examples

### Rename a project

React · `project-name.tsx`

```tsx
import { Editable } from "@/components/ui/editable";

/* reads as text until focused or clicked; Enter saves, Escape puts the old name back */
export default function ProjectName() {
  return <Editable label="Project name" defaultValue="billing-api" style={{ width: 260 }} />;
}
```

Vue · `project-name.vue`

```vue
<script setup lang="ts">
import { ref } from "vue";
import { Editable } from "@/components/ui/editable";

const name = ref("billing-api");
</script>

<!-- reads as text until focused or clicked; Enter saves, Escape puts the old name back -->
<template>
  <Editable v-model:value="name" label="Project name" style="width: 260px" />
</template>
```

Svelte · `project-name.svelte`

```svelte
<script lang="ts">
  import { Editable } from "$lib/components/ui/editable";

  let name = $state("billing-api");
</script>

<!-- reads as text until focused or clicked; Enter saves, Escape puts the old name back -->
<Editable bind:value={name} label="Project name" style="width: 260px" />
```

### Release title, with buttons

React · `release-title.tsx`

```tsx
import { Editable } from "@/components/ui/editable";

/* controls: Edit, Save and Cancel buttons for people who do not guess the double-click */
export default function ReleaseTitle() {
  return (
    <Editable
      label="Release title"
      defaultValue="v2.14: usage alerts and invoice fixes"
      controls
      activationMode="dblclick"
      style={{ width: 340 }}
    />
  );
}
```

Vue · `release-title.vue`

```vue
<script setup lang="ts">
import { ref } from "vue";
import { Editable } from "@/components/ui/editable";

const title = ref("v2.14: usage alerts and invoice fixes");
</script>

<!-- controls: Edit, Save and Cancel buttons for people who do not guess the double-click -->
<template>
  <Editable
    v-model:value="title"
    label="Release title"
    controls
    activation-mode="dblclick"
    style="width: 340px"
  />
</template>
```

Svelte · `release-title.svelte`

```svelte
<script lang="ts">
  import { Editable } from "$lib/components/ui/editable";

  let title = $state("v2.14: usage alerts and invoice fixes");
</script>

<!-- controls: Edit, Save and Cancel buttons for people who do not guess the double-click -->
<Editable
  bind:value={title}
  label="Release title"
  controls
  activationMode="dblclick"
  style="width: 340px"
/>
```

### Project details, edited in place

React · `project-details.tsx`

```tsx
import { Editable } from "@/components/ui/editable";
import { TextRow } from "@/components/ui/text-row";

/* inline: the value of a detail row, edited in place */
export default function ProjectDetails() {
  return (
    <div style={{ width: 320 }}>
      <TextRow label="Production branch" value={<Editable variant="inline" aria-label="Production branch" defaultValue="main" />} />
      <TextRow label="Build command" value={<Editable variant="inline" aria-label="Build command" defaultValue="pnpm build" />} />
      <TextRow label="Build timeout" value={<Editable variant="inline" aria-label="Build timeout in minutes" defaultValue="15" />} meta="min" />
    </div>
  );
}
```

Vue · `project-details.vue`

```vue
<script setup lang="ts">
import { ref } from "vue";
import { Editable } from "@/components/ui/editable";
import { TextRow } from "@/components/ui/text-row";

const branch = ref("main");
const command = ref("pnpm build");
const timeout = ref("15");
</script>

<!-- inline: the value of a detail row, edited in place -->
<template>
  <div style="width: 320px">
    <TextRow label="Production branch">
      <template #value><Editable v-model:value="branch" variant="inline" aria-label="Production branch" /></template>
    </TextRow>
    <TextRow label="Build command">
      <template #value><Editable v-model:value="command" variant="inline" aria-label="Build command" /></template>
    </TextRow>
    <TextRow label="Build timeout" meta="min">
      <template #value><Editable v-model:value="timeout" variant="inline" aria-label="Build timeout in minutes" /></template>
    </TextRow>
  </div>
</template>
```

Svelte · `project-details.svelte`

```svelte
<script lang="ts">
  import { Editable } from "$lib/components/ui/editable";
  import { TextRow } from "$lib/components/ui/text-row";

  let branch = $state("main");
  let command = $state("pnpm build");
  let timeout = $state("15");
</script>

<!-- inline: the value of a detail row, edited in place -->
{#snippet branchValue()}<Editable bind:value={branch} variant="inline" aria-label="Production branch" />{/snippet}
{#snippet commandValue()}<Editable bind:value={command} variant="inline" aria-label="Build command" />{/snippet}
{#snippet timeoutValue()}<Editable bind:value={timeout} variant="inline" aria-label="Build timeout in minutes" />{/snippet}

<div style="width: 320px">
  <TextRow label="Production branch" value={branchValue} />
  <TextRow label="Build command" value={commandValue} />
  <TextRow label="Build timeout" value={timeoutValue} meta="min" />
</div>
```

### Issue description, several lines

React · `issue-description.tsx`

```tsx
import { Editable } from "@/components/ui/editable";

const description = "Webhook deliveries that time out are dropped today.\nRetry them three times with backoff, then mark the event failed.";

/* a description reads as plain text; the field appears only while editing. Enter starts a new line,
 * Cmd/Ctrl+Enter or a click away saves */
export default function IssueDescription() {
  return <Editable aria-label="Description" multiline size="sm" defaultValue={description} placeholder="Add a description…" style={{ width: 360 }} />;
}
```

Vue · `issue-description.vue`

```vue
<script setup lang="ts">
import { ref } from "vue";
import { Editable } from "@/components/ui/editable";

const description = ref("Webhook deliveries that time out are dropped today.\nRetry them three times with backoff, then mark the event failed.");
</script>

<!-- a description reads as plain text; the field appears only while editing. Enter starts a new line,
     Cmd/Ctrl+Enter or a click away saves -->
<template>
  <Editable v-model:value="description" aria-label="Description" multiline size="sm" placeholder="Add a description…" style="width: 360px" />
</template>
```

Svelte · `issue-description.svelte`

```svelte
<script lang="ts">
  import { Editable } from "$lib/components/ui/editable";

  let description = $state("Webhook deliveries that time out are dropped today.\nRetry them three times with backoff, then mark the event failed.");
</script>

<!-- a description reads as plain text; the field appears only while editing. Enter starts a new line,
     Cmd/Ctrl+Enter or a click away saves -->
<Editable bind:value={description} aria-label="Description" multiline size="sm" placeholder="Add a description…" style="width: 360px" />
```

### Saved view in a sidebar

React · `saved-view.tsx`

```tsx
import { Editable } from "@/components/ui/editable";

/* sm: a saved view's name in a sidebar, renamed where it is */
export default function SavedView() {
  return <Editable size="sm" aria-label="View name" defaultValue="Failed deploys this week" style={{ width: 220 }} />;
}
```

Vue · `saved-view.vue`

```vue
<script setup lang="ts">
import { ref } from "vue";
import { Editable } from "@/components/ui/editable";

const name = ref("Failed deploys this week");
</script>

<!-- sm: a saved view's name in a sidebar, renamed where it is -->
<template>
  <Editable v-model:value="name" size="sm" aria-label="View name" style="width: 220px" />
</template>
```

Svelte · `saved-view.svelte`

```svelte
<script lang="ts">
  import { Editable } from "$lib/components/ui/editable";

  let name = $state("Failed deploys this week");
</script>

<!-- sm: a saved view's name in a sidebar, renamed where it is -->
<Editable bind:value={name} size="sm" aria-label="View name" style="width: 220px" />
```

## Recipe

`editable.recipe.ts`:

```ts
import { tv, type VariantProps } from "../lib/tv";
import { controlSize } from "../lib/control";

/* editable — text that turns into a field when you edit it (a title, a name, a value in a row).
 * Behaviour comes from the Zag editable machine: the preview and the input swap through their
 * hidden attributes; Enter or a click away saves, Escape cancels.
 *
 * The input is the real Input (asChild), so the field is the Input's box and ring. The preview
 * takes the same box without the fill: same height, same padding, same type, so the text does not
 * move when the field takes its place. A line box as tall as the control puts the preview's text
 * where the input centres its own. Hovered (pointer), it takes the hover fill: it can be edited.
 *
 *   field   the Input's sizes (the shared control's: xs 24 · sm 28 · md 32 · lg 36)
 *   inline  the text around it (font, size, weight, line height), 6px of padding taken back by a
 *           negative margin so the text lines up with its neighbours: a value in a TextRow
 *
 * multiline (a description): the field is the Input's textarea, one line high and growing with its
 * text (autoresize, no handle, no 72px minimum), and the preview takes the same box: the size's
 * height as a minimum, the textarea's padding and 1.5 leading, the text wrapped and shown whole
 * (pre-wrap). The text stays where it is when the field takes its place; only the ring appears.
 * The engine handles it: Enter is a new line, Cmd/Ctrl+Enter or a click away saves.
 *
 * The controls (edit, save, cancel) are real Buttons (ghost, xs, square, muted) with Tooltips,
 * marked data-editable="edit-trigger|submit-trigger|cancel-trigger". */

export const editable = tv({
  slots: {
    /* label above; the area and the controls share a row */
    base: "grid grid-cols-[minmax(0,1fr)] items-center gap-x-1 gap-y-1.5 min-w-0 max-w-full",
    label: "col-span-full text-sm font-medium text-ink",
    area: "flex min-w-0",
    preview: [
      "block w-full min-w-0 overflow-hidden text-ellipsis whitespace-nowrap rounded-control text-ink cursor-text",
      /* the text keeps its own direction; it stays on the page's side, set physically from the
       * element's direction (plaintext does not change it), as the TextRow does */
      "[unicode-bidi:plaintext] [&:dir(ltr)]:text-left [&:dir(rtl)]:text-right",
      "hover:bg-hover motion-colors-fast",
      "data-placeholder-shown:text-ink-3",
      "data-disabled:opacity-50 data-disabled:pointer-events-none",
    ],
    input: "",
    /* room for the two buttons of an edit (save, cancel) at rest too, so the field does not change
     * width when the edit starts */
    control: "inline-flex items-center gap-0.5 min-w-[50px]",
  },
  variants: {
    variant: {
      field: {},
      inline: {
        base: "inline-grid",
        /* w-auto flex-1: the box is the area plus the 6px it reaches out on each side, so the text
         * lines up with its neighbours and is not cut by the padding (w-full kept the area's width
         * and slid it 6px towards the start: the text ran 6px past the end). The input likewise. */
        preview: "w-auto flex-1 -mx-1.5 px-1.5 rounded-chip [font:inherit]",
        input: "w-auto flex-1 h-auto min-h-0 pointer-coarse:min-h-0 -mx-1.5 px-1.5 py-0 rounded-chip text-[length:inherit] leading-[inherit] [font-family:inherit]",
      },
    },
    /* the Input's sizes (lib/control.ts) */
    size: {
      xs: {},
      sm: {},
      md: {},
      lg: {},
    },
    multiline: {
      true: {
        preview: "h-auto whitespace-pre-wrap wrap-break-word overflow-visible text-clip",
        input: "min-h-(--control-height) resize-none overflow-hidden",
      },
    },
    /* the buttons take a column of their own; with several lines they sit at the top */
    controls: {
      true: { base: "grid-cols-[minmax(0,1fr)_auto]" },
    },
  },
  compoundVariants: [
    /* field: the Input's box (the control's size: height, padding, text, radius) and the line box
     * of the Input's text, placed where the Input centres it (half of what the line leaves) */
    { variant: "field", class: { preview: "h-(--control-height) py-[calc((var(--control-height)-1lh)/2)] px-(--control-px) leading-[1.4]" } },
    { variant: "field", size: "xs", class: { preview: controlSize.xs } },
    { variant: "field", size: "sm", class: { preview: controlSize.sm } },
    { variant: "field", size: "md", class: { preview: controlSize.md } },
    { variant: "field", size: "lg", class: { preview: controlSize.lg } },
    /* multiline: the textarea's box (input.recipe.ts multiline: 1.5 leading, the block padding that centres
     * one line in the size's height), at least one line high, then as tall as its text */
    { variant: "field", multiline: true, class: { preview: "h-auto min-h-(--control-height) leading-[1.5]" } },
    { variant: "field", multiline: true, size: "xs", class: { preview: "py-[3px]" } },
    { variant: "field", multiline: true, size: "sm", class: { preview: "py-[5px]" } },
    { variant: "field", multiline: true, size: "md", class: { preview: "py-[7px]" } },
    { variant: "field", multiline: true, size: "lg", class: { preview: "py-2" } },
    { multiline: true, controls: true, class: { base: "items-start" } },
  ],
  defaultVariants: { variant: "field", size: "md", multiline: false },
});

export type EditableRecipeProps = VariantProps<typeof editable>;
```
