Text you click to change in place: a name, a title, a label.
$ pnpm dlx shadcn@latest add @dashral/editable-textInstallation
$ pnpm dlx shadcn@latest add @dashral/editable-textAdds the component, the components it builds on, its npm packages and the theme tokens it uses. Or by its full URL: https://dashral.com/r/editable-text.json
Usage
import { EditableText } from "@/components/dashral/editable-text"
<EditableText label="Project name" value={project.name} onValueChange={(name) => api.projects.rename(project.id, name)} required className="text-2xl font-semibold" />Using the npm package? Import from @dashral/ui/components/editable-text instead, here and in the examples; the rest is the same.
Examples
API reference
EditableText
| Prop | Type | Default |
|---|---|---|
label | stringWhat the text is, for screen readers: the button reads "Edit" and it. Required. | — |
value / defaultValue | stringThe text, controlled or initial. | "" |
onValueChange | (value: string) => void | PromiseCalled with the saved text (Enter or leaving), not on every keystroke. Return a promise: a spinner shows, and a rejection puts the text back with its message. | — |
placeholder | stringShown, muted, while it's empty. | — |
required | booleanAn empty edit is refused with a message. | false |
validate | (value: string) => string | undefinedReturn a message to refuse an edit. | — |
maxLength | numberAs on an input. | — |
disabled | booleanIt can't be edited. | false |
className | stringShared by the text and its field (size, weight), so switching moves nothing. | — |
classNames | { error }The error message's classes. | — |
Keyboard
| Keys | Action |
|---|---|
| EnterorSpace | Start editing (on the text) |
| Enter | Save |
| Escape | Put it back and stop editing |
| Tab | Save and move on |
Accessibility
- The text is a button named "Edit" and the label; editing, it's an input with the label as its name, the text selected.
- After Enter or Escape the focus goes back to the text; a refused edit stays open with its message, announced.
Motion
- The pencil fades in on hover and focus (always shown on touch screens); the field fades in over the text in the same box, so nothing shifts.