Inline edit
A value that edits in place as text, textarea or select, with save feedback and errors.
For a value in a record's attribute list or a table cell that should be edited where it stands. In a form that is submitted as a whole, use @khaeli/input.
Text
Project
"use client"
import { useState } from "react"
import { InlineEdit } from "@/components/ui/inline-edit"
export default function InlineEditText() {
const [name, setName] = useState("Atlas onboarding")
return (
<div className="flex w-full max-w-xs items-center gap-2">
<span className="w-20 shrink-0 text-base text-muted-foreground">Project</span>
<InlineEdit ariaLabel="Project name" value={name} onSave={setName} />
</div>
)
}
Long text
Summary
"use client"
import { useState } from "react"
import { InlineEdit } from "@/components/ui/inline-edit"
export default function InlineEditLongText() {
const [summary, setSummary] = useState("Move every open invoice to the new ledger before the March close.")
return (
<div className="flex w-full max-w-sm items-center gap-2">
<span className="w-20 shrink-0 text-base text-muted-foreground">Summary</span>
<InlineEdit kind="textarea" ariaLabel="Summary" value={summary} onSave={setSummary} />
</div>
)
}
Select
Status
"use client"
import { useState } from "react"
import { InlineEdit } from "@/components/ui/inline-edit"
const statuses = [
{ value: "backlog", label: "Backlog" },
{ value: "in-progress", label: "In progress" },
{ value: "in-review", label: "In review" },
{ value: "done", label: "Done" },
]
export default function InlineEditSelect() {
const [status, setStatus] = useState("in-progress")
return (
<div className="flex w-full max-w-xs items-center gap-2">
<span className="w-20 shrink-0 text-base text-muted-foreground">Status</span>
<InlineEdit ariaLabel="Status" value={status} options={statuses} onSave={setStatus} />
</div>
)
}
Custom display
Budget
"use client"
import { useState } from "react"
import { InlineEdit } from "@/components/ui/inline-edit"
const dollars = new Intl.NumberFormat("en-US", { style: "currency", currency: "USD", maximumFractionDigits: 0 })
export default function InlineEditCustomDisplay() {
const [budget, setBudget] = useState("12500")
return (
<div className="flex w-full max-w-xs items-center gap-2">
<span className="w-20 shrink-0 text-base text-muted-foreground">Budget</span>
<InlineEdit
kind="number"
ariaLabel="Budget in dollars"
value={budget}
onSave={setBudget}
renderValue={(value) => <span className="tabular-nums">{dollars.format(Number(value))}</span>}
/>
</div>
)
}
Empty
Order
"use client"
import { useState } from "react"
import { InlineEdit } from "@/components/ui/inline-edit"
export default function InlineEditEmpty() {
const [order, setOrder] = useState<string | null>(null)
return (
<div className="flex w-full max-w-xs items-center gap-2">
<span className="w-20 shrink-0 text-base text-muted-foreground">Order</span>
<InlineEdit ariaLabel="Purchase order" value={order} placeholder="Add a purchase order" onSave={setOrder} />
</div>
)
}
Slow save
Release
"use client"
import { useState } from "react"
import { InlineEdit } from "@/components/ui/inline-edit"
export default function InlineEditSlowSave() {
const [name, setName] = useState("Spring release")
async function save(next: string) {
await new Promise((resolve) => setTimeout(resolve, 1200))
setName(next)
}
return (
<div className="flex w-full max-w-xs items-center gap-2">
<span className="w-20 shrink-0 text-base text-muted-foreground">Release</span>
<InlineEdit ariaLabel="Release name" value={name} onSave={save} />
</div>
)
}
Rejected save
Key
"use client"
import { useState } from "react"
import { InlineEdit } from "@/components/ui/inline-edit"
export default function InlineEditRejectedSave() {
const [key, setKey] = useState("ATL")
function save(next: string) {
if (!/^[A-Z]{2,5}$/.test(next)) throw new Error("Use two to five capital letters.")
setKey(next)
}
return (
<div className="flex w-full max-w-xs items-start gap-2">
<span className="flex h-7.5 w-20 shrink-0 items-center text-base text-muted-foreground">Key</span>
<InlineEdit ariaLabel="Project key" value={key} onSave={save} />
</div>
)
}
Disabled
Invoice
INV-2041
"use client"
import { InlineEdit } from "@/components/ui/inline-edit"
export default function InlineEditDisabled() {
return (
<div className="flex w-full max-w-xs items-center gap-2">
<span className="w-20 shrink-0 text-base text-muted-foreground">Invoice</span>
<InlineEdit ariaLabel="Invoice number" value="INV-2041" onSave={() => {}} disabled />
</div>
)
}
Installation
npx shadcn@latest add @khaeli/inline-editbunx --bun shadcn@latest add @khaeli/inline-editInstall @khaeli/foundation first: this item's styling lives in its tokens and [data-slot] rules.
Installs with
- Registry items
- npm packages
- @keyline-icons/react ^2.2.0
Keyboard
EnterSpace- On the value, open the editor with the text selected. A list of
optionsopens as the browser's menu. Enter- In the editor, saves and closes it. Focus returns to the value.
Shift + Enter- In a
textarea, adds a line. Escape- Closes the editor and keeps the old value. Focus returns to the value.
Tab- Saves a changed value and moves focus on.
Accessible name
ariaLabelis required. It names the editor ("Project name"), and the value's button is described as "Edit Project name".- The button's name is the text it shows, so it reads as the value and not as a command. An empty field shows its
placeholderand is named by it; with no placeholder it is named byariaLabel. - After a save a polite live region says "Project name saved". A check is drawn beside the value for a moment; it is decoration.
- When
onSavethrows or rejects, the editor stays open witharia-invalid, and the error's message is announced and tied to the control as its description. - A
disabledfield is text, not a button: it leaves the tab order.
Registry JSON
The item as the shadcn CLI reads it, with every file it ships: https://ui.khaeli.com/r/inline-edit.json