Inline edit
FreeA 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.
npx shadcn@latest add https://pixilui.com/r/inline-edit.jsonPreview
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>
);
}When to use
Use it when
- Renaming a run, an agent or a file where it is listed.
Use something else when
- A form field: use Input.
Installation
npx shadcn@latest add https://pixilui.com/r/inline-edit.jsonAdds inline-edit.tsx with field.tsx, and installs clsx and tailwind-merge. The URL works in any app with a components.json, with no registry to add first.
"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>
);
});
It uses Field: add it the same way. Then npm i clsx tailwind-merge, and add the foundation once per app, as in Installation.
Usage
import { InlineEdit } from "@/components/pixilui/inline-edit";
<InlineEdit aria-label={aria-label} onSave={onSave} />Fills its container's width; the label row and the message wrap.
API reference
aria-labelRequiredstring–What the text is, for screen readers ("Run name"): the row shows no label.valuestring–The saved text, controlled. Pair with onSave.defaultValuestring""The starting text when uncontrolled.onSave(value: string) => void | Promise<void>–Runs with the new text when an edit is saved with a change.editIconReactNode–The pencil that fades in on hover (the animated edit icon).savedIconReactNode–The tick shown after a save (the animated done icon draws it).classNamestring–Classes for the field. Native attributes and the ref go to the input.Also takes every native <input> attribute except size, value, defaultValue, onChange, readOnly, and a ref.
Keyboard
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.