Skip to content

Tree Actions

Add and delete tree items from a toolbar, with the selection following focus. selectedItems finds the target, reveal() and focus() show the new item, and remove() deletes one.

A new item goes into the selected folder, or next to the selected file. Delete or the trash button removes the item, and the tree moves focus to a neighbor. Since each value is the path of its item, an action can use it to update your own data.

src index.ts tree.ts docs package.json

Code

The following example assumes that you have imported the component and set up the theme.

Tree Actions
<div class="tree-actions">
46 collapsed lines
<div class="tree-actions-header">
<label for="tree-actions-files">Project files</label>
<div class="tree-actions-toolbar">
<button type="button" class="new-file-button" aria-label="New file">
<!-- File plus -->
<svg aria-hidden="true" viewBox="0 0 24 24">
<path d="M15 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V7Z" />
<path d="M14 2v4a2 2 0 0 0 2 2h4" />
<path d="M9 15h6" />
<path d="M12 18v-6" />
</svg>
</button>
<button type="button" class="new-folder-button" aria-label="New folder">
<!-- Folder plus -->
<svg aria-hidden="true" viewBox="0 0 24 24">
<path d="M12 10v6" />
<path d="M9 13h6" />
<path
d="M20 20a2 2 0 0 0 2-2V8a2 2 0 0 0-2-2h-7.9a2 2 0 0 1-1.69-.9L9.6 3.9A2 2 0 0 0 7.93 3H4a2 2 0 0 0-2 2v13a2 2 0 0 0 2 2Z"
/>
</svg>
</button>
<button type="button" class="delete-button" aria-keyshortcuts="Delete" aria-label="Delete">
<!-- Trash -->
<svg aria-hidden="true" viewBox="0 0 24 24">
<path d="M3 6h18" />
<path d="M19 6v14c0 1-1 2-2 2H7c-1 0-2-1-2-2V6" />
<path d="M8 6V4c0-1 1-2 2-2h4c1 0 2 1 2 2v2" />
</svg>
</button>
</div>
</div>
<sv-tree id="tree-actions-files" selection="single">
<sv-tree-item branch expanded value="src">
src
<sv-tree-item selected value="src/index.ts">index.ts</sv-tree-item>
<sv-tree-item value="src/tree.ts">tree.ts</sv-tree-item>
</sv-tree-item>
<sv-tree-item branch value="docs">docs</sv-tree-item>
<sv-tree-item value="package.json">package.json</sv-tree-item>
</sv-tree>
</div>
<script type="application/x-typescript" module>
69 collapsed lines
const tree = document.querySelector<Tree>("#tree-actions-files")!;
const deleteButton = document.querySelector<HTMLButtonElement>(".tree-actions .delete-button")!;
function getFreeName(parent: Tree | TreeItem, stem: string, extension: string) {
const takenNames = parent.items.map(item => item.value.split("/").pop());
for (let index = 1; ; index++) {
const name = `${stem}-${index}${extension}`;
if (!takenNames.includes(name)) {
return name;
}
}
}
function addItem(isFolder: boolean) {
const [selectedItem] = tree.selectedItems;
const folder = selectedItem?.closest<TreeItem>("[branch]");
const parent = folder ?? tree;
const name = isFolder
? getFreeName(parent, "folder", "")
: getFreeName(parent, "untitled", ".txt");
const path = folder ? `${folder.value}/${name}` : name;
const item = document.createElement("sv-tree-item");
item.value = path;
item.textContent = name;
item.branch = isFolder;
parent.append(item);
item.reveal();
item.focus();
}
// The tree moves focus to a neighbor when the focused item is removed
function deleteItem(item: TreeItem) {
item.focus();
item.remove();
}
document
.querySelector(".tree-actions .new-file-button")!
.addEventListener("click", () => addItem(false));
document
.querySelector(".tree-actions .new-folder-button")!
.addEventListener("click", () => addItem(true));
deleteButton.addEventListener("click", () => {
const [selectedItem] = tree.selectedItems;
if (selectedItem) deleteItem(selectedItem);
});
tree.addEventListener("keydown", event => {
const target = event.target as HTMLElement;
if (event.key === "Delete" && target.matches("sv-tree-item")) {
deleteItem(target as TreeItem);
}
});
// The toolbar acts on the highlighted item because selection follows focus
tree.addEventListener("focusin", event => {
(event.target as TreeItem).selected = true;
});
tree.addEventListener("selectedchange", () => {
deleteButton.disabled = tree.selectedItems.length === 0;
});
</script>
<style>
80 collapsed lines
.tree-actions {
display: grid;
grid-template-columns: min(100%, 20em);
gap: 0.4em;
justify-content: center;
sv-tree {
padding: 0.5em;
border: 1px solid var(--sv-gray-5);
border-radius: var(--sv-bdr-rad-md);
&:state(--no-results)::after {
display: block;
padding-block: 0.25em;
color: var(--sv-gray-3);
text-align: center;
content: "No files";
}
}
sv-tree-item::part(label)::before {
flex: none;
inline-size: 1em;
block-size: 1em;
content: "";
background-color: currentcolor;
mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M15 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V7Z'/%3E%3Cpath d='M14 2v4a2 2 0 0 0 2 2h4'/%3E%3C/svg%3E")
center / contain no-repeat;
}
sv-tree-item[branch]::part(label)::before {
mask-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M20 20a2 2 0 0 0 2-2V8a2 2 0 0 0-2-2h-7.9a2 2 0 0 1-1.69-.9L9.6 3.9A2 2 0 0 0 7.93 3H4a2 2 0 0 0-2 2v13a2 2 0 0 0 2 2Z'/%3E%3C/svg%3E");
}
}
.tree-actions-header {
display: flex;
align-items: center;
justify-content: space-between;
label {
font-size: 0.9em;
font-weight: 600;
}
}
.tree-actions-toolbar {
display: flex;
gap: 0.25em;
button {
display: flex;
padding: 0.35em;
font: inherit;
color: var(--sv-gray-2);
cursor: pointer;
background: none;
border: none;
border-radius: var(--sv-bdr-rad-sm);
&:hover:not(:disabled) {
background: var(--sv-gray-6);
}
&:disabled {
cursor: default;
opacity: 0.4;
}
}
svg {
inline-size: 1.1em;
block-size: 1.1em;
fill: none;
stroke: currentcolor;
stroke-width: 2;
stroke-linecap: round;
stroke-linejoin: round;
}
}
</style>