# Inline edit

> Rename a title, a project or a row where it stands: the text becomes the field without moving a pixel, and saves itself.

Most rename-in-place fields swap the text for an input that sits a few pixels off, in a different size, with the caret thrown to the end. Here the field is already under the text, laid out letter for letter the same, so a click puts the caret exactly where you clicked and the only thing that changes is a hairline frame turning to the focus colour. It takes the type you set it in: the frame and its padding are in em, so a table cell and a page title both fit. Enter or leaving the field saves through your onSave. The new name stays put while a spinner opens beside it and a check draws itself; if your server stores something else (an address it turned into "relay-web"), the text morphs into what was stored. If it refuses, the field opens again with what you typed, gives a small shake and says why underneath. Escape morphs the text back to the saved name, and a new value from outside (someone else renamed it) morphs in unless you are in the middle of an edit.

## Install

```bash
npx shadcn@latest add @hairline/inline-edit
```

Needs React 19, Tailwind CSS v4 and shadcn/ui theme variables (any style, light and dark). Icons from lucide-react.

- npm: none
- Hairline UI components it needs (the CLI installs them too): `text-morph`, `number-roll`
- Docs and live preview: https://hairlineui.com/ui/inline-edit
- Registry item (source): https://hairlineui.com/r/inline-edit.json

## Usage

```tsx
import { InlineEdit } from "@/components/inline-edit";

<h1 className="text-2xl font-semibold">
  <InlineEdit label="Project name" defaultValue="Relay web" onSave={(name) => renameProject(name)} />
</h1>
```

## Saved by a server action

onSave gets the new name and the old one. Return what the database stored (here the address the server derives from it), or throw to keep the field open with your message.

```tsx
// app/projects/actions.ts
"use server";
import { db } from "@/lib/db";

export async function renameProject(id: string, name: string) {
  const user = await requireUser();
  const taken = await db.project.findFirst({ where: { workspaceId: user.workspaceId, name, NOT: { id } } });
  if (taken) return { error: `There's already a project called “${name}”.` };
  const project = await db.project.update({ where: { id }, data: { name } });
  return { name: project.name };
}

// app/projects/[id]/title.tsx
"use client";
import { InlineEdit } from "@/components/inline-edit";
import { renameProject } from "../actions";

export function ProjectTitle({ id, name }: { id: string; name: string }) {
  return (
    <h1 className="text-2xl font-semibold tracking-tight">
      <InlineEdit
        wrap
        label="Project name"
        defaultValue={name}
        maxLength={60}
        onSave={async (next) => {
          const result = await renameProject(id, next);
          if (result.error) throw new Error(result.error);
          return result.name;
        }}
      />
    </h1>
  );
}
```

## Props

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `label` | `string` | – | Accessible name of the field, e.g. "Project name". |
| `value` | `string` | – | The saved text, controlled. A new value morphs in when you are not editing. |
| `defaultValue` | `string` | `""` | The saved text when uncontrolled. |
| `onValueChange` | `(value) => void` | – | Called with the stored text after a successful save. |
| `onSave` | `(value, previous) => Promise<string \| void> \| string \| void` | – | Saves the new text. Return what was stored (it morphs in if different); throw an Error to refuse with its message. |
| `validate` | `(value) => string \| null` | – | Refuses a value before it is sent; the message opens under the field. |
| `normalize` | `(value) => string` | `collapse spaces, trim` | Applied before validating and saving. |
| `wrap` | `boolean` | `false` | Fill the width and wrap onto more lines (titles). Off, it is one line that sizes to its text and truncates with an ellipsis at rest. |
| `maxLength` | `number` | – | Stops typing at this length and counts the last ten characters. |
| `blurAction` | `"save" \| "cancel"` | `"save"` | What leaving the field does with an edit. |
| `allowEmpty` | `boolean` | `false` | Saves an empty value instead of going back to the saved one. |
| `placeholder` | `string` | `"Untitled"` | Shown while the text is empty. |
| `name` | `string` | – | Submits the saved value with a form. |

## States

- **rest**: Plain text in your own type. Under a mouse, a 1px frame sized by the text fades in over 160ms; it extends 0.4em either side, with negative margins, so the text stays aligned with what is around it.
- **edit**: A click focuses the field at the point you clicked (double-click selects a word); Tab, Enter or F2 select everything. The frame turns to the focus colour in 200ms. The field grows with the text: one-line fields widen, wrapped titles take more lines.
- **save**: Enter or leaving the field calls onSave with the trimmed value. The new text stays where it is; a 14px slot opens beside one-line text (380ms) with a spinner, then a check draws itself (420ms) and the slot closes 1.4s later. Wrapped titles say Saving, then Saved, in the line below.
- **stored**: If onSave returns a different string, the text morphs into it: shared letters glide, new ones rise out of a blur, and one-line text eases to its new width.
- **refused**: If validate returns a message or onSave throws, the field opens again with what you typed, shakes ±4px over 380ms, its frame turns red and the reason folds open under it. Typing clears the red.
- **cancel**: Escape morphs the text back to the saved value and keeps focus on the field; Enter edits it again. An emptied name cancels the same way unless allowEmpty is set.
- **elsewhere**: A new value prop while you are not editing morphs in (wrapped titles crossfade whole and ease to their new height in 420ms). While you are editing, your text is left alone and Escape goes back to the newer name.
- **limit**: With maxLength, the last ten characters are counted under the field; the number rolls down as you type.

## What it handles

Swapping text for an input looks like ten lines of code. Then the text jumps when it becomes a field, the caret lands at the end, a failed save throws away what was typed, and a teammate’s rename overwrites your edit. This handles all of it.

- **The text never moves**: A real field already sits under the text, letter for letter, so focusing it shifts nothing, in whatever font and size you set.
- **The caret lands where you click**: Clicks, double-clicks and drags select exactly as in any field; Tab or F2 selects it all.
- **Saves the way people expect**: Enter or leaving the field saves, Escape cancels and morphs back, and unchanged or emptied text never reaches your server.
- **Your server has the last word**: Return what you stored and the text morphs into it, so a trimmed name or a generated slug shows up exactly.
- **Failures keep the edit**: If your save throws, the field reopens with what was typed, gives a small shake and says why underneath.
- **Live renames don’t fight you**: A new value from a teammate morphs in, but never while someone is in the middle of an edit.

## Replaces

an edit-mode useState, an <input> swap, blur-to-save logic, optimistic updates, error toasts, a character counter

## Where it fits

- **Page titles**: Rename a project, a doc or an issue from its header, the way Linear and Notion do.
- **Table cells**: Fix a name or a label inside a data table row without opening a form.
- **Sidebars and breadcrumbs**: Rename a workspace, a folder or a saved view right where it’s listed.
- **Settings**: Change a slug, a display name or an API key’s label in place, with the server’s rules applied.

## Notes

- The field is a real textarea labelled by label, so screen readers, spellcheck, undo and the clipboard behave as anywhere else. Errors and the character count are linked with aria-describedby and marked aria-invalid; saves and refusals are announced in a status region outside the field.
- Text and field use the same letters in the same box with kerning and ligatures off, so focusing never moves a glyph. Line breaks pasted into a title become spaces.
- A refusal that arrives while you are already typing something newer keeps your new text and only shows the reason. Results from an older save are ignored.
- Type is inherited: set font size, weight and tracking on the component or its parent. On touch screens, text under 16px makes iOS zoom in when it is focused; set a 16px minimum on phones if that matters for your layout.
- Reduced motion changes everything in place: no morphs, shakes or easing; the colours and words still say what happened.

License: MIT (https://hairlineui.com/license#components)
