Text you change where it sits, such as a title: press it, type, Enter saves and Escape puts it back.
- 1.7 kB JS 2.7 kB CSS gzipped
- No serious axe issues
- Server-rendered, hydrates
- 44px touch targets
- 4 keys checked
- Right-to-left
Press the name to rename it.
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 sitexport 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`}.`), ),})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 instead when the field is part of a form with a Save button.
Usage
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
Rows in a list
Enter or leaving the field saves; Escape puts the old text back. Empty text isn't saved.
- $3,200.00
- $5,750.00
- $1,800.00
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 rowexport 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 |
|---|---|---|---|
valuerequired | string | — | — |
labelrequired | 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 | Does |
|---|---|
| EnterEnter SpaceSpace | On the text, start editing with it selected |
| EnterEnter | Save |
| EscEscape | Put the old text back |
| TabTab | Save and move on |
Installation
pnpm dlx shadcn@latest add checkfu/uicontext/inline-edit