Skip to the page content

Tree & Sortable

Nested trees and lists you can reorder with mouse, touch or keys.

ARIA tree pattern

11 component types

List group tree

Bootstrap's own list group and collapse, nested. No extra script: each folder is a collapse button. Reach for it when a short menu only needs to open and close.

Tree
Show code
<div class="list-group">
  <button class="list-group-item list-group-item-action" type="button"
          data-bs-toggle="collapse" data-bs-target="#menu-devices"
          aria-expanded="true" aria-controls="menu-devices">Clothing</button>
  <div class="collapse show" id="menu-devices">
    <div class="list-group list-group-flush">
      <button type="button" class="list-group-item list-group-item-action active"
              aria-current="true">Sculpted Shoulder Blazer</button>
      <button type="button" class="list-group-item list-group-item-action">Leather Trench Coat</button>
    </div>
  </div>
</div>

Tri-state checkbox tree

Checking a group checks everything in it, and a group shows a dash when only part of it is checked. Each group counts its own checks.

Tree

6 of 11

permissions for the Scheduler role

  • Visits
    • See visits
    • Create visits
    • Delete visits
  • Schedule
    • See the schedule board
    • Assign team members
    • Change service windows
Show code
<!-- Set aria-checked on the leaves; groups work theirs out. -->
<ul class="basalt-tree" role="tree" id="perms" aria-label="Permissions"
    aria-multiselectable="true"
    data-basalt-tree data-basalt-tree-check data-basalt-tree-count="#perm-count">
  <li role="treeitem" aria-expanded="true">
    <div class="basalt-tree-row"><span class="basalt-tree-label">Visits</span></div>
    <ul role="group">
      <li role="treeitem" aria-checked="true">
        <div class="basalt-tree-row"><span class="basalt-tree-label">See visits</span></div>
      </li>
      <li role="treeitem" aria-checked="false">
        <div class="basalt-tree-row"><span class="basalt-tree-label">Delete visits</span></div>
      </li>
    </ul>
  </li>
</ul>
<p id="perm-count"></p>
<button type="button" class="btn btn-sm btn-outline-secondary"
        data-basalt-tree-check-all="#perms">Check all</button>

Searchable tree

Filters as you type, keeps the folders that lead to a match open, and marks the words that match. Clearing the box puts every folder back the way it was.

Tree
  • Clothing
Show code
<input type="search" class="form-control" aria-label="Search the catalog"
       aria-controls="catalog" aria-describedby="catalog-count"
       data-basalt-tree-search="#catalog"
       data-basalt-tree-search-count="#catalog-count"
       data-basalt-tree-search-empty="#catalog-empty">
<p id="catalog-count" aria-live="polite"></p>

<ul class="basalt-tree" role="tree" id="catalog" aria-label="Catalog" data-basalt-tree>
  …
</ul>
<p id="catalog-empty" hidden>Nothing matches that.</p>

Lazy-loading tree

Folders fetch what is inside them the first time they open, with placeholder rows while they wait. One folder here fails on purpose, so you can see the retry row.

Tree
Show code
<li role="treeitem" aria-expanded="false" data-basalt-tree-lazy="fuel">
  <div class="basalt-tree-row"><span class="basalt-tree-label">Fleet fuel cards</span></div>
  <ul role="group" hidden></ul>
</li>

<script>
  tree.addEventListener('basalt:tree-load', (e) => {
    fetch('/api/folders/' + e.detail.key)
      .then((r) => r.text())
      .then(e.detail.resolve)             // HTML for the <li> rows
      .catch(() => e.detail.reject());    // shows a retry row
  });
</script>

Drag-and-drop tree

Drag a row above, below or onto a folder. A line shows where it will land, closed folders open when you hover them, and every move can be undone. From the keyboard, hold Alt and use the arrow keys, or use the buttons beside the tree.

Tree
  • Clothing
    • Evening
      • Bar Loafers$790
      • Tiered Organza Dress$1,680
      • Draped Column Gown$2,150
  • Accessories
    • Two-Tone Oxfords$850
    • Cat-Eye Sunglasses$465
    • Shield Sunglasses$520
  • Services
    • Private Styling Session$650

    Move the highlighted row

    Try dragging the styling session into Services.

    Alt ↑ ↓
    Move up or down
    Alt →
    Into the folder above
    Alt ←
    Out of its folder
    Ctrl Z
    Undo the last move
    Show code
    <!-- The script adds the carets, grips and roving focus.
         A folder is any item with a role="group" list inside. -->
    <ul class="basalt-tree" role="tree" id="store-tree" aria-label="Marlo shop catalog"
        data-basalt-tree data-basalt-tree-dnd data-basalt-tree-counts
        data-basalt-tree-status="#menu-status">
      <li role="treeitem" aria-expanded="true">
        <div class="basalt-tree-row">
          <i class="bi bi-folder2-open basalt-tree-icon basalt-tree-icon-folder" aria-hidden="true"></i>
          <span class="basalt-tree-label">Clothing</span>
        </div>
        <ul role="group">
          <li role="treeitem">
            <div class="basalt-tree-row">
              <span class="basalt-tree-label">Bar Loafers</span>
              <span class="basalt-tree-meta">$790</span>
            </div>
          </li>
        </ul>
      </li>
    </ul>
    
    <!-- Optional buttons: up, down, in, out and undo -->
    <button type="button" class="btn btn-sm btn-outline-secondary"
            data-basalt-tree-cmd="in" data-basalt-tree-for="#store-tree">Into folder</button>
    <p id="menu-status"></p>
    
    <script>
      document.getElementById('store-tree').addEventListener('basalt:tree-move', (e) => {
        // e.detail: { item, parent, index } - save the new order here
      });
    </script>

    File tree with inline rename

    Click to select, double-click a name or press F2 to rename it in place. Enter saves and Esc cancels. Type a letter to jump to the next name that starts with it.

    Tree
    Selected Estimates / Estimate form.xlsx
    • Estimates
      • Estimate form.xlsx48 KB
      • Terms.docx31 KB
    Show code
    <ul class="basalt-tree" role="tree" id="drive" aria-label="Shared drive"
        data-basalt-tree data-basalt-tree-rename data-basalt-tree-out="#drive-path">
      <li role="treeitem" aria-selected="true">
        <div class="basalt-tree-row">
          <i class="bi bi-file-earmark-spreadsheet basalt-tree-icon" aria-hidden="true"></i>
          <span class="basalt-tree-label">Estimate form.xlsx</span>
          <span class="basalt-tree-meta">48 KB</span>
        </div>
      </li>
    </ul>
    <p id="drive-path"></p>
    
    <script>
      tree.addEventListener('basalt:tree-rename', (e) => {
        // e.detail: { item, from, to }
      });
    </script>

    Sortable list group

    A plain Bootstrap list group with a grip added to each row. The sortable script does the rest, so an existing list becomes reorderable without changing its look.

    Sortable
    • Basket
    • Gift wrapping
    • Delivery address
    • PO number
    • Payment
    Show code
    <ul class="list-group basalt-sortable" aria-label="Checkout steps" data-basalt-sortable>
      <li class="list-group-item basalt-sortable-item">
        <button class="basalt-sortable-handle" type="button" aria-label="Move Basket">
          <i class="bi bi-grip-vertical" aria-hidden="true"></i>
        </button>
        <span class="basalt-sortable-main">Basket</span>
      </li>
    </ul>

    Ranked sortable list

    The numbers follow the order, and the other rows glide out of the way. Drag by the grip, or focus it and press Space, then ↑ ↓. Esc puts the row back.

    Sortable
    1. Nugent residence14 Moorings St · Weekly home clean 8:00 am
    2. Coppice Lane Records2 Coppice Lane · Office deep clean 9:30 am
    3. Wharfside Road Apartments88 Quay Rd · Window clean 11:00 am
    4. Orrery Square Library1 Orrery Sq · Carpet and rug clean 1:30 pm
    5. Brennan residence40 Fore St · Quote visit 3:00 pm
    Show code
    <ol class="basalt-sortable basalt-sortable-ranked" aria-label="Friday rounds, Car 3"
        data-basalt-sortable>
      <li class="basalt-sortable-item">
        <button class="basalt-sortable-handle" type="button" aria-label="Move Wharfside Road Apartments">
          <i class="bi bi-grip-vertical" aria-hidden="true"></i>
        </button>
        <span class="basalt-sortable-rank" aria-hidden="true"></span>
        <span class="basalt-sortable-main">
          <span class="basalt-sortable-title">Wharfside Road Apartments</span>
          <span class="basalt-sortable-sub">88 Quay Rd &middot; Window clean</span>
        </span>
        <span class="basalt-sortable-end">11:00 am</span>
      </li>
    </ol>
    
    <script>
      list.addEventListener('basalt:sortable-change', (e) => {
        // e.detail: { item, from, to, index }
      });
    </script>

    Sortable grid

    Tiles that trade places in two directions, like arranging a dashboard. Drag anywhere on a tile. With the keyboard, pick one up from its grip and all four arrow keys move it.

    Sortable
    • Revenue$96,820 12%
    • Visits completed412 8%
    • On-time arrival rate87% 5%
    • New customers38 3%
    • Average visit value$235 2%
    • Late arrivals3 2 fewer
    Show code
    <ul class="basalt-sortable basalt-sortable-grid" aria-label="Dashboard tiles"
        data-basalt-sortable data-basalt-sortable-whole>
      <li class="basalt-sortable-item basalt-sortable-tile">
        <button class="basalt-sortable-handle" type="button" aria-label="Move Revenue">
          <i class="bi bi-grip-horizontal" aria-hidden="true"></i>
        </button>
        <span class="basalt-sortable-sub">Revenue</span>
        <span class="basalt-sortable-value">$96,820</span>
      </li>
    </ul>

    Transfer list

    Two Bootstrap list groups with checkboxes and a search in each. Check what you want and send it across; the buttons only light up when there is something to move.

    Between lists

    Available products

    0 of 6 checked

    Winter gift box contents

    0 of 2 checked
    Show code
    <div class="basalt-transfer" data-basalt-transfer>
      <section class="basalt-transfer-pane" aria-labelledby="avail">
        <header class="basalt-transfer-head">
          <input class="form-check-input" type="checkbox" data-basalt-transfer-all
                 aria-label="Select every available product">
          <h3 class="basalt-transfer-title" id="avail">Available</h3>
          <span class="basalt-transfer-count" data-basalt-transfer-count></span>
        </header>
        <div class="list-group list-group-flush basalt-transfer-list"
             data-basalt-transfer-list="source">
          <label class="list-group-item">
            <input class="form-check-input" type="checkbox"> <span>Draped Column Gown</span>
          </label>
        </div>
      </section>
      <div class="basalt-transfer-moves" role="group" aria-label="Move products">
        <button type="button" class="btn btn-sm btn-outline-secondary"
                data-basalt-transfer-move="to-target" disabled>…</button>
        <button type="button" class="btn btn-sm btn-outline-secondary"
                data-basalt-transfer-move="to-source" disabled>…</button>
      </div>
      <!-- second pane with data-basalt-transfer-list="target" -->
    </div>

    Board with column limits

    Cards move between columns and a gap opens where they will land. Each column can set a limit, and the header warns when it goes over. From the keyboard, press Space on a grip to pick a card up, the arrow keys to move it, and Space again to put it down.

    Between lists

    To pack

    3 of 4
    • #4218 Arbor North Studio 8 blazers · Wholesale
      Rush
    • #4221 Cheryl Sorensen 2 gowns, 2 pairs of oxfords
    • #4223 Travis Daugherty Reorder · 2 pairs of loafers

    Packing

    2 of 2
    • #4209 The Silk Room 12 bar loafers · Wholesale
      Swing tags
    • #4214 Christopher Vance Cat-eye sunglasses

    Ready to ship

    1
    • #4202 Cheryl Schultz Courier at 3pm
      Label printed
    Show code
    <div class="basalt-board">
      <section class="basalt-board-col" aria-labelledby="col-packing">
        <header class="basalt-board-head">
          <h3 class="basalt-board-title" id="col-packing">
            <span class="basalt-board-dot" aria-hidden="true"></span>Packing
          </h3>
          <span class="basalt-board-count" id="col-packing-count">2 of 2</span>
        </header>
        <!-- Lists with the same group trade cards. The limit is optional. -->
        <ul class="basalt-sortable" aria-labelledby="col-packing"
            data-basalt-sortable data-basalt-sortable-group="orders"
            data-basalt-sortable-whole data-basalt-sortable-limit="2"
            data-basalt-sortable-count="#col-packing-count">
          <li class="basalt-sortable-item basalt-board-card">
            <button class="basalt-sortable-handle" type="button" aria-label="Move order 4209">
              <i class="bi bi-grip-vertical" aria-hidden="true"></i>
            </button>
            <div class="basalt-board-body">
              <span class="basalt-board-ref">#4209</span>
              <span class="basalt-board-name">Ironbridge Fintech</span>
            </div>
          </li>
        </ul>
      </section>
      <!-- …more columns -->
    </div>