# Inline edit

A name you rename in place: plain text until clicked, then a field in the same box; Enter saves with a drawn tick and Esc puts the old text back.

Tier: free. Type: component. Category: inputs. Tags: keyboard, undo.

## Install

```bash
npx shadcn@latest add https://pixilui.com/r/inline-edit.json
```

One-time setup: add the registry to `components.json`:

```json
{
  "registries": {
    "@pixilui": "https://pixilui.com/r/{name}.json"
  }
}
```

Files: `components/pixilui/inline-edit.tsx`.

## Preview

Live preview: https://pixilui.com/components/inline-edit

Knobs:

- `list`: `runs`, `agents`

Signature: Rename a run and press Enter: the field melts back into text and a small tick draws beside it, then leaves.

Styles: `lively` (default), `quiet`, set with `data-style` on <html>. The item reads the style through the foundation; it needs no prop. Standalone files: lively https://pixilui.com/r/standalone/inline-edit.tsx, quiet https://pixilui.com/r/standalone/inline-edit.quiet.tsx.

## Code

`components/pixilui/inline-edit.tsx`

```tsx
"use client";

/*
  Inline edit, locked 1 Dec 2026 (design/explorations/components/locked/inputs, B8). The frame is the
  Field's (../field/field); the look is foundation.css section 8.

  At rest   plain text in a row. Pointer over it: a soft fill comes in and the pencil fades in (150ms).
  Editing   a click, or Enter or F2 on the focused row, makes the same box a field in place with the focus
            ring; the text is selected. Its surface comes and goes in 150ms.
  Saving    Enter or leaving saves; Esc puts the old text back; an empty name is never saved. A change
            draws the done tick, which stays for --pixilui-field-confirm-hold (1600ms).
  Reduced   the same: nothing here travels.
*/

import { forwardRef, useImperativeHandle, useRef, useState, type ComponentPropsWithoutRef, type KeyboardEvent, type ReactNode } from "react";
import { Field, FieldControl, FieldInput, FieldSuffix, fieldTime } from "./field";

export type InlineEditProps = Omit<ComponentPropsWithoutRef<"input">, "size" | "value" | "defaultValue" | "onChange" | "readOnly"> & {
  /** What the text is, for screen readers ("Run name"): the row shows no label. */
  "aria-label": string;
  /** The saved text, controlled. Pair with onSave. */
  value?: string;
  /** The starting text when uncontrolled. */
  defaultValue?: string;
  /** Runs with the new text when an edit is saved with a change. */
  onSave?: (value: string) => void | Promise<void>;
  /** The pencil that fades in on hover (the animated edit icon). */
  editIcon?: ReactNode;
  /** The tick shown after a save (the animated done icon draws it). */
  savedIcon?: ReactNode;
  /** Classes for the field. Native attributes and the ref go to the input. */
  className?: string;
};

/** A name you click to rename in place: Enter saves with a tick, Esc puts it back. */
export const InlineEdit = forwardRef<HTMLInputElement, InlineEditProps>(function InlineEdit(
  { value, defaultValue = "", onSave, editIcon, savedIcon, className, onKeyDown, onBlur, id, ...props },
  ref,
) {
  const input = useRef<HTMLInputElement | null>(null);
  const field = useRef<HTMLLabelElement | null>(null);
  useImperativeHandle(ref, () => input.current!, []);
  const [savedValue, setSavedValue] = useState(defaultValue);
  const committed = value ?? savedValue;
  const [draft, setDraft] = useState<string | null>(null);
  const [saved, setSaved] = useState(false);
  const timer = useRef<ReturnType<typeof setTimeout> | undefined>(undefined);
  const editing = draft !== null;
  // the edit's state as of now, for the blur that follows Enter or Esc within the same event
  const live = useRef<string | null>(null);

  const start = () => {
    if (live.current !== null) return;
    live.current = committed;
    setDraft(committed);
    setSaved(false);
    clearTimeout(timer.current);
    const el = input.current;
    if (!el) return;
    el.readOnly = false;
    el.focus();
    el.select();
  };
  const end = (save: boolean) => {
    const now = live.current;
    if (now === null) return;
    live.current = null;
    const next = save && now.trim() ? now : committed;
    setDraft(null);
    if (save && next !== committed) {
      if (value == null) setSavedValue(next);
      void onSave?.(next);
      setSaved(true);
      clearTimeout(timer.current);
      timer.current = setTimeout(() => setSaved(false), fieldTime(field.current, "--pixilui-field-confirm-hold"));
    }
    const el = input.current;
    if (el) {
      try {
        el.setSelectionRange(0, 0);
      } catch {
        // nothing to select
      }
      el.scrollLeft = 0;
    }
  };

  return (
    <Field ref={field} className={className} kind="inline" editing={editing} saved={saved} filled={(editing ? draft : committed).length > 0} inputId={id}>
      <FieldControl data-pxi="" onClick={start}>
        <FieldInput
          ref={input}
          type="text"
          data-inline=""
          readOnly={!editing}
          value={editing ? draft : committed}
          onChange={(event) => {
            live.current = event.target.value;
            setDraft(event.target.value);
          }}
          onKeyDown={(event: KeyboardEvent<HTMLInputElement>) => {
            onKeyDown?.(event);
            if (event.defaultPrevented || event.nativeEvent.isComposing) return;
            if (!editing) {
              if (event.key === "Enter" || event.key === "F2") {
                event.preventDefault();
                start();
              }
              return;
            }
            if (event.key === "Enter" || event.key === "Escape") {
              event.preventDefault();
              end(event.key === "Enter");
              event.currentTarget.blur();
            }
          }}
          onBlur={(event) => {
            onBlur?.(event);
            end(true);
          }}
          {...props}
        />
        <FieldSuffix>
          {editIcon != null ? <span className="pixilui-field-edit">{editIcon}</span> : null}
          {savedIcon != null ? (
            <span className="pixilui-field-saved" data-state={saved ? "done" : undefined}>
              {savedIcon}
            </span>
          ) : null}
        </FieldSuffix>
      </FieldControl>
    </Field>
  );
});
```

## Usage

When to use:

- Renaming a run, an agent or a file where it is listed.

When not to use:

- A form field: use Input.

```tsx
import { IconDone } from "@/components/pixilui/icons/done";
import { IconEdit } from "@/components/pixilui/icons/edit";
import { InlineEdit } from "@/components/pixilui/inline-edit";

// The first option of each knob is the demo's default; the docs preview starts there.
export const knobs = {
  list: ["runs", "agents"],
} as const;

const names = {
  runs: ["Refund for Lena Ortiz", "Seat audit for Hana Ito", "Late refund for Dev Patel"],
  agents: ["Support agent", "Billing reviewer", "Churn digest"],
};

/** A run list whose names rename in place: click one, or focus it and press Enter. */
export default function InlineEditDemo({ list = "runs" }: KnobValues<typeof knobs>) {
  return (
    <ul className="flex w-full max-w-sm flex-col gap-1">
      {names[list].map((name) => (
        <li key={name}>
          <InlineEdit aria-label={list === "runs" ? "Run name" : "Agent name"} defaultValue={name} editIcon={<IconEdit />} savedIcon={<IconDone />} />
        </li>
      ))}
    </ul>
  );
}
```

## Examples

None.

## API

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `className` | `string` |  | Classes for the field. Native attributes and the ref go to the input. |
| `aria-label` (required) | `string` |  | What the text is, for screen readers ("Run name"): the row shows no label. |
| `value` | `string` |  | The saved text, controlled. Pair with onSave. |
| `defaultValue` | `string` | `""` | The starting text when uncontrolled. |
| `onSave` | `(value: string) => void \| Promise<void>` |  | Runs with the new text when an edit is saved with a change. |
| `editIcon` | `ReactNode` |  | The pencil that fades in on hover (the animated edit icon). |
| `savedIcon` | `ReactNode` |  | The tick shown after a save (the animated done icon draws it). |

Also accepts every native `<input>` attribute except `size`, `value`, `defaultValue`, `onChange`, `readOnly`, and a ref to it.

## Keyboard

- `Enter` + `F2`: On the focused row, starts editing with the text selected.
- `Enter`: Saves.
- `Escape`: Puts the old text back.

## Accessibility

- Named by its aria-label; read-only until editing, so it reads as text.

## Motion

- The pencil fades in on hover (150ms); the field's surface comes and goes in 150ms; on save the done icon draws its tick for 1.6s.

## Responsive

- Fills its container's width; the label row and the message wrap.

## Performance

- Native typing: nothing renders per key beyond the value; the look is CSS in the foundation (section 8).

## Notes for AI

- <InlineEdit aria-label="Run name" defaultValue="Refund for Lena Ortiz" onSave={rename} editIcon={...} savedIcon={...} />.
- An empty name is never saved; leaving the field saves.

## Related

- [Input](https://pixilui.com/components/input/markdown): A text field with its label, help and error, a unit or icon on either side, and a rule that checks on blur and Enter and says what's wrong in plain words.
- [Field](https://pixilui.com/components/field/markdown): The frame every PixilUI field shares: a label row, a well with a rim you can see on white, and a message that says what's wrong in plain words, wired for screen readers.
