sv-tree-dnd
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+andSafari on iOS 17.4+. - checkVisibility() needs
Safari 17.4+andSafari on iOS 17.4+.
Partially supported:
- Keyboard events (code): Chrome Android and Firefox for Android only ship it partially.
Android on-screen keyboards leave `code` empty. Only the shortcut on a non-Latin layout is lost there.
sv-tree-dnd lets users move the items of the sv-tree inside it by drag and drop or cut and paste.
Usage
Wrap an sv-tree in sv-tree-dnd, and load sv-tree too, since sv-tree-dnd only adds the moving:
<sv-tree-dnd> <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-item value="package.json">package.json</sv-tree-item> </sv-tree></sv-tree-dnd>On touch, a long press starts the move, and a copy of the item follows the finger.
Dropping
The row under the pointer drops before the item in its top quarter, inside it in the middle, and after it in the bottom quarter. In the bottom zone of an open item, the drop goes before its first child.
A row that cannot take the item inside splits in half. By default, an empty item takes drops inside only as a lazy item or a branch, like a folder.
Hovering over a closed item to drop inside it opens the item after a short delay.
Keyboard
| Key | Behavior |
|---|---|
Ctrl+X / Cmd+X | Cuts the focused item |
Ctrl+V / Cmd+V | Moves the cut item inside the focused item, else after it |
Ctrl+Shift+V / Cmd+Shift+V | Moves the cut item before the focused item |
Escape | Cancels the cut |
To translate the short English text a screen reader reads after each step, set getAnnouncement.
Handle
To start a drag only from a grip icon, put the icon in the item and point handle at it:
<sv-tree-dnd handle=".grip"> <sv-tree> <sv-tree-item> <svg class="grip" aria-hidden="true">...</svg> index.ts </sv-tree-item> </sv-tree></sv-tree-dnd>Rules
To keep package.json in place and let only branches take children, set shouldAllowDrop:
treeDnd.shouldAllowDrop = ({ item, targetItem, position }) => item?.value !== "package.json" && (position !== "inside" || targetItem.branch);Drops from outside
To take files from the desktop, set accept="Files", and add the new items yourself in itemdropped, where item is null:
<sv-tree-dnd accept="Files">...</sv-tree-dnd>treeDnd.addEventListener("itemdropped", ({ detail }) => { if (detail.item) return;
const fileItems = Array.from(detail.dataTransfer.files, file => { const fileItem = document.createElement("sv-tree-item"); fileItem.textContent = file.name; return fileItem; });
detail.targetItem[detail.position === "inside" ? "append" : detail.position](...fileItems);});Frameworks
Since sv-tree-dnd moves the item elements itself, cancel itemdropped when a framework renders the items, and update your data to let the framework render the new order:
treeDnd.addEventListener("itemdropped", event => { const { item, targetItem, position } = event.detail; if (!item) return;
event.preventDefault(); moveNode(item.value, targetItem.value, position);});Styling
Style the states on the items to change how a move looks:
sv-tree-item:state(--drop-inside)::part(row) { background-color: var(--sv-gray-6);}To change the drag image, like to give it a solid background, style --drag-preview:
sv-tree-item:state(--drag-preview)::part(row) { background-color: Canvas;}API References
Properties
shouldAllowDrop
Decides whether an item can go to a place while the user drags or pastes, and gets the same detail as itemdropped.
When unset, an item can go before or after any item, and inside an item that has children or is lazy or a branch.
Set or not, an item never goes into itself or into a disabled item, and a drag from outside needs a type in accept.
Since the browser exposes only dataTransfer.types during a drag, read the data and the files in itemdropped.
getAnnouncement
Returns the text a screen reader reads when the user cuts an item, moves it, or cancels the cut. Set it to translate the texts. drop is set only for move.
When unset, the texts are short English ones, like "Moved.". Since the moved item gets focus when the focus was in the tree, a screen reader also reads its name after a move.
accept
The drag types that the tree accepts from outside, like Files or text/uri-list. When empty, the tree refuses every drag from outside.
handle
A selector for the elements that start a drag, like a grip icon in the label. When empty, the whole row starts it.
Attributes
"accept"
A space-separated list, like Files text/uri-list.
"handle"
A selector for the elements that start a drag, like a grip icon in the label. When empty, the whole row starts it.
Events
itemdropped
Fired when the user drops an item or pastes a cut item, before the item moves.
Slots
Default
The sv-tree whose items move.
CSS Properties
--sv-tree-drop-clr
The color of the line or the box that shows where the item drops.
CSS States
:state(--moving)
On an sv-tree-item, while it is dragged or cut.
:state(--drop-before)
On an sv-tree-item, when the drop puts the item before it.
:state(--drop-inside)
On an sv-tree-item, when the drop puts the item inside it.
:state(--drop-after)
On an sv-tree-item, when the drop puts the item after it.
:state(--drag-preview)
On an sv-tree-item, while the browser takes its drag image, and on the copy that follows the finger on touch. Style it to change the image.