# Inline edit

> Click-to-edit values - popover or inline editing, click, double-click or edit-button triggers, an async save action with pending, abort and error states, Data Grid validation rules and a ref API.

Canonical: https://salmonui.dev/docs/editors/inline-edit

`InlineEdit` from `@salmonui/editors/inline-edit` shows a value as text and
turns it into an editor when the user activates it.

```tsx
import { InlineEdit } from '@salmonui/editors/inline-edit';
import '@salmonui/editors/inline-edit/styles.css';

<InlineEdit
  label="Title"
  value={title}
  save={async (value, { signal }) => {
    await updateTitle(value, { signal });
    setTitle(value);
  }}
  validation={{ required: true, maxLength: [80, 'At most 80 characters'] }}
  placeholder="Untitled"
/>
```

The stylesheet carries the rules of the Save and Cancel `Button`s, so
`inline-edit/styles.css` is the only stylesheet besides
`@salmonui/core/styles.css` (the theme, once per app).

### Live demo: Inline edit

Live demo: https://salmonui.dev/demos/editors/inline-edit

Click-to-edit fields - a title in a popover with validation and a slow save, a number in place with a range rule, and a notes field that saves with Ctrl+Enter.

`Demo.tsx`:

```tsx
'use client';

import { InlineEdit } from '@salmonui/editors/inline-edit';
import { NumberField } from '@salmonui/inputs/number-field';
import { TextArea } from '@salmonui/inputs/text-field';
import { EventLog, useEventLog } from '../../components/demo-kit';

const wait = (ms: number, signal: AbortSignal): Promise<void> =>
  new Promise((resolve, reject) => {
    const timer = setTimeout(resolve, ms);
    signal.addEventListener('abort', () => {
      clearTimeout(timer);
      reject(new Error('aborted'));
    });
  });

export default function InlineEditDemo() {
  const [log, push] = useEventLog();
  return (
    <div style={{ display: 'grid', gap: 16, maxWidth: 560 }}>
      <div>
        <strong>Title</strong>{' '}
        <InlineEdit
          label="Title"
          defaultValue="Quarterly report"
          placeholder="Untitled"
          validation={{ required: true, maxLength: [40, 'At most 40 characters'] }}
          save={async (value, { signal }) => {
            await wait(600, signal);
            if (value === 'fail') throw new Error('Server unavailable');
          }}
          onSave={(e) => push(`saved "${e.value}" (${e.trigger})`)}
          onSaveError={() => push('save failed')}
          onEditCancel={(e) => push(`cancelled (${e.trigger})`)}
        />
      </div>
      <div>
        <strong>Quantity</strong>{' '}
        <InlineEdit<number>
          label="Quantity"
          mode="inline"
          defaultValue={3}
          validation={{ range: [1, 10] }}
          renderPreview={({ value }) => `${value} pcs`}
          onSave={(e) => push(`quantity ${e.previousValue} -> ${e.value}`)}
        >
          {({ value, setValue, inputProps }) => (
            <NumberField
              id={inputProps.id}
              aria-label={inputProps['aria-label']}
              aria-describedby={inputProps['aria-describedby']}
              invalid={inputProps['aria-invalid']}
              autoFocus
              min={1}
              max={10}
              value={value}
              onValueChange={(next) => setValue(next ?? 0)}
              onKeyDown={inputProps.onKeyDown}
            />
          )}
        </InlineEdit>
      </div>
      <div>
        <strong>Notes</strong>{' '}
        <InlineEdit label="Notes" mode="inline" trigger="editButton" blurAction="keep" defaultValue="Ship on Friday" onSave={() => push('notes saved')}>
          {({ inputProps }) => <TextArea {...inputProps} rows={3} />}
        </InlineEdit>
      </div>
      <EventLog entries={log} />
    </div>
  );
}
```

## Display state

The saved value comes from `value` (controlled) or `defaultValue`
(uncontrolled, default `''`). The preview shows `String(value)`;
`renderPreview({ value, placeholder })` replaces it with any React node, for
example a label lookup for a list editor or a unit suffix. An empty
value shows `placeholder`, or `localeText.empty` (`'Empty'`) when no
placeholder is given. Values render as text: there is no HTML
parsing or sanitizer switch (see [Design notes](https://salmonui.dev/docs/editors/inline-edit#design-notes)).

The preview is a `<button>`. With `label`, a visually hidden `label: ` prefix
inside it makes the accessible name "Title: Quarterly report" while the
visible text stays the value.

## Modes and triggers

- `mode="popover"` (default) opens the editor in a
  non-modal dialog anchored to the preview; `mode="inline"` replaces the
  preview with the editor. The popover mounts only while editing and
  never in server HTML. `popoverProps` sets `placement` (default
  `'bottom-start'`, logical `start` / `end` mirror in RTL) and `offset`
  (default 4 px); the popover fades in with one CSS animation that
  `prefers-reduced-motion` turns off. Below 480 px the popover sits
  at the bottom edge of the viewport (CSS only).
- `trigger` is `'click'` (default), `'doubleClick'` or `'editButton'`.
With `'doubleClick'` a single click only focuses the preview;
  Enter or Space still start editing, and on coarse pointers (touch) a
  visible edit button appears. With `'editButton'` the preview is plain text
  and a pencil button labelled "Edit {label}" starts editing.
- `disabled` makes the preview inert (`aria-disabled`, still focusable).
- `editing` / `defaultEditing` / `onEditingChange(editing, { source, reason })`
  control the editing state: `reason` is `'start'`, `'save'` or
  `'cancel'`.

## Editors

`children` is a render prop. It receives `{ value, setValue, inputProps,
save, cancel, pending, errors }`: `value` is the draft,
`inputProps` wires a text-like input (`value`, `onChange`, Enter and Escape
handling, `aria-invalid`, `aria-describedby`, `aria-label`, `readOnly` while
saving, `autoFocus`). Without `children` a text input is rendered.

```tsx
<InlineEdit<number> label="Quantity" mode="inline" defaultValue={3} validation={{ range: [1, 10] }} renderPreview={({ value }) => `${value} pcs`}>
  {({ value, setValue, inputProps }) => (
    <input {...inputProps} type="number" value={String(value)} onChange={(e) => setValue(e.target.valueAsNumber)} />
  )}
</InlineEdit>
```

There is no editor `type` enum: number, date, date-time, time,
date range, dropdown, autocomplete, multi-select, masked, color and slider
editors are compositions through `children`, so
`@salmonui/editors/inline-edit` imports none of them and an application pays
only for the fields it uses:

```tsx
import { NumberField } from '@salmonui/inputs/number-field';

<InlineEdit<number | null> label="Quantity" value={quantity} validation={{ range: [0, 100] }} save={saveQuantity}>
  {({ value, setValue, inputProps: { value: _text, onChange: _change, ...wiring } }) => (
    <NumberField {...wiring} max={100} value={value} onValueChange={setValue} />
  )}
</InlineEdit>
```

| Value | Salmon field | Bind |
| --- | --- | --- |
| Number | `NumberField` | the `inputProps` wiring without `value` / `onChange`, plus `value` and `setValue` |
| Masked text | `MaskedField` | same; the draft is the raw value |
| Date, DateTime | `DatePicker` (`granularity="minute"` for DateTime) | `aria-label`, `value`, `onValueChange` |
| Time | `TimePicker` | `aria-label`, `value`, `onValueChange` |
| Date range | `DateRangePicker` | `aria-label`, `value`, `onValueChange` |
| One or several options from a list | `Select`, `Select multiple` | `aria-label`, `data`, `value`, `onValueChange` |
| Typed text with suggestions | `Combobox` | `aria-label`, `data`, `value`, `onValueChange` |
| Color | `ColorPicker` | `aria-label`, `value`, `onValueChange` |
| Slider | `Slider` | `label` (it names the thumb), `value`, `onValueChange` |

Give only text-like fields the `inputProps` key handler: a picker or dropdown
keeps Enter for its own list or calendar, and the edit is saved with the Save
action or on blur. Focus or a press inside a field's popup does not end the
edit, and Escape closes the open popup first, then cancels the edit.
`ColorPicker` and `DateRangePicker` keep their Apply step by default, so a
pick reaches the draft on Apply. `NumberField` clamps on blur but not on
Enter: pair `max` / `min` with `validation.range` so Enter cannot save an
out-of-range draft.

The rich text editor is `RichTextEditor`
from `@salmonui/rich-text`, bound to the draft, with `RichTextContent` rendering
the stored HTML (sanitized) as the preview. Turn off `saveOnEnter` so Enter
makes new paragraphs, and use `blurAction="keep"` so the editor's own dialogs
and menus do not save:

```tsx
import { InlineEdit } from '@salmonui/editors/inline-edit';
import { RichTextContent, RichTextEditor } from '@salmonui/rich-text';

<InlineEdit<string>
  label="Description"
  value={html}
  save={setHtml}
  saveOnEnter={false}
  blurAction="keep"
  renderPreview={({ value }) => <RichTextContent value={value} />}
>
  {({ value, setValue }) => <RichTextEditor aria-label="Description" defaultValue={value} onChange={setValue} />}
</InlineEdit>
```

### Live demo: Inline rich text

Live demo: https://salmonui.dev/demos/editors/inline-edit-rich-text

Rich text editing in place as a composition - InlineEdit with RichTextEditor as its editor and RichTextContent rendering the saved HTML as the preview.

`Demo.tsx`:

```tsx
'use client';

import { useState } from 'react';
import { InlineEdit } from '@salmonui/editors/inline-edit';
import { RichTextContent, RichTextEditor } from '@salmonui/rich-text';
import '@salmonui/rich-text/styles.css';

const initial = '<p>The <strong>v1</strong> release ships every editor:</p><ul><li><p>InlineEdit</p></li><li><p>RichTextEditor</p></li></ul>';

export default function InlineEditRichTextDemo() {
  const [html, setHtml] = useState(initial);
  return (
    <div style={{ display: 'grid', gap: 12, maxWidth: 640 }}>
      <strong id="inline-rte-label">Description</strong>
      <InlineEdit<string>
        label="Description"
        mode="inline"
        value={html}
        save={setHtml}
        saveOnEnter={false}
        blurAction="keep"
        renderPreview={({ value }) => <RichTextContent value={value} />}
      >
        {({ value, setValue }) => (
          <RichTextEditor
            aria-labelledby="inline-rte-label"
            defaultValue={value}
            onChange={setValue}
            toolbar={['bold', 'italic', 'underline', '|', 'unorderedList', 'orderedList', '|', 'link', 'undo', 'redo']}
            style={{ minBlockSize: 160 }}
          />
        )}
      </InlineEdit>
      <details>
        <summary>Saved HTML</summary>
        <pre style={{ whiteSpace: 'pre-wrap', fontSize: 12 }}>{html}</pre>
      </details>
    </div>
  );
}
```

## Saving, cancelling and blur

- Enter saves (`saveOnEnter`, default true); in a `<textarea>` Enter adds a
  line and Ctrl+Enter / Cmd+Enter saves. Escape cancels.
- `showActions` (default true) shows Save and Cancel buttons labelled from
  `localeText`; `renderActions({ save, cancel, pending })` replaces them.
- `blurAction` decides what focus leaving the editor does: `'save'`
  (default), `'cancel'` or `'keep'`. Tab out of the editor follows
  it too.
- `save(value, { signal })` persists the value and may return a promise.
While it is pending the editor is `aria-busy`, the input
  is read-only, the Save button shows a spinner and a polite status says
  "Saving". Cancelling aborts `signal`, as does unmounting. A rejection keeps
  the editor open with `localeText.saveFailed`, announced politely, and calls
  `onSaveError({ value, error })`. Without `save` the value is saved
  locally (uncontrolled) or reported through `onSave` (controlled).
- `name` renders a hidden input with the saved value, so an enclosing
  `<form>` submits it.

## Validation

`validation` takes the Data Grid `column.validation` vocabulary and is
evaluated by `@salmonui/core/validation`: `required`,
`min`, `max`, `range`, `minLength`, `maxLength`, `rangeLength`, `number`,
`digits`, `email`, `url`, `date`, `dateIso`, `creditcard`, `tel`, `pattern`,
and `validate(value)` (sync or async, returning a message, or null / true
when valid). Each rule value is the argument or `[argument, message]`;
`messages` overrides the defaults. A failing rule blocks the save, shows the
message under the editor and sets `aria-invalid` and `aria-describedby` on
the input.

## Events and ref

Lifecycle events come in before/after pairs; each before event is
cancellable with `event.preventDefault()`:

| Event | When | Payload |
| --- | --- | --- |
| `onBeforeEditStart` / `onEditStart` | editing starts | `{ source }` |
| `onBeforeSave` / `onSave` | after validation / after the value is saved | `{ value, previousValue, source, trigger }` |
| `onBeforeEditCancel` / `onEditCancel` | the edit is discarded | `{ value, source, trigger }` |
| `onDraftChange` | each draft change | `{ value, previousValue }` |
| `onSaveError` | `save` rejected | `{ value, error }` |

`source` is `'pointer' | 'keyboard' | 'touch' | 'api'`; `trigger` is
`'actions' | 'enter' | 'escape' | 'blur' | 'api'`.

`ref` receives an `InlineEditApi` with the Data Grid method names: `startEdit()`, `save()` (resolves to whether the value was saved),
`cancelEdit()`, `validate()` (resolves to the error list) and `element` (the
root `<span>`).

### Live demo: Inline edit events and ref

Live demo: https://salmonui.dev/demos/editors/inline-edit-api

A controlled InlineEdit driven from outside - editing / onEditingChange, the ref methods startEdit, validate, save and cancelEdit, a cancellable onBeforeEditStart and every before/after event with its source and trigger.

`Demo.tsx`:

```tsx
'use client';

import { useRef, useState } from 'react';
import { Button } from '@salmonui/buttons/button';
import { Checkbox } from '@salmonui/inputs/checkbox';
import { InlineEdit, type InlineEditApi } from '@salmonui/editors/inline-edit';
import { EventLog, useEventLog } from '../../components/demo-kit';

export default function InlineEditApiDemo() {
  const [log, push] = useEventLog();
  const [owner, setOwner] = useState('Ana Lima');
  const [editing, setEditing] = useState(false);
  const [lockStart, setLockStart] = useState(false);
  const [errors, setErrors] = useState<readonly string[] | null>(null);
  const api = useRef<InlineEditApi | null>(null);
  return (
    <div style={{ display: 'grid', gap: 12, maxWidth: 620 }}>
      <div>
        Owner:{' '}
        <InlineEdit
          ref={api}
          label="Owner"
          value={owner}
          editing={editing}
          mode="inline"
          blurAction="keep"
          onEditingChange={(next, event) => {
            setEditing(next);
            push(`onEditingChange: ${next} (${event.reason}, ${event.source})`);
          }}
          validation={{ required: [true, 'Enter an owner'], minLength: [3, 'At least 3 characters'] }}
          save={(value) => setOwner(value)}
          onBeforeEditStart={(event) => {
            if (lockStart) {
              event.preventDefault();
              push(`onBeforeEditStart: vetoed (${event.source})`);
            } else push(`onBeforeEditStart (${event.source})`);
          }}
          onEditStart={(event) => push(`onEditStart (${event.source})`)}
          onDraftChange={(event) => push(`onDraftChange: "${event.previousValue}" -> "${event.value}"`)}
          onBeforeSave={(event) => push(`onBeforeSave: "${event.value}" (${event.source}, ${event.trigger})`)}
          onSave={(event) => push(`onSave: "${event.previousValue}" -> "${event.value}" (${event.source}, ${event.trigger})`)}
          onBeforeEditCancel={(event) => push(`onBeforeEditCancel (${event.source}, ${event.trigger})`)}
          onEditCancel={(event) => push(`onEditCancel: draft "${event.value}" dropped (${event.source}, ${event.trigger})`)}
        />
      </div>
      <div role="group" aria-label="Ref methods" style={{ display: 'flex', flexWrap: 'wrap', gap: 8 }}>
        <Button variant="outline" type="button" onClick={() => api.current?.startEdit()}>
          startEdit()
        </Button>
        <Button variant="outline"
          type="button"
          onClick={async () => {
            const list = (await api.current?.validate()) ?? [];
            setErrors(list);
            push(`validate(): ${list.length === 0 ? 'no errors' : list.join('; ')}`);
          }}
        >
          validate()
        </Button>
        <Button variant="outline" type="button" onClick={async () => push(`save(): ${String(await api.current?.save())}`)}>
          save()
        </Button>
        <Button variant="outline" type="button" onClick={() => api.current?.cancelEdit()}>
          cancelEdit()
        </Button>
        <Button variant="outline" type="button" aria-pressed={editing} onClick={() => setEditing((value) => !value)}>
          editing = {String(!editing)}
        </Button>
        <Checkbox checked={lockStart} onCheckedChange={(checked) => setLockStart(checked)}>
          Veto onBeforeEditStart
        </Checkbox>
      </div>
      <p style={{ margin: 0 }} aria-live="polite">
        Saved value: <strong>{owner}</strong>
        {errors ? ` · last validate(): ${errors.length === 0 ? 'valid' : errors.join('; ')}` : ''}
      </p>
      <EventLog entries={log} />
    </div>
  );
}
```

## Keyboard and accessibility

| Key | Action |
| --- | --- |
| Enter, Space on the preview or edit button | Start editing; focus moves into the editor |
| Enter | Save (Ctrl+Enter / Cmd+Enter in a textarea) |
| Escape | Cancel; focus returns to the preview |
| Tab | Moves focus; leaving the editor applies `blurAction` |

Focus returns to the preview after a save or cancel from the keyboard or the
buttons. The popover is a dialog labelled with `label`. Axe and ARIA
snapshots run in light, dark and RTL.

## Styling, RTL and localization

`className` and `style` go to the root `<span class="salmon-inline-edit">`;
`classNames` / `styles` reach the `preview`, `editor`, `actions` and `error`
parts. Direction is inherited: the actions sit at the
inline end and the popover carries the direction it was opened in.
`localeText` overrides `save`, `cancel`, `edit`, `empty`, `saving` and
`saveFailed` (`INLINE_EDIT_DEFAULT_LOCALE_TEXT` holds the English
defaults).

The root is a `<span>`, so an `InlineEdit` may sit inside a paragraph. The
server renders the preview and hidden input only; the editor mounts on the
client when editing starts (or in server HTML with `mode="inline"` and
`defaultEditing`), with no hydration difference. An idle page of
1,000 `InlineEdit`s mounts no editor and no popover.

### Live demo: Inline edit options

Live demo: https://salmonui.dev/demos/editors/inline-edit-options

InlineEdit options side by side - a placeholder for an empty value, a disabled state, a select editor with a label lookup in the preview, saveOnEnter off with custom actions, popover placement, right to left with localized action labels, and part styling.

`Demo.tsx`:

```tsx
'use client';

import { useState } from 'react';
import { Button } from '@salmonui/buttons/button';
import { Checkbox } from '@salmonui/inputs/checkbox';
import { InlineEdit } from '@salmonui/editors/inline-edit';
import { Select } from '@salmonui/dropdowns/select';

const STATUSES = [
  { value: 'todo', label: 'To do' },
  { value: 'doing', label: 'In progress' },
  { value: 'done', label: 'Done' },
] as const;
type Status = (typeof STATUSES)[number]['value'];

const row = { display: 'flex', gap: 8, alignItems: 'baseline', flexWrap: 'wrap' } as const;

export default function InlineEditOptionsDemo() {
  const [disabled, setDisabled] = useState(false);
  const [rtl, setRtl] = useState(false);
  return (
    <div style={{ display: 'grid', gap: 14, maxWidth: 620 }}>
      <div style={row}>
        <Checkbox checked={disabled} onCheckedChange={(checked) => setDisabled(checked)}>
          Disable the fields
        </Checkbox>
        <Checkbox checked={rtl} onCheckedChange={(checked) => setRtl(checked)}>
          Right to left (Arabic labels)
        </Checkbox>
      </div>
      <div style={row}>
        <strong>Subtitle</strong>
        {/* Empty value: the placeholder shows in the preview. */}
        <InlineEdit label="Subtitle" defaultValue="" placeholder="Add a subtitle" disabled={disabled} popoverProps={{ placement: 'right-start', offset: 12 }} />
      </div>
      <div style={row}>
        <strong>Status</strong>
        {/* A custom editor (Select) with a label lookup for the preview. */}
        <InlineEdit<Status>
          label="Status"
          mode="inline"
          defaultValue="doing"
          disabled={disabled}
          showActions={false}
          blurAction="save"
          renderPreview={({ value }) => STATUSES.find((s) => s.value === value)?.label ?? value}
        >
          {({ value, setValue, inputProps }) => (
            <Select
              id={inputProps.id}
              aria-label={inputProps['aria-label']}
              autoFocus
              data={STATUSES}
              value={value}
              onValueChange={(next) => next != null && setValue(next as Status)}
            />
          )}
        </InlineEdit>
      </div>
      <div style={row}>
        <strong>Summary</strong>
        {/* saveOnEnter={false}: Enter never saves a single-line input; custom actions replace Save and Cancel. */}
        <InlineEdit
          label="Summary"
          mode="inline"
          defaultValue="Draft the launch email"
          saveOnEnter={false}
          disabled={disabled}
          styles={{ editor: { minInlineSize: 260 } }}
          renderActions={({ save, cancel, pending }) => (
            <span>
              <Button type="button" onClick={save} disabled={pending}>
                Apply
              </Button>{' '}
              <Button variant="outline" type="button" onClick={cancel}>
                Discard
              </Button>
            </span>
          )}
        />
      </div>
      <div dir={rtl ? 'rtl' : 'ltr'} lang={rtl ? 'ar' : 'en'} style={row}>
        <strong>{rtl ? 'العنوان' : 'Heading'}</strong>
        {/* Inherited direction, localeText and part styling. */}
        <InlineEdit
          label={rtl ? 'العنوان' : 'Heading'}
          defaultValue=""
          mode="inline"
          disabled={disabled}
          className="demo-inline-edit"
          style={{ fontWeight: 600 }}
          styles={{ preview: { borderBlockEnd: '1px dashed currentColor' } }}
          localeText={rtl ? { save: 'حفظ', cancel: 'إلغاء', edit: 'تعديل', empty: 'فارغ', saving: 'جارٍ الحفظ', saveFailed: 'تعذّر الحفظ' } : { empty: 'No heading yet' }}
        />
      </div>
    </div>
  );
}
```

## Design notes

- There is no built-in submit URL: Salmon UI prescribes no backend
  protocol, so saving is the `save(value, { signal })` action, which can
  call `fetch`, a server action or a data source, for example
  `save={(value) => fetch(url, { method: 'POST', body: JSON.stringify({ id, [name]: value }) })}`.
  React to the outcome in `onSave` / `onSaveError`.
- There are no lifecycle events or teardown method: React owns
  mount and unmount, so lifecycle events would duplicate effects; use an
  effect or a ref callback, and unmount the component to remove it.
- There is no HTML parse or sanitize option: values never render as
  HTML strings, so there is no parse or sanitize switch to get wrong; build
  rich display in `renderPreview`, which returns a React node (for stored
  HTML, `RichTextContent` as in the rich text example above).
- There is no built-in persistence: components never write to `localStorage` on
  their own, because hidden persistence breaks server rendering
  consistency; pass a controlled `value` and persist it from `onSave`.

## API reference

[`@salmonui/editors/inline-edit`](https://salmonui.dev/docs/reference/editors/inline-edit) lists
every prop, event payload and type, including `InlineEditApi` and
`INLINE_EDIT_DEFAULT_LOCALE_TEXT`.
