Trees

Trees

<nav class="tree max-w-xs" aria-label="Theme files">
<input class="input" type="search" placeholder="Search" aria-label="Search theme files" />
<ul>
<li>
<details>
<summary><i class="icon icon-folder" aria-hidden="true"></i><span>Assets</span></summary>
<ul>
<li><a href="#"><i class="icon icon-file" aria-hidden="true"></i><span>js/cart.js</span></a></li>
<li><a href="#"><i class="icon icon-file" aria-hidden="true"></i><span>img/hero.jpg</span></a></li>
</ul>
</details>
</li>
<li>
<details>
<summary><i class="icon icon-folder" aria-hidden="true"></i><span>Layouts</span></summary>
<ul>
<li><a href="#"><i class="icon icon-file" aria-hidden="true"></i><span>base.html</span></a></li>
</ul>
</details>
</li>
<li>
<details open>
<summary><i class="icon icon-folder" aria-hidden="true"></i><span>Templates</span></summary>
<ul>
<li><a href="#"><i class="icon icon-file" aria-hidden="true"></i><span>cart.html</span></a></li>
<li><a href="#" aria-current="page"><i class="icon icon-file" aria-hidden="true"></i><span>index.html</span></a></li>
<li><a href="#"><i class="icon icon-file" aria-hidden="true"></i><span>product.html</span></a></li>
</ul>
</details>
</li>
</ul>
<p class="tree-empty" hidden>No files found.</p>
</nav>

A folder holds folders, a link can sit outside every folder, and icons are optional

aria-current="page" on a link marks the open file, and open on its details shows it

A script filters the files with filter()

<div class="flex max-w-xs flex-col gap-3">
<nav class="tree" id="demo-tree-filter" aria-label="Partials">
<ul>
<li>
<details>
<summary><i class="icon icon-folder" aria-hidden="true"></i><span>Partials</span></summary>
<ul>
<li><a href="#"><i class="icon icon-file" aria-hidden="true"></i><span>footer.html</span></a></li>
<li><a href="#"><i class="icon icon-file" aria-hidden="true"></i><span>header.html</span></a></li>
<li><a href="#"><i class="icon icon-file" aria-hidden="true"></i><span>product_card.html</span></a></li>
</ul>
</details>
</li>
</ul>
<p class="tree-empty" hidden>No files found.</p>
</nav>
<div class="flex flex-wrap gap-2">
<button type="button" class="btn" data-variant="outline" data-size="sm" onclick="document.getElementById('demo-tree-filter').filter('er')">Show files with "er"</button>
<button type="button" class="btn" data-variant="outline" data-size="sm" onclick="document.getElementById('demo-tree-filter').filter('')">Show every file</button>
</div>
</div>

API Reference

Attributes

Attribute On Values Default Description
open a details present or absent absent Shows the folder's files. It is the browser's own attribute.
aria-current a link "page" none Marks the file that is open.
hidden a li, the .tree-empty present or absent set by the script Hides a file a search does not match, a folder with no match, and the empty line while a file is shown.

Methods and properties

Member Type Description
filter(term) method Shows only the files whose name holds the term, in any letter case, and opens their folders. An empty term shows every file and puts the folders back as they were.

Keyboard

Key Description
Tab Moves to the next folder or file. It is the browser's own.
Enter, Space On a folder, opens or closes it. It is the browser's own.
Enter On a file, follows its link. It is the browser's own.

An input[type=search] inside the tree filters it as it is typed in.

A tree with no search field needs no script: a folder is a details element.

It sends no events of its own. A details sends the browser's toggle event.

Once the script has started it, the element carries data-tree-initialized="true" and sends tsugi:initialized. The methods and properties above are on the element itself.