# Inline Edit

> Text you change where it sits, such as a title: press it, type, Enter saves and Escape puts it back.

Page: https://uicontext.com/docs/components/inline-edit

Group: Primitives.

## When to use

Text you change where it sits, such as a title: press it, type, Enter saves and Escape puts it back. Use [Input](https://uicontext.com/docs/components/input.md) instead when the field is part of a form with a Save button.

## Usage

```ts
import { InlineEdit } from "./ui/uicontext/components/InlineEdit.ts"

const Message = UiSchema.TaggedEnum({ Renamed: { value: Schema.String } })

export const Title = (h: Html.H<typeof Message.Type>, title: string) =>
	InlineEdit(h, { key: "title", props: { value: title, label: "Dashboard name", size: "title" }, onEmit: { Committed: ({ value }) => Message.Renamed({ value }) } })
```

## Examples

Each example is a complete file. Its imports point where the CLI installs the source.

### A project's name and summary

The example the page opens with: Inline Edit in “A project's name and summary”.

```ts title="src/demo.ts" source="www/src/examples/inline-edit/demo.ts"
import { UiSchema } from "effect/ui"
import { Html } from "effect/ui/html"
import * as stylex from "@stylexjs/stylex"
import { Schema } from "effect"
import { InlineEdit } from "./ui/uicontext/components/InlineEdit.ts"
import { sx } from "./ui/uicontext/components/Sx.ts"
import { color, font, space } from "./ui/uicontext/components/tokens.stylex.ts"

const styles = stylex.create({
	page: { display: "grid", gap: space.xs, inlineSize: "min(100%, 32rem)" },
	meta: { margin: 0, fontSize: font.sm, color: color.muted },
})

const Message = UiSchema.TaggedEnum({ Renamed: { value: Schema.String }, ChangedSummary: { value: Schema.String } })

// a project's header: its name and summary change where they sit
export const ProjectHeader = Html.component({
	name: "ProjectHeader",
	model: Schema.Struct({ name: Schema.String, summary: Schema.String, saves: Schema.Number }),
	message: Message,
}).make({
	init: () => ({ name: "Checkout redesign", summary: "", saves: 0 }),
	update: {
		Renamed: ($, { value }) => $.evolve({ name: value, saves: (n) => n + 1 }),
		ChangedSummary: ($, { value }) => $.evolve({ summary: value, saves: (n) => n + 1 }),
	},
	view: (h, model) =>
		h.div.attrs(sx(styles.page)).children(
			InlineEdit(h, { key: "name", props: { value: model.name, label: "Project name", size: "title", maxLength: 80 }, onEmit: { Committed: ({ value }) => Message.Renamed({ value }) } }),
			InlineEdit(h, {
				key: "summary",
				props: { value: model.summary, label: "Summary", placeholder: "Add a one-line summary", allowEmpty: true, maxLength: 140 },
				onEmit: { Committed: ({ value }) => Message.ChangedSummary({ value }) },
			}),
			h.p.attrs({ role: "status", dir: "auto", ...sx(styles.meta) }).children(model.saves === 0 ? "Press the name to rename it." : `Saved ${model.saves === 1 ? "once" : `${model.saves} times`}.`),
		),
})
```

### Rows in a list

Enter or leaving the field saves; Escape puts the old text back. Empty text isn't saved.

```ts title="src/rows.ts" source="www/src/examples/inline-edit/rows.ts"
import { UiSchema } from "effect/ui"
import { Html } from "effect/ui/html"
import * as stylex from "@stylexjs/stylex"
import { Schema } from "effect"
import { InlineEdit } from "./ui/uicontext/components/InlineEdit.ts"
import { sx } from "./ui/uicontext/components/Sx.ts"
import { color, font, space, stroke } from "./ui/uicontext/components/tokens.stylex.ts"

const styles = stylex.create({
	list: { display: "grid", margin: 0, padding: 0, listStyle: "none", inlineSize: "min(100%, 30rem)" },
	row: {
		display: "grid",
		gridTemplateColumns: "minmax(0, 1fr) auto",
		alignItems: "center",
		gap: space.md,
		paddingBlock: space.xs,
		paddingInline: space.sm,
		borderBlockEndWidth: stroke.hairline,
		borderBlockEndStyle: "solid",
		borderBlockEndColor: color.line,
	},
	rate: { fontSize: font.sm, color: color.muted, fontVariantNumeric: "tabular-nums" },
})

const Message = UiSchema.TaggedEnum({ Renamed: { id: Schema.String, value: Schema.String } })

// an invoice's line items: each description is edited in its row
export const LineItems = Html.component({
	name: "LineItems",
	model: Schema.Struct({ items: Schema.Array(Schema.Struct({ id: Schema.String, text: Schema.String, amount: Schema.String })) }),
	message: Message,
}).make({
	init: () => ({
		items: [
			{ id: "1", text: "Discovery workshop, 2 days", amount: "$3,200.00" },
			{ id: "2", text: "Checkout prototype", amount: "$5,750.00" },
			{ id: "3", text: "Usability sessions (6)", amount: "$1,800.00" },
		],
	}),
	update: { Renamed: ($, { id, value }) => $.evolve({ items: (items) => items.map((i) => (i.id === id ? { ...i, text: value } : i)) }) },
	view: (h, model) =>
		h.ul.attrs(sx(styles.list)).children(
			model.items.map((item) =>
				h.li.key(item.id).attrs(sx(styles.row)).children(
					InlineEdit(h, { key: item.id, props: { value: item.text, label: "Line item" }, onEmit: { Committed: ({ value }) => Message.Renamed({ id: item.id, value }) } }),
					h.span.attrs(sx(styles.rate)).children(item.amount),
				)),
		),
})
```

## API reference

**Props**

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `value` (required) | `string` | — | — |
| `label` (required) | `string` | — | What the text is, for the button and the input: "Project name" |
| `placeholder` | `string` | — | Shown, quieter, while the value is empty: "Untitled" |
| `allowEmpty` | `boolean` | — | Saves empty text too |
| `maxLength` | `number` | — | The most characters |
| `size` | `"body" \| "title"` | `body` | How big the text reads: a heading or body text; body by default |
| `disabled` | `boolean` | — | — |

**Events**

| Event | Payload | Description |
| --- | --- | --- |
| `Committed` | `{ value: string }` | The new text, trimmed, when it changed |

## Keyboard and accessibility

### Keys

| Keys | Does |
| --- | --- |
| Enter, Space | On the text, start editing with it selected |
| Enter | Save |
| Escape | Put the old text back |
| Tab | Save and move on |

On a Mac, Control is Command and Alt is Option. Left and Right trade places in right-to-left text.

## Installation

```sh
pnpm dlx shadcn@latest add checkfu/uicontext/inline-edit
```

With npm: `npx shadcn@latest add checkfu/uicontext/inline-edit`. With yarn: `yarn dlx shadcn@latest add checkfu/uicontext/inline-edit`. With bun: `bunx --bun shadcn@latest add checkfu/uicontext/inline-edit`.

It writes these files:

- `src/ui/uicontext/components/InlineEdit.ts`
- `src/ui/uicontext/components/Sx.ts`
- `src/ui/uicontext/components/tokens.stylex.ts`

And installs the npm packages `@stylexjs/stylex`, `effect`.

## Source

- [src/InlineEdit.ts](https://github.com/checkfu/uicontext/blob/main/src/InlineEdit.ts)

## Related

- [Button](https://uicontext.com/docs/components/button.md) (`button`): A button, and a link that looks like one, in every tone and three sizes.
- [Toggle](https://uicontext.com/docs/components/toggle.md) (`toggle`): A button that stays pressed, such as Bold or Pin.
- [Toggle Group](https://uicontext.com/docs/components/toggle-group.md) (`toggle-group`): Buttons that stay pressed and share one value, such as text styles.
- [Input](https://uicontext.com/docs/components/input.md) (`input`): Text inputs, selects and textareas in a labeled field with a hint and an error, and their styles for controls you lay out yourself.
- [Status](https://uicontext.com/docs/components/status.md) (`status`): Status badges and tags for runs and records.
- [Glyphs](https://uicontext.com/docs/components/glyphs.md) (`glyphs`): Small marks for status, priority, color and people.
