Editable
A value read as text that becomes a field in place.
On this page
An editable is a value read as text that becomes a field in place: a title, a name, a cell. At rest it is plain text in the type of whatever holds it; under the pointer a faint well appears around it with a pencil at its end; pressed, the well becomes a focused field with the value selected. The preview and the field share one grid cell, one font, one padding and one corner, so the text does not move by a pixel when one replaces the other. The editable behavior switches the two and saves.
Editable
<div class="max-w(420) mx-auto"> <span class="block mb-xs text-xs fg-text-muted" id="doc-title-label">Title</span> <h2 class="heading heading-xl"> <span class="editable"> <button type="button" class="editable-preview" id="doc-title-preview" aria-labelledby="doc-title-label doc-title-preview"> Quarterly report<svg class="icon editable-pencil" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> <path d="M2.6556 12.4849L3.2844 10.1793A0.7 0.7 0 0 1 3.4647 9.8686L10.505 2.8283A0.7 0.7 0 0 1 11.495 2.8283L13.1717 4.505A0.7 0.7 0 0 1 13.1717 5.495L6.1314 12.5353A0.7 0.7 0 0 1 5.8207 12.7156L3.5151 13.3444A0.7 0.7 0 0 1 2.6556 12.4849Z" /><path d="M9.6667 3.6667L12.3333 6.3333" /> </svg> </button> <input class="editable-field" value="Quarterly report" aria-labelledby="doc-title-label" required maxlength="80" hidden> </span> </h2> <span class="block mt-xs text-sm fg-text-muted">Last edited by Ada, 2 minutes ago</span></div><script type="module"> import { attach } from '@master/ui/behaviors' attach()</script>import { useEffect, useRef } from 'react'import { attach } from '@master/ui/behaviors'import PencilIcon from '@master/icons-react/native/16/soft/pencil'export function Example() { const root = useRef<HTMLDivElement>(null) useEffect(() => attach(root.current!), []) return ( <div ref={root} className="max-w(420) mx-auto"> <span className="block mb-xs text-xs fg-text-muted" id="doc-title-label">Title</span> <h2 className="heading heading-xl"> <span className="editable"> <button type="button" className="editable-preview" id="doc-title-preview" aria-labelledby="doc-title-label doc-title-preview"> Quarterly report<PencilIcon className="icon editable-pencil" /> </button> <input className="editable-field" defaultValue="Quarterly report" aria-labelledby="doc-title-label" required maxLength={80} hidden /> </span> </h2> <span className="block mt-xs text-sm fg-text-muted">Last edited by Ada, 2 minutes ago</span> </div> )}<script> import { attach } from '@master/ui/behaviors' import PencilIcon from '@master/icons-svelte/native/16/soft/pencil'</script><div class="max-w(420) mx-auto" {@attach attach}> <span class="block mb-xs text-xs fg-text-muted" id="doc-title-label">Title</span> <h2 class="heading heading-xl"> <span class="editable"> <button type="button" class="editable-preview" id="doc-title-preview" aria-labelledby="doc-title-label doc-title-preview"> Quarterly report<PencilIcon class="icon editable-pencil" /> </button> <input class="editable-field" value="Quarterly report" aria-labelledby="doc-title-label" required maxlength="80" hidden> </span> </h2> <span class="block mt-xs text-sm fg-text-muted">Last edited by Ada, 2 minutes ago</span></div><script setup>import { onMounted, onUnmounted, ref } from 'vue'import { attach } from '@master/ui/behaviors'import PencilIcon from '@master/icons-vue/native/16/soft/pencil'const root = ref(null)let detachonMounted(() => { detach = attach(root.value) })onUnmounted(() => detach?.())</script><template> <div ref="root" class="max-w(420) mx-auto"> <span class="block mb-xs text-xs fg-text-muted" id="doc-title-label">Title</span> <h2 class="heading heading-xl"> <span class="editable"> <button type="button" class="editable-preview" id="doc-title-preview" aria-labelledby="doc-title-label doc-title-preview"> Quarterly report<PencilIcon class="icon editable-pencil" /> </button> <input class="editable-field" value="Quarterly report" aria-labelledby="doc-title-label" required maxlength="80" hidden> </span> </h2> <span class="block mt-xs text-sm fg-text-muted">Last edited by Ada, 2 minutes ago</span> </div></template>#Where the text stands
The value lines up with the column it stands in: the root reaches out by --editable-padding-x on both sides, so the well grows around the text instead of pushing it in. The padding is in em, so a heading's well is as generous for its size as a cell's is for its own.
- At rest the preview is plain text. It is a button, so it takes focus, and the pencil, an
editable-pencilicon after its words, shows with the focus ring. - Under the pointer the inset tone and a half-pixel contour appear around the text, with the pencil after the last word. On a screen without a hovering pointer the pencil is always there, a little quieter: it is the only sign that the text can be changed.
- Editing, the field is a well risen to the sheet with one ink contour and a halo, as a focused input is. Hidden, the preview still holds the cell open, so nothing below moves. Where the browser has
field-sizing: content, the field grows past the preview as the value does; elsewhere its text scrolls inside the preview's width. - Pending. While the value is on its way, set
aria-busy="true"on the root: the preview reads muted and takes no press.
#Several lines
A <textarea> as the field holds a value of several lines. Enter starts a line there, so Control or Command with Enter saves. While the value is empty the preview says the field's placeholder, in editable-placeholder.
Several lines
<div class="max-w(420) mx-auto text-sm fg-text-body"> <span class="block mb-xs text-xs fg-text-muted" id="doc-summary-label">Summary</span> <div class="editable w-full"> <button type="button" class="editable-preview" id="doc-summary-preview" aria-labelledby="doc-summary-label doc-summary-preview"> <span class="editable-placeholder">Add a summary for the team</span><svg class="icon editable-pencil" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> <path d="M2.6556 12.4849L3.2844 10.1793A0.7 0.7 0 0 1 3.4647 9.8686L10.505 2.8283A0.7 0.7 0 0 1 11.495 2.8283L13.1717 4.505A0.7 0.7 0 0 1 13.1717 5.495L6.1314 12.5353A0.7 0.7 0 0 1 5.8207 12.7156L3.5151 13.3444A0.7 0.7 0 0 1 2.6556 12.4849Z" /><path d="M9.6667 3.6667L12.3333 6.3333" /> </svg> </button> <textarea class="editable-field" aria-labelledby="doc-summary-label" placeholder="Add a summary for the team" rows="3" hidden></textarea> </div></div><script type="module"> import { attach } from '@master/ui/behaviors' attach()</script>import { useEffect, useRef } from 'react'import { attach } from '@master/ui/behaviors'import PencilIcon from '@master/icons-react/native/16/soft/pencil'export function Example() { const root = useRef<HTMLDivElement>(null) useEffect(() => attach(root.current!), []) return ( <div ref={root} className="max-w(420) mx-auto text-sm fg-text-body"> <span className="block mb-xs text-xs fg-text-muted" id="doc-summary-label">Summary</span> <div className="editable w-full"> <button type="button" className="editable-preview" id="doc-summary-preview" aria-labelledby="doc-summary-label doc-summary-preview"> <span className="editable-placeholder">Add a summary for the team</span><PencilIcon className="icon editable-pencil" /> </button> <textarea className="editable-field" aria-labelledby="doc-summary-label" placeholder="Add a summary for the team" rows={3} hidden>{``}</textarea> </div> </div> )}<script> import { attach } from '@master/ui/behaviors' import PencilIcon from '@master/icons-svelte/native/16/soft/pencil'</script><div class="max-w(420) mx-auto text-sm fg-text-body" {@attach attach}> <span class="block mb-xs text-xs fg-text-muted" id="doc-summary-label">Summary</span> <div class="editable w-full"> <button type="button" class="editable-preview" id="doc-summary-preview" aria-labelledby="doc-summary-label doc-summary-preview"> <span class="editable-placeholder">Add a summary for the team</span><PencilIcon class="icon editable-pencil" /> </button> <textarea class="editable-field" aria-labelledby="doc-summary-label" placeholder="Add a summary for the team" rows="3" hidden></textarea> </div></div><script setup>import { onMounted, onUnmounted, ref } from 'vue'import { attach } from '@master/ui/behaviors'import PencilIcon from '@master/icons-vue/native/16/soft/pencil'const root = ref(null)let detachonMounted(() => { detach = attach(root.value) })onUnmounted(() => detach?.())</script><template> <div ref="root" class="max-w(420) mx-auto text-sm fg-text-body"> <span class="block mb-xs text-xs fg-text-muted" id="doc-summary-label">Summary</span> <div class="editable w-full"> <button type="button" class="editable-preview" id="doc-summary-preview" aria-labelledby="doc-summary-label doc-summary-preview"> <span class="editable-placeholder">Add a summary for the team</span><PencilIcon class="icon editable-pencil" /> </button> <textarea class="editable-field" aria-labelledby="doc-summary-label" placeholder="Add a summary for the team" rows="3" hidden></textarea> </div> </div></template>#The behavior
editable takes the root, the element with the editable class, and returns the function that detaches it.
import { editable } from '@master/ui/behaviors/editable.js'const title = editable(document.querySelector('.editable'))title.edit() // open the fieldtitle.value // the saved value- Opening. Pressing the preview hides it, shows the field with the saved value and selects its text.
- Saving. Enter saves, and so does leaving the field. A value the field's own constraints refuse (
required,maxlength,pattern) is not saved: Enter keeps the field open and lets the browser say why, and leaving puts the saved value back. Passblur: 'cancel'to make leaving cancel. - Cancelling. Escape puts the saved value back.
- Focus returns to the preview after Enter and Escape, never when the reader moved it elsewhere.
- Events. Saving a different value writes it into the preview and dispatches
inputandchangefrom the field. The function it returns has avalue, the saved value, andedit(); setting the value writes the preview and the field and dispatches nothing.
Where the value goes after that is the page's. Set aria-busy="true" on the root while it is on its way. If the server refuses it, set the value back and say why next to it, or call edit() and mark the field aria-invalid="true", which draws the danger edge: what was typed is kept for the reader to correct.
Without the behavior the preview is a button that does nothing, and the value stays readable.
#Choosing it
An editable suits a value read far more often than it is changed, and changed one at a time where it stands. When several values are changed together and saved at once, a form of inputs with a Save button says more plainly what is saved and when.
#Keyboard
| Keys | Action |
|---|---|
| Enter, Space | On the preview, open the field with the value selected |
| Enter | In the field, save and return to the preview; in a textarea, Control or Command with Enter |
| Escape | Put the saved value back and return to the preview |
| Tab | Leave the field, which saves the value, or puts it back when the field's constraints refuse it |
While an input method composes, Enter and Escape belong to it.
#Accessibility
- Name the value and its field with what it is. In the examples a visible label names the field, and the preview is named by the label and itself through
aria-labelledby, so it is read as "Title, Quarterly report, button". - Keep the saved value readable without the script: it is the preview's text.
- Say a save that fails next to the value, in words, not with the danger edge alone.
#React, Svelte and Vue
The editable component draws the first value into the first render and attaches the behavior, which writes the preview after that. The value that comes out is the saved one, never what is being typed. Copy it into the project with npx master-ui add editable; React, Svelte and Vue says how the components are built and used.
#Classes
| Class | Description |
|---|---|
editable | The value and its field, in one cell that the preview holds open; it reaches out by its padding so the text lines up with the column |
editable-preview | The saved value, as a button: plain text at rest, a faint well with a pencil under the pointer. Hidden, it still holds the cell open |
editable-pencil | The pencil after the words of the preview, an icon that shows under the pointer and with focus, and always where no pointer hovers |
editable-field | The field that takes the preview's place, hidden at rest: a focused well in the same font, padding and corner |
editable-placeholder | Inside the preview, what it says while the value is empty: the field's placeholder, in the muted ink |
#Custom properties
| Property | Default | Description |
|---|---|---|
--editable-padding-x | .375em | Inline padding of the preview and the field, which the root bleeds into |
--editable-padding-y | .125em | Block padding of the preview and the field |
--editable-radius | var(--radius-control-sm) | The corner of the well |
#Related
- InputText fields cut into the surface, with labels, hints, errors and groups.
- HeadingA title in the ink of the page, with the line over it and the paragraph under it.
- TableRows of data on hairlines, with sortable headings and aligned figures.