Skip to content

sv-tree-rename

Baseline 2024 Widely available

Every feature it needs is Baseline widely available.

Works in Chrome 125+, Chrome Android 125+, Edge 125+, Firefox 126+, Firefox for Android 126+, Safari 17.4+, Safari on iOS 17.4+.

Why these versions:

  • :state() needs Chrome 125+, Chrome Android 125+, Edge 125+, Firefox 126+, Firefox for Android 126+, Safari 17.4+ and Safari on iOS 17.4+.

Partially supported:

  • Keyboard events (isComposing): Safari and Safari on iOS only ship it partially.
    Safari before 27 misses only the Enter that ends a composition, which then also ends the rename.

Optional features:

  • Mouse events: Chrome 55+, Chrome Android 55+, Edge 79+, Firefox 53+, Firefox for Android 53+, Safari 18.2+, Safari on iOS 18.2+.
    A middle click on the rename field of a link item can open the link.

Full browser support report

sv-tree-rename lets users rename the items of the sv-tree inside it with an input in place of the label.

Usage

Wrap an sv-tree in sv-tree-rename, and load sv-tree too, since sv-tree-rename only adds the renaming:

<sv-tree-rename>
<sv-tree>
<sv-tree-item value="src" expanded>
src
<sv-tree-item value="src/index.ts">index.ts</sv-tree-item>
</sv-tree-item>
</sv-tree>
</sv-tree-rename>

Keyboard

Key Behavior
F2 Renames the focused item, even when other items are selected
Enter Saves the new name
Escape Keeps the old name

Rename button

Since a click on a button moves the focus off the tree, a rename button passes the selected item to rename:

renameButton.addEventListener("click", () => {
const [selectedItem] = tree.selectedItems;
if (selectedItem) {
treeRename.rename(selectedItem);
}
});

Rejecting a name

To reject a name, like a file name with a slash, cancel itemrenamed:

treeRename.addEventListener("itemrenamed", event => {
if (event.detail.name.includes("/")) {
event.preventDefault();
}
});

Frameworks

Since sv-tree-rename sets the text of the label itself, cancel itemrenamed when a framework renders the items, and update your data to let the framework render the new name:

treeRename.addEventListener("itemrenamed", event => {
event.preventDefault();
renameNode(event.detail.item.value, event.detail.name);
});

Styling

Style the input, a child of the item, with your own CSS:

sv-tree-item:state(--renaming) > input:not([slot]) {
outline-color: var(--sv-gray-3);
}

API References

Methods

rename()

Starts renaming item. It does nothing when the item is disabled, outside this element, or has no text label.

The input takes the place of the data-sv-tree-label child of the item, or else of its last text, and an icon before the text stays.

Properties

cancelOnBlur

Whether a blur keeps the old name. When off, a blur saves the new name, like Enter.

renamingItem

The item that is renamed now, or null. Set an item to start renaming it, or null to stop and keep the old name.

Attributes

"input-label"

The accessible name of the input. A change applies at the next rename.

"cancel-on-blur"

Whether a blur keeps the old name. When off, a blur saves the new name, like Enter.

Events

itemrenamed

Fired when the user confirms a new name, before the label changes. It does not fire for an empty or unchanged name.

Slots

Default

The sv-tree whose items are renamed.

CSS States

:state(--renaming)

On an sv-tree-item, while it is renamed.