Skip to content
Primitives

Inline Edit

Click-to-edit text that swaps to an input in place and saves optimistically — rolls back if the save rejects. Removes a class of one-off edit modals.

Motionnone

Install

Install Kineto and follow the theme setup once in your app.

bash
npm install kineto
tsx
import { InlineEdit } from "kineto/inline-edit";

Preview

Click the text to edit · Enter saves · Esc cancels

tsx
"use client";

import * as React from "react";
import { Pencil } from "lucide-react";

import { cn } from "@/lib/cn";

export interface InlineEditProps
  extends Omit<React.HTMLAttributes<HTMLDivElement>, "onChange" | "onSubmit"> {
  /** The current value. */
  value: string;
  /** Persist a new value. May be async; if it rejects, the edit is rolled back. */
  onSave?: (value: string) => void | Promise<void>;
  placeholder?: string;
  /** Use a multi-line textarea instead of a single-line input. */
  multiline?: boolean;
  inputClassName?: string;
}

/**
 * Click-to-edit text that swaps to an input in place and saves optimistically — the new value shows
 * immediately and only rolls back if `onSave` rejects. Removes a whole class of one-off edit modals.
 * Enter (or blur) saves; Escape cancels. Shift+Enter inserts a newline in multiline mode.
 */
const InlineEdit = React.forwardRef<HTMLDivElement, InlineEditProps>(
  ({ className, value, onSave, placeholder = "Empty", multiline = false, inputClassName, ...props }, ref) => {
    const [committed, setCommitted] = React.useState(value);
    const [editing, setEditing] = React.useState(false);
    const [draft, setDraft] = React.useState(value);
    const [error, setError] = React.useState(false);
    const inputRef = React.useRef<HTMLInputElement & HTMLTextAreaElement>(null);

    // Keep in sync if the value changes externally while not editing.
    React.useEffect(() => {
      if (!editing) setCommitted(value);
    }, [value, editing]);

    const begin = () => {
      setDraft(committed);
      setError(false);
      setEditing(true);
    };

    React.useEffect(() => {
      if (editing) inputRef.current?.select();
    }, [editing]);

    const commit = async () => {
      const next = draft.trim();
      setEditing(false);
      if (next === committed) return;
      const previous = committed;
      setCommitted(next); // optimistic
      try {
        await onSave?.(next);
        setError(false);
      } catch {
        setCommitted(previous); // roll back
        setError(true);
      }
    };

    const cancel = () => {
      setDraft(committed);
      setEditing(false);
    };

    const onKeyDown = (e: React.KeyboardEvent) => {
      if (e.key === "Enter" && !(multiline && e.shiftKey)) {
        e.preventDefault();
        void commit();
      } else if (e.key === "Escape") {
        e.preventDefault();
        cancel();
      }
    };

    const sharedInputClass = cn(
      "w-full rounded-md border border-input bg-background px-2 py-1 text-inherit outline-none ring-ring focus-visible:ring-2",
      inputClassName
    );

    return (
      <div ref={ref} className={cn("min-w-0", className)} {...props}>
        {editing ? (
          multiline ? (
            <textarea
              ref={inputRef}
              value={draft}
              rows={3}
              onChange={(e) => setDraft(e.target.value)}
              onBlur={() => void commit()}
              onKeyDown={onKeyDown}
              className={cn(sharedInputClass, "resize-none")}
            />
          ) : (
            <input
              ref={inputRef}
              value={draft}
              onChange={(e) => setDraft(e.target.value)}
              onBlur={() => void commit()}
              onKeyDown={onKeyDown}
              className={sharedInputClass}
            />
          )
        ) : (
          <button
            type="button"
            onClick={begin}
            className={cn(
              "group inline-flex max-w-full items-center gap-1.5 rounded-md px-2 py-1 text-left hover:bg-muted focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring",
              error && "ring-2 ring-destructive"
            )}
          >
            <span className={cn("truncate", !committed && "text-muted-foreground")}>
              {committed || placeholder}
            </span>
            <Pencil className="size-3.5 shrink-0 text-muted-foreground opacity-0 transition-opacity group-hover:opacity-100" />
          </button>
        )}
      </div>
    );
  }
);
InlineEdit.displayName = "InlineEdit";

export { InlineEdit };

src/components/ui/inline-edit.tsx

Props

PropTypeDefaultDescription
valuestring—The current value.
onSave(value: string) => void | Promise<void>—Persist a new value; if it rejects, the edit rolls back.
placeholderstring"Empty"Shown when the value is empty.
multilinebooleanfalseUse a textarea (Shift+Enter for newlines).