Skip to content

sv-tree-item

sv-tree-item is one node of an sv-tree, with a label and the child items nested inside it.

Usage

An item lives inside an sv-tree, which handles the selection, the keyboard, and the form value. Nest child items directly inside an item to build a branch:

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

Label content

To keep icons, counts, and buttons out of typeahead and query, put icons in before-label and counts or buttons in after-label:

<sv-tree-item value="inbox">
<svg slot="before-label" aria-hidden="true">...</svg>
Inbox
<span slot="after-label">12</span>
</sv-tree-item>

When the label holds more than the name, like a date, wrap the name in a direct child with data-sv-tree-label, and typeahead and query read only that element.

Controls in an item

Keys pressed in a focused element inside an item stay with that element, and so does a click on a focusable element in the label, like an input. A click on a button in before-label or after-label, though, also selects and toggles the item, unless the click listener of the button calls event.preventDefault().

Wrap a tree of links in a <nav>. To open the path to the current page, reveal its item:

tree.querySelector("sv-tree-item[current]")?.reveal(true);

For a client router, cancel only the link in itemclicked, and leave clicks with ctrlKey, metaKey, or shiftKey to the browser for a new tab or window:

tree.addEventListener("itemclicked", ({ detail: { item, mouseEvent } }) => {
if (!item.href || mouseEvent.ctrlKey || mouseEvent.metaKey || mouseEvent.shiftKey) return;
mouseEvent.preventDefault();
router.navigate(item.href);
});

Custom checkbox

To replace the checkbox, slot an element into checkbox and style it from the item states:

sv-tree-item:state(--checked) > [slot="checkbox"] {
background: green;
}

Structure

divrow
slotmarker
spanmarker - Default marker
slotcheckbox - Only in the multiple modes
svgcheckbox - Default checkbox
slotbefore-label
alabel - A link when the item has an href
slotdefault
slotafter-label
divchildren
slotchildren

API References

Methods

expand()

Shows the children, skipping the animation when isInstant is true.

collapse()

Hides the children, skipping the animation when isInstant is true.

toggle()

Expands or collapses the item, skipping the animation when isInstant is true.

reveal()

Expands every ancestor of the item, skipping the animation when isInstant is true.

Properties

items

The child items of this item.

value

The value of the item, submitted with the form when the item is selected. Keep it unique within the tree.

selected

Whether the item is selected or checked. Setting it updates the tree, like option.selected.

expanded

Whether the item shows its children. Setting it animates the change, unlike expand(true) and collapse(true).

lazy

Whether the item can expand before its children exist. Expanding it fires lazyload.

branch

Whether the item is a branch, like a folder, that can expand and is never a leaf, even with no children.

disabled

Whether the item and all the items below it are disabled. A disabled branch can still be focused and expanded.

href

Turns the label into a link to this address.

target

Where the link of the label opens, the same as target on an <a> element.

current

Marks the item as the current page with aria-current="page". Style it with [current].

Attributes

"hidden"

The native hidden attribute. The tree skips a hidden item and its children.

"selected"

Sets the starting selection. Form reset returns to it.

"expanded"

Sets the starting state, with no animation.

"lazy"

Whether the item can expand before its children exist. Expanding it fires lazyload.

"branch"

Whether the item is a branch, like a folder, that can expand and is never a leaf, even with no children.

"disabled"

Whether the item and all the items below it are disabled. A disabled branch can still be focused and expanded.

"current"

Marks the item as the current page with aria-current="page". Style it with [current].

"href"

Turns the label into a link to this address.

"target"

Where the link of the label opens, the same as target on an <a> element.

"value"

The value of the item, submitted with the form when the item is selected. Keep it unique within the tree.

Events

expandedchange

Fired after expanded changes, whether by the user or from code.

transitionfinish

Fired when the expand or collapse animation ends, or right away when there is no animation.

lazyload

Fired when a lazy item expands. Add the children, then remove lazy, or collapse the item when loading fails. An item that is still lazy fires again on its next expansion.

Slots

marker

Replaces the marker, which shows only when the item can expand and turns when it expands, unless it has rotate: none.

checkbox

Replaces the checkbox, which shows only when the item is checkable in a tree with selection="multiple" or "leaf-multiple".

before-label

Content before the label, outside its link.

Default

The label of the item.

after-label

Content after the label, outside its link, like a count or a button.

children

The child items. A nested item puts itself here.

CSS Properties

--sv-tree-item-height

The smallest height of a row.

--sv-tree-item-bdr-rad

The border radius of a row.

--sv-tree-selected-bg

The background of a selected row.

--sv-tree-selected-clr

The text color of a selected row.

--sv-tree-guide-bdr-rad

The radius of the corner where a branch guide turns to the last child.

--sv-tree-checkbox-bdr-clr

The border color of an unchecked checkbox.

--sv-tree-active-checkbox-clr

The color of the check mark and the dash.

--sv-tree-indent

The indent of each level, which is one and a half marker sizes by default when the tree has guides.

--sv-tree-gap

The space between the pieces of a row, like the marker and the label.

--sv-tree-item-pad

The space at the start and end of a row.

--sv-tree-hover-bg

The background of a hovered row.

--sv-tree-marker-sz

The size of the marker.

--sv-tree-marker-clr

The color of the marker.

--sv-tree-guide-thickness

The thickness of the indent guides.

--sv-tree-guide-clr

The color of the indent guides.

--sv-tree-active-checkbox-bg

The background of a checked or indeterminate checkbox.

--sv-tree-dur

The duration of the expand and collapse animation.

--sv-tree-ease

The easing function of the expand and collapse animation.

CSS Parts

::part(row)

The row with the marker, the checkbox, and the label.

::part(marker)

The default marker. It turns when the item expands, and shows a spinner while the item loads.

::part(checkbox)

The default checkbox.

::part(label)

The label, which is a link when the item has an href.

::part(children)

The container of the child items.

CSS States

:state(--expanded)

When the item shows its children.

:state(--expandable)

When the item has children, or is lazy or a branch.

:state(--empty)

When the item is a branch with no children, and not lazy.

:state(--loading)

While a lazy item is expanded and waits for its children.

:state(--selected)

When the item is selected, in a tree with selection="single" or "leaf".

:state(--checked)

When the item is checked, in a tree with selection="multiple" or "leaf-multiple".

:state(--indeterminate)

When some, but not all, of the items below the item are checked.

:state(--checkable)

When the item has a checkbox, in a tree with selection="multiple" or "leaf-multiple".

:state(--filtered)

When the query of the tree hides the item.

:state(--query-expanded)

When the query of the tree opened the item, until the query clears.

:state(--last-visible)

When the item is the last of its siblings that is not hidden or filtered.