Editable Examples
The examples below assume that you have imported the component and set up the theme.
Basic
Let the user rename a title in place with a click. value sets the text, and aria-label names the group that a screen reader reads.
--editing--empty
<div class="editable-basic"> <sv-editable aria-label="Document title" placeholder="Add a title" value="Quarterly report" ></sv-editable></div>
<style> .editable-basic { display: grid; grid-template-columns: min(100%, 24em); place-content: center center; line-height: 1.4; }</style>Multiline
Edit a description that has more than one line. multiline edits in a text area, placeholder shows while it is empty, and buttons adds a submit and a cancel button.
Enter adds a line, and Ctrl + Enter or Cmd + Enter saves.
<div class="editable-multiline"> <sv-editable aria-label="Description" buttons multiline placeholder="Add a description" ></sv-editable></div>
<style> .editable-multiline { display: grid; grid-template-columns: min(100%, 24em); justify-content: center; line-height: 1.4; }</style>Edit Button
Start editing from a separate button, and keep the text plain. activation="none" turns the preview into plain text, the button calls edit(), and editingchange hides the button while editing.
Focus goes back to the button when editing ends.
<div class="editable-edit-button"> <div class="editable-edit-button-row"> <sv-editable activation="none" aria-label="Display name" placeholder="Add a name" value="Ada Lovelace" ></sv-editable>
<button type="button" class="btn"> <!-- Pen --> <svg aria-hidden="true" viewBox="0 0 24 24"> <path d="M21.174 6.812a1 1 0 0 0-3.986-3.987L3.842 16.174a2 2 0 0 0-.5.83l-1.321 4.352a.5.5 0 0 0 .623.622l4.353-1.32a2 2 0 0 0 .83-.497z" /> </svg> Edit </button> </div></div>
<script type="application/x-typescript" module> const editable = document.querySelector<Editable>(".editable-edit-button sv-editable")!; const editButton = document.querySelector<HTMLButtonElement>(".editable-edit-button button")!;
editButton.addEventListener("click", () => editable.edit());
editable.addEventListener("editingchange", () => { editButton.hidden = editable.editing; });</script>
<style> .editable-edit-button { display: grid; grid-template-columns: min(100%, 20em); gap: 0.4em; justify-content: center; line-height: 1.4;
.btn { display: flex; gap: 0.5em; align-items: center; }
svg { inline-size: 1em; block-size: 1em; fill: none; stroke: currentcolor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; } }
.editable-edit-button-row { display: flex; gap: 1em; align-items: center;
sv-editable { flex: 1; min-inline-size: 0; } }</style>