網站導覽

v2.1.0
網站導覽

樹狀檢視

以巢狀列呈現有層級的項目,例如資料夾、大綱或分類,不需要腳本也能展開與收合。

本頁內容

樹狀檢視以巢狀列呈現有層級的項目,例如資料夾、大綱或分類。沒有腳本時,分支使用原生 <details>,並以 <summary> 作為該列,因此可以用瀏覽器原本支援的按鍵展開或收合。在頁面中搜尋時,瀏覽器也會展開包含相符結果的分支。

樹狀檢視

<ul class="tree max-w(260) mx-auto">  <li>    <details class="tree-group" open>      <summary class="tree-item tree-toggle">        <svg class="icon tree-chevron" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">          <path d="M6 3.5L10.3586 7.8586A0.2 0.2 0 0 1 10.3586 8.1414L6 12.5" />        </svg>packages      </summary>      <ul class="tree-sub">        <li>          <details class="tree-group" open>            <summary class="tree-item tree-toggle">              <svg class="icon tree-chevron" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">                <path d="M6 3.5L10.3586 7.8586A0.2 0.2 0 0 1 10.3586 8.1414L6 12.5" />              </svg>ui            </summary>            <ul class="tree-sub">              <li><a class="tree-item" href="#button" aria-current="page">button.css</a></li>              <li><a class="tree-item" href="#input">input.css</a></li>              <li><a class="tree-item" href="#theme">theme.css</a></li>            </ul>          </details>        </li>        <li>          <details class="tree-group">            <summary class="tree-item tree-toggle">              <svg class="icon tree-chevron" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">                <path d="M6 3.5L10.3586 7.8586A0.2 0.2 0 0 1 10.3586 8.1414L6 12.5" />              </svg>tests            </summary>            <ul class="tree-sub">              <li><a class="tree-item" href="#test">master.test.ts</a></li>            </ul>          </details>        </li>      </ul>    </details>  </li>  <li><a class="tree-item" href="#readme">README.md</a></li>  <li><a class="tree-item" href="#license">LICENSE</a></li></ul>
import ChevronRightIcon from '@master/icons-react/native/16/soft/chevron-right'<ul className="tree max-w(260) mx-auto">  <li>    <details className="tree-group" open>      <summary className="tree-item tree-toggle">        <ChevronRightIcon className="icon tree-chevron" />packages      </summary>      <ul className="tree-sub">        <li>          <details className="tree-group" open>            <summary className="tree-item tree-toggle">              <ChevronRightIcon className="icon tree-chevron" />ui            </summary>            <ul className="tree-sub">              <li><a className="tree-item" href="#button" aria-current="page">button.css</a></li>              <li><a className="tree-item" href="#input">input.css</a></li>              <li><a className="tree-item" href="#theme">theme.css</a></li>            </ul>          </details>        </li>        <li>          <details className="tree-group">            <summary className="tree-item tree-toggle">              <ChevronRightIcon className="icon tree-chevron" />tests            </summary>            <ul className="tree-sub">              <li><a className="tree-item" href="#test">master.test.ts</a></li>            </ul>          </details>        </li>      </ul>    </details>  </li>  <li><a className="tree-item" href="#readme">README.md</a></li>  <li><a className="tree-item" href="#license">LICENSE</a></li></ul>
<script>  import ChevronRightIcon from '@master/icons-svelte/native/16/soft/chevron-right'</script><ul class="tree max-w(260) mx-auto">  <li>    <details class="tree-group" open>      <summary class="tree-item tree-toggle">        <ChevronRightIcon class="icon tree-chevron" />packages      </summary>      <ul class="tree-sub">        <li>          <details class="tree-group" open>            <summary class="tree-item tree-toggle">              <ChevronRightIcon class="icon tree-chevron" />ui            </summary>            <ul class="tree-sub">              <li><a class="tree-item" href="#button" aria-current="page">button.css</a></li>              <li><a class="tree-item" href="#input">input.css</a></li>              <li><a class="tree-item" href="#theme">theme.css</a></li>            </ul>          </details>        </li>        <li>          <details class="tree-group">            <summary class="tree-item tree-toggle">              <ChevronRightIcon class="icon tree-chevron" />tests            </summary>            <ul class="tree-sub">              <li><a class="tree-item" href="#test">master.test.ts</a></li>            </ul>          </details>        </li>      </ul>    </details>  </li>  <li><a class="tree-item" href="#readme">README.md</a></li>  <li><a class="tree-item" href="#license">LICENSE</a></li></ul>
<script setup>import ChevronRightIcon from '@master/icons-vue/native/16/soft/chevron-right'</script><template>  <ul class="tree max-w(260) mx-auto">    <li>      <details class="tree-group" open>        <summary class="tree-item tree-toggle">          <ChevronRightIcon class="icon tree-chevron" />packages        </summary>        <ul class="tree-sub">          <li>            <details class="tree-group" open>              <summary class="tree-item tree-toggle">                <ChevronRightIcon class="icon tree-chevron" />ui              </summary>              <ul class="tree-sub">                <li><a class="tree-item" href="#button" aria-current="page">button.css</a></li>                <li><a class="tree-item" href="#input">input.css</a></li>                <li><a class="tree-item" href="#theme">theme.css</a></li>              </ul>            </details>          </li>          <li>            <details class="tree-group">              <summary class="tree-item tree-toggle">                <ChevronRightIcon class="icon tree-chevron" />tests              </summary>              <ul class="tree-sub">                <li><a class="tree-item" href="#test">master.test.ts</a></li>              </ul>            </details>          </li>        </ul>      </details>    </li>    <li><a class="tree-item" href="#readme">README.md</a></li>    <li><a class="tree-item" href="#license">LICENSE</a></li>  </ul></template>

#列

  • tree-item 代表一列,可以是連結、按鈕或分支的 <summary>。每列高 28px,排列緊湊、色調低調。指標停留的列會套用膠囊標籤的底色,目前所在列則以帶有層次感的小面板樣式浮起,並使用加深的文字顏色。
  • 分支列上的 tree-toggle 以 tree-chevron 圖示開頭,圖示位於最前方的欄位。分支展開時,圖示會轉向下方。每列都保留這一欄,因此末端項目會與同層分支對齊。
  • tree-sub 容納分支下一層的列,並沿著從箭頭向下延伸的細線縮排。

#搭配圖示

在箭頭欄後方放入套用 icon 的圖示。圖示平時維持低調色調,直到該列成為目前所在列。

搭配圖示

<ul class="tree max-w(260) mx-auto">  <li>    <details class="tree-group" open>      <summary class="tree-item tree-toggle">        <svg class="icon tree-chevron" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">          <path d="M6 3.5L10.3586 7.8586A0.2 0.2 0 0 1 10.3586 8.1414L6 12.5" />        </svg>        <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">          <path d="M2 12L2 4A1 1 0 0 1 3 3L6.5858 3A1 1 0 0 1 7.2929 3.2929L8.2071 4.2071A1 1 0 0 0 8.9142 4.5L13 4.5A1 1 0 0 1 14 5.5L14 12A1 1 0 0 1 13 13L3 13A1 1 0 0 1 2 12Z" />        </svg> Design      </summary>      <ul class="tree-sub">        <li>          <a class="tree-item" href="#tokens" aria-current="page">            <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">              <path d="M4.5 2L9.0858 2A1 1 0 0 1 9.7929 2.2929L12.2071 4.7071A1 1 0 0 1 12.5 5.4142L12.5 13A1 1 0 0 1 11.5 14L4.5 14A1 1 0 0 1 3.5 13L3.5 3A1 1 0 0 1 4.5 2Z" /><path d="M10 2.5V4.4A0.1 0.1 0 0 0 10.1 4.5H12L12.2409 4.7427A0.9993 0.9993 0 0 1 12.5 5.4142V5.5H10.1Q9.6444 5.5 9.3222 5.1778Q9 4.8556 9 4.4V2A1.1002 1.1002 0 0 1 9.3761 2.0431A1.0001 1.0001 0 0 1 9.7929 2.2929L10 2.5Z" fill="currentColor" stroke="none" fill-rule="evenodd" />            </svg> Tokens          </a>        </li>        <li>          <a class="tree-item" href="#materials">            <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">              <path d="M4.5 2L9.0858 2A1 1 0 0 1 9.7929 2.2929L12.2071 4.7071A1 1 0 0 1 12.5 5.4142L12.5 13A1 1 0 0 1 11.5 14L4.5 14A1 1 0 0 1 3.5 13L3.5 3A1 1 0 0 1 4.5 2Z" /><path d="M10 2.5V4.4A0.1 0.1 0 0 0 10.1 4.5H12L12.2409 4.7427A0.9993 0.9993 0 0 1 12.5 5.4142V5.5H10.1Q9.6444 5.5 9.3222 5.1778Q9 4.8556 9 4.4V2A1.1002 1.1002 0 0 1 9.3761 2.0431A1.0001 1.0001 0 0 1 9.7929 2.2929L10 2.5Z" fill="currentColor" stroke="none" fill-rule="evenodd" />            </svg> Materials          </a>        </li>      </ul>    </details>  </li>  <li>    <details class="tree-group">      <summary class="tree-item tree-toggle">        <svg class="icon tree-chevron" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">          <path d="M6 3.5L10.3586 7.8586A0.2 0.2 0 0 1 10.3586 8.1414L6 12.5" />        </svg>        <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">          <path d="M2 12L2 4A1 1 0 0 1 3 3L6.5858 3A1 1 0 0 1 7.2929 3.2929L8.2071 4.2071A1 1 0 0 0 8.9142 4.5L13 4.5A1 1 0 0 1 14 5.5L14 12A1 1 0 0 1 13 13L3 13A1 1 0 0 1 2 12Z" />        </svg> Engineering      </summary>      <ul class="tree-sub">        <li><a class="tree-item" href="#api">API</a></li>      </ul>    </details>  </li></ul>
import ChevronRightIcon from '@master/icons-react/native/16/soft/chevron-right'import FolderIcon from '@master/icons-react/native/16/soft/folder'import FileIcon from '@master/icons-react/native/16/soft/file'<ul className="tree max-w(260) mx-auto">  <li>    <details className="tree-group" open>      <summary className="tree-item tree-toggle">        <ChevronRightIcon className="icon tree-chevron" />        <FolderIcon className="icon" /> Design      </summary>      <ul className="tree-sub">        <li>          <a className="tree-item" href="#tokens" aria-current="page">            <FileIcon className="icon" /> Tokens          </a>        </li>        <li>          <a className="tree-item" href="#materials"><FileIcon className="icon" /> Materials</a>        </li>      </ul>    </details>  </li>  <li>    <details className="tree-group">      <summary className="tree-item tree-toggle">        <ChevronRightIcon className="icon tree-chevron" />        <FolderIcon className="icon" /> Engineering      </summary>      <ul className="tree-sub">        <li><a className="tree-item" href="#api">API</a></li>      </ul>    </details>  </li></ul>
<script>  import ChevronRightIcon from '@master/icons-svelte/native/16/soft/chevron-right'  import FolderIcon from '@master/icons-svelte/native/16/soft/folder'  import FileIcon from '@master/icons-svelte/native/16/soft/file'</script><ul class="tree max-w(260) mx-auto">  <li>    <details class="tree-group" open>      <summary class="tree-item tree-toggle">        <ChevronRightIcon class="icon tree-chevron" />        <FolderIcon class="icon" /> Design      </summary>      <ul class="tree-sub">        <li>          <a class="tree-item" href="#tokens" aria-current="page">            <FileIcon class="icon" /> Tokens          </a>        </li>        <li><a class="tree-item" href="#materials"><FileIcon class="icon" /> Materials</a></li>      </ul>    </details>  </li>  <li>    <details class="tree-group">      <summary class="tree-item tree-toggle">        <ChevronRightIcon class="icon tree-chevron" />        <FolderIcon class="icon" /> Engineering      </summary>      <ul class="tree-sub">        <li><a class="tree-item" href="#api">API</a></li>      </ul>    </details>  </li></ul>
<script setup>import ChevronRightIcon from '@master/icons-vue/native/16/soft/chevron-right'import FolderIcon from '@master/icons-vue/native/16/soft/folder'import FileIcon from '@master/icons-vue/native/16/soft/file'</script><template>  <ul class="tree max-w(260) mx-auto">    <li>      <details class="tree-group" open>        <summary class="tree-item tree-toggle">          <ChevronRightIcon class="icon tree-chevron" />          <FolderIcon class="icon" /> Design        </summary>        <ul class="tree-sub">          <li>            <a class="tree-item" href="#tokens" aria-current="page">              <FileIcon class="icon" /> Tokens            </a>          </li>          <li><a class="tree-item" href="#materials"><FileIcon class="icon" /> Materials</a></li>        </ul>      </details>    </li>    <li>      <details class="tree-group">        <summary class="tree-item tree-toggle">          <ChevronRightIcon class="icon tree-chevron" />          <FolderIcon class="icon" /> Engineering        </summary>        <ul class="tree-sub">          <li><a class="tree-item" href="#api">API</a></li>        </ul>      </details>    </li>  </ul></template>

#行為

WAI-ARIA 樹狀檢視模式只設一個 Tab 停駐點,並可用方向鍵在樹中移動及展開或收合分支。這需要透過腳本套用 tree 行為,並沿用相同的類別。為清單加上 role="tree",為每個分支的列加上 role="treeitem" 和 aria-expanded,再為其 tree-sub 加上 role="group"。箭頭會依 aria-expanded 改變方向,設為 aria-expanded="false" 的列會隱藏緊接在後的 tree-sub,而 aria-selected="true" 會標記已選取的列。

由 ARIA 控制

<ul class="tree max-w(260) mx-auto" role="tree" aria-label="Outline">  <li role="none">    <div class="tree-item tree-toggle" role="treeitem" aria-expanded="true" tabindex="0">      <svg class="icon tree-chevron" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">        <path d="M6 3.5L10.3586 7.8586A0.2 0.2 0 0 1 10.3586 8.1414L6 12.5" />      </svg>Introduction    </div>    <ul class="tree-sub" role="group">      <li role="none">        <div class="tree-item" role="treeitem" aria-selected="true" tabindex="-1">          How it fits together        </div>      </li>      <li role="none">        <div class="tree-item" role="treeitem" tabindex="-1">Principles</div>      </li>    </ul>  </li>  <li role="none">    <div class="tree-item tree-toggle" role="treeitem" aria-expanded="false" tabindex="-1">      <svg class="icon tree-chevron" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">        <path d="M6 3.5L10.3586 7.8586A0.2 0.2 0 0 1 10.3586 8.1414L6 12.5" />      </svg>Theme    </div>    <ul class="tree-sub" role="group">      <li role="none">        <div class="tree-item" role="treeitem" tabindex="-1">Surfaces</div>      </li>    </ul>  </li></ul><script type="module">  import { attach } from '@master/ui/behaviors'  attach()</script>
import { useEffect, useRef } from 'react'import { attach } from '@master/ui/behaviors'import ChevronRightIcon from '@master/icons-react/native/16/soft/chevron-right'export function Example() {  const root = useRef<HTMLUListElement>(null)  useEffect(() => attach(root.current!), [])  return (    <ul ref={root} className="tree max-w(260) mx-auto" role="tree" aria-label="Outline">      <li role="none">        <div className="tree-item tree-toggle" role="treeitem" aria-expanded="true" tabIndex={0}>          <ChevronRightIcon className="icon tree-chevron" />Introduction        </div>        <ul className="tree-sub" role="group">          <li role="none">            <div className="tree-item" role="treeitem" aria-selected="true" tabIndex={-1}>              How it fits together            </div>          </li>          <li role="none">            <div className="tree-item" role="treeitem" tabIndex={-1}>Principles</div>          </li>        </ul>      </li>      <li role="none">        <div className="tree-item tree-toggle" role="treeitem" aria-expanded="false" tabIndex={-1}>          <ChevronRightIcon className="icon tree-chevron" />Theme        </div>        <ul className="tree-sub" role="group">          <li role="none">            <div className="tree-item" role="treeitem" tabIndex={-1}>Surfaces</div>          </li>        </ul>      </li>    </ul>  )}
<script>  import { attach } from '@master/ui/behaviors'  import ChevronRightIcon from '@master/icons-svelte/native/16/soft/chevron-right'</script><ul class="tree max-w(260) mx-auto" role="tree" aria-label="Outline" {@attach attach}>  <li role="none">    <div class="tree-item tree-toggle" role="treeitem" aria-expanded="true" tabindex="0">      <ChevronRightIcon class="icon tree-chevron" />Introduction    </div>    <ul class="tree-sub" role="group">      <li role="none">        <div class="tree-item" role="treeitem" aria-selected="true" tabindex="-1">          How it fits together        </div>      </li>      <li role="none">        <div class="tree-item" role="treeitem" tabindex="-1">Principles</div>      </li>    </ul>  </li>  <li role="none">    <div class="tree-item tree-toggle" role="treeitem" aria-expanded="false" tabindex="-1">      <ChevronRightIcon class="icon tree-chevron" />Theme    </div>    <ul class="tree-sub" role="group">      <li role="none">        <div class="tree-item" role="treeitem" tabindex="-1">Surfaces</div>      </li>    </ul>  </li></ul>
<script setup>import { onMounted, onUnmounted, ref } from 'vue'import { attach } from '@master/ui/behaviors'import ChevronRightIcon from '@master/icons-vue/native/16/soft/chevron-right'const root = ref(null)let detachonMounted(() => { detach = attach(root.value) })onUnmounted(() => detach?.())</script><template>  <ul ref="root" class="tree max-w(260) mx-auto" role="tree" aria-label="Outline">    <li role="none">      <div class="tree-item tree-toggle" role="treeitem" aria-expanded="true" tabindex="0">        <ChevronRightIcon class="icon tree-chevron" />Introduction      </div>      <ul class="tree-sub" role="group">        <li role="none">          <div class="tree-item" role="treeitem" aria-selected="true" tabindex="-1">            How it fits together          </div>        </li>        <li role="none">          <div class="tree-item" role="treeitem" tabindex="-1">Principles</div>        </li>      </ul>    </li>    <li role="none">      <div class="tree-item tree-toggle" role="treeitem" aria-expanded="false" tabindex="-1">        <ChevronRightIcon class="icon tree-chevron" />Theme      </div>      <ul class="tree-sub" role="group">        <li role="none">          <div class="tree-item" role="treeitem" tabindex="-1">Surfaces</div>        </li>      </ul>    </li>  </ul></template>
將焦點移到樹狀檢視後,使用方向鍵操作。按 Enter、空白鍵或點一下可展開分支,或選取沒有子項目的列。

tree 接收帶有 tree 類別的根元素,並傳回用來移除行為的函式。它的選項用來開啟重新命名功能:rename: true,以及欄位的可存取名稱 renameLabel,預設為 Rename <label>。

app.js
import { tree } from '@master/ui/behaviors/tree.js'const root = document.querySelector('.tree')const files = tree(root)root.addEventListener('change', () => console.log(files.value))
  • 單一 Tab 停駐點。 此行為透過 tabindex 只保留一列在 Tab 巡覽順序中。優先採用標記中設有 tabindex="0" 的列,其次是已選取或目前所在的列,最後才是第一列。取得焦點的列會成為新的 Tab 停駐點。卸除行為時,各列會恢復原有的 tabindex。
  • 移動。 使用方向鍵移動時,會跳過收合分支內的列、標記為 hidden 的列,以及停用的列。
  • 啟用。 摘要列、連結和按鈕沿用瀏覽器原有的啟用方式。若列本身沒有原生啟用方式,例如加上 role="treeitem" 的 div,可用 Enter、Space 或點擊啟用。分支會展開或收合,末端項目則會成為已選取的列,aria-selected="true" 也會從原先選取的列移到該列。
  • 值。 tree 傳回的函式帶有 value:已選取的列所代表的值,也就是它的 data-value 或文字,沒有選取任何列時為 null。在可多選的樹狀檢視和核取方塊樹狀檢視中,它是一個清單。從外部設定它即可選取或勾選列,不會派發事件。
  • 事件。 每個事件都會從根元素向上冒泡,並在 detail.item 中提供該列。選取或勾選時依序派發 input 和 change。展開分支時派發 expand,並在 detail.value 中提供該列所代表的值。重新命名時派發 rename,附帶 detail.value,以及放在 detail.label 中的新標籤。
  • 停用。 帶有 disabled 或 aria-disabled="true" 的列不會回應操作:連結不會開啟,分支會維持原狀,也不會被選取、勾選或重新命名。

此行為也適用於以 <details> 建立的樹狀檢視,例如前兩個未套用此行為的預覽。它透過 open 展開或收合 tree-group,並讓整棵樹只保留一個 Tab 停駐點,沿用同一套方向鍵操作。attach() 會將此行為套用到所有樹狀檢視。若要保留 <details> 樹狀檢視的原生操作方式,請依名稱匯入所需的行為。

#多列

在樹狀檢視上設定 aria-multiselectable="true" 後,就能像檔案清單一樣選取任意數量的末端項目。每個可選取的列都會標示 aria-selected,值為 true 或 false;分支不會被選取,只會展開或收合。值是已選取的列組成的清單,依樹狀檢視中的順序排列。

多列

<ul class="tree max-w(260) mx-auto" role="tree" aria-label="Files" aria-multiselectable="true">  <li role="none">    <div class="tree-item tree-toggle" role="treeitem" aria-expanded="true">      <svg class="icon tree-chevron" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">        <path d="M6 3.5L10.3586 7.8586A0.2 0.2 0 0 1 10.3586 8.1414L6 12.5" />      </svg>components    </div>    <ul class="tree-sub" role="group">      <li role="none">        <div class="tree-item" role="treeitem" aria-selected="true">button.css</div>      </li>      <li role="none">        <div class="tree-item" role="treeitem" aria-selected="true">input.css</div>      </li>      <li role="none">        <div class="tree-item" role="treeitem" aria-selected="false">theme.css</div>      </li>      <li role="none">        <div class="tree-item" role="treeitem" aria-selected="false">tree.css</div>      </li>    </ul>  </li>  <li role="none">    <div class="tree-item tree-toggle" role="treeitem" aria-expanded="false">      <svg class="icon tree-chevron" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">        <path d="M6 3.5L10.3586 7.8586A0.2 0.2 0 0 1 10.3586 8.1414L6 12.5" />      </svg>themes    </div>    <ul class="tree-sub" role="group">      <li role="none">        <div class="tree-item" role="treeitem" aria-selected="false">paper.css</div>      </li>    </ul>  </li>  <li role="none">    <div class="tree-item" role="treeitem" aria-selected="false">README.md</div>  </li></ul><script type="module">  import { attach } from '@master/ui/behaviors'  attach()</script>
import { useEffect, useRef } from 'react'import { attach } from '@master/ui/behaviors'import ChevronRightIcon from '@master/icons-react/native/16/soft/chevron-right'export function Example() {  const root = useRef<HTMLUListElement>(null)  useEffect(() => attach(root.current!), [])  return (    <ul ref={root} className="tree max-w(260) mx-auto" role="tree" aria-label="Files" aria-multiselectable="true">      <li role="none">        <div className="tree-item tree-toggle" role="treeitem" aria-expanded="true">          <ChevronRightIcon className="icon tree-chevron" />components        </div>        <ul className="tree-sub" role="group">          <li role="none">            <div className="tree-item" role="treeitem" aria-selected="true">button.css</div>          </li>          <li role="none">            <div className="tree-item" role="treeitem" aria-selected="true">input.css</div>          </li>          <li role="none">            <div className="tree-item" role="treeitem" aria-selected="false">theme.css</div>          </li>          <li role="none">            <div className="tree-item" role="treeitem" aria-selected="false">tree.css</div>          </li>        </ul>      </li>      <li role="none">        <div className="tree-item tree-toggle" role="treeitem" aria-expanded="false">          <ChevronRightIcon className="icon tree-chevron" />themes        </div>        <ul className="tree-sub" role="group">          <li role="none">            <div className="tree-item" role="treeitem" aria-selected="false">paper.css</div>          </li>        </ul>      </li>      <li role="none">        <div className="tree-item" role="treeitem" aria-selected="false">README.md</div>      </li>    </ul>  )}
<script>  import { attach } from '@master/ui/behaviors'  import ChevronRightIcon from '@master/icons-svelte/native/16/soft/chevron-right'</script><ul class="tree max-w(260) mx-auto" role="tree" aria-label="Files" aria-multiselectable="true" {@attach attach}>  <li role="none">    <div class="tree-item tree-toggle" role="treeitem" aria-expanded="true">      <ChevronRightIcon class="icon tree-chevron" />components    </div>    <ul class="tree-sub" role="group">      <li role="none">        <div class="tree-item" role="treeitem" aria-selected="true">button.css</div>      </li>      <li role="none">        <div class="tree-item" role="treeitem" aria-selected="true">input.css</div>      </li>      <li role="none">        <div class="tree-item" role="treeitem" aria-selected="false">theme.css</div>      </li>      <li role="none">        <div class="tree-item" role="treeitem" aria-selected="false">tree.css</div>      </li>    </ul>  </li>  <li role="none">    <div class="tree-item tree-toggle" role="treeitem" aria-expanded="false">      <ChevronRightIcon class="icon tree-chevron" />themes    </div>    <ul class="tree-sub" role="group">      <li role="none">        <div class="tree-item" role="treeitem" aria-selected="false">paper.css</div>      </li>    </ul>  </li>  <li role="none">    <div class="tree-item" role="treeitem" aria-selected="false">README.md</div>  </li></ul>
<script setup>import { onMounted, onUnmounted, ref } from 'vue'import { attach } from '@master/ui/behaviors'import ChevronRightIcon from '@master/icons-vue/native/16/soft/chevron-right'const root = ref(null)let detachonMounted(() => { detach = attach(root.value) })onUnmounted(() => detach?.())</script><template>  <ul ref="root" class="tree max-w(260) mx-auto" role="tree" aria-label="Files" aria-multiselectable="true">    <li role="none">      <div class="tree-item tree-toggle" role="treeitem" aria-expanded="true">        <ChevronRightIcon class="icon tree-chevron" />components      </div>      <ul class="tree-sub" role="group">        <li role="none">          <div class="tree-item" role="treeitem" aria-selected="true">button.css</div>        </li>        <li role="none">          <div class="tree-item" role="treeitem" aria-selected="true">input.css</div>        </li>        <li role="none">          <div class="tree-item" role="treeitem" aria-selected="false">theme.css</div>        </li>        <li role="none">          <div class="tree-item" role="treeitem" aria-selected="false">tree.css</div>        </li>      </ul>    </li>    <li role="none">      <div class="tree-item tree-toggle" role="treeitem" aria-expanded="false">        <ChevronRightIcon class="icon tree-chevron" />themes      </div>      <ul class="tree-sub" role="group">        <li role="none">          <div class="tree-item" role="treeitem" aria-selected="false">paper.css</div>        </li>      </ul>    </li>    <li role="none">      <div class="tree-item" role="treeitem" aria-selected="false">README.md</div>    </li>  </ul></template>
按一下某一列,再按住 Shift 按另一列,即可選取兩者之間的列。按住 Control 或 Command 再點擊可加選一列;用鍵盤時按 Space 也有相同效果。
  • 點擊只會選取該列。按住 Control 或 Command 再點擊,會選取或取消選取一列,並保留其他列的選取狀態。Enter 的作用與點擊相同。
  • Space 會選取或取消選取取得焦點的列。方向鍵只會移動。
  • 連續範圍。 最後選擇的列是錨點。按住 Shift 的同時點擊或按下 Space、ArrowDown 或 ArrowUp,會選取從錨點到該列之間所有可見的列;按住 Control 或 Command 加上 Shift,再按 Home 或 End,會一路選到第一列或最後一列。收合分支內的列和停用的列會被略過。
  • 全部。 按住 Control 或 Command 再按 A,會選取所有可見的列;若已全部選取,則全部取消。

#核取方塊樹狀檢視

當各列帶有 aria-checked 時,樹狀檢視就是核取方塊樹狀檢視:每列都有一個勾選框,勾選分支會勾選其下的每一列,包括收合分支內的列。分支會反映其下各列的狀態:全部勾選時為 true,全部未勾選時為 false,其餘情況為 mixed。在每列中放一個帶有 aria-hidden="true" 的 tree-check,位置在箭頭欄之後、標籤之前:它就是核取方塊本身的方框,依該列的 aria-checked 繪製,因此該列的狀態只會播報一次。

核取方塊樹狀檢視

<ul class="tree max-w(260) mx-auto" role="tree" aria-label="Files to publish" aria-multiselectable="true">  <li role="none">    <div class="tree-item tree-toggle" role="treeitem" aria-expanded="true" aria-checked="mixed">      <svg class="icon tree-chevron" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">        <path d="M6 3.5L10.3586 7.8586A0.2 0.2 0 0 1 10.3586 8.1414L6 12.5" />      </svg><span class="tree-check" aria-hidden="true"></span>components    </div>    <ul class="tree-sub" role="group">      <li role="none">        <div class="tree-item" role="treeitem" aria-checked="true">          <span class="tree-check" aria-hidden="true"></span>button.css        </div>      </li>      <li role="none">        <div class="tree-item" role="treeitem" aria-checked="true">          <span class="tree-check" aria-hidden="true"></span>input.css        </div>      </li>      <li role="none">        <div class="tree-item" role="treeitem" aria-checked="false">          <span class="tree-check" aria-hidden="true"></span>theme.css        </div>      </li>      <li role="none">        <div class="tree-item" role="treeitem" aria-checked="false">          <span class="tree-check" aria-hidden="true"></span>tree.css        </div>      </li>    </ul>  </li>  <li role="none">    <div class="tree-item tree-toggle" role="treeitem" aria-expanded="false" aria-checked="false">      <svg class="icon tree-chevron" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">        <path d="M6 3.5L10.3586 7.8586A0.2 0.2 0 0 1 10.3586 8.1414L6 12.5" />      </svg><span class="tree-check" aria-hidden="true"></span>themes    </div>    <ul class="tree-sub" role="group">      <li role="none">        <div class="tree-item" role="treeitem" aria-checked="false">          <span class="tree-check" aria-hidden="true"></span>paper.css        </div>      </li>    </ul>  </li>  <li role="none">    <div class="tree-item" role="treeitem" aria-checked="true">      <span class="tree-check" aria-hidden="true"></span>README.md    </div>  </li></ul><script type="module">  import { attach } from '@master/ui/behaviors'  attach()</script>
import { useEffect, useRef } from 'react'import { attach } from '@master/ui/behaviors'import ChevronRightIcon from '@master/icons-react/native/16/soft/chevron-right'export function Example() {  const root = useRef<HTMLUListElement>(null)  useEffect(() => attach(root.current!), [])  return (    <ul ref={root} className="tree max-w(260) mx-auto" role="tree" aria-label="Files to publish" aria-multiselectable="true">      <li role="none">        <div className="tree-item tree-toggle" role="treeitem" aria-expanded="true" aria-checked="mixed">          <ChevronRightIcon className="icon tree-chevron" /><span className="tree-check" aria-hidden="true"></span>components        </div>        <ul className="tree-sub" role="group">          <li role="none">            <div className="tree-item" role="treeitem" aria-checked="true">              <span className="tree-check" aria-hidden="true"></span>button.css            </div>          </li>          <li role="none">            <div className="tree-item" role="treeitem" aria-checked="true">              <span className="tree-check" aria-hidden="true"></span>input.css            </div>          </li>          <li role="none">            <div className="tree-item" role="treeitem" aria-checked="false">              <span className="tree-check" aria-hidden="true"></span>theme.css            </div>          </li>          <li role="none">            <div className="tree-item" role="treeitem" aria-checked="false">              <span className="tree-check" aria-hidden="true"></span>tree.css            </div>          </li>        </ul>      </li>      <li role="none">        <div className="tree-item tree-toggle" role="treeitem" aria-expanded="false" aria-checked="false">          <ChevronRightIcon className="icon tree-chevron" /><span className="tree-check" aria-hidden="true"></span>themes        </div>        <ul className="tree-sub" role="group">          <li role="none">            <div className="tree-item" role="treeitem" aria-checked="false">              <span className="tree-check" aria-hidden="true"></span>paper.css            </div>          </li>        </ul>      </li>      <li role="none">        <div className="tree-item" role="treeitem" aria-checked="true">          <span className="tree-check" aria-hidden="true"></span>README.md        </div>      </li>    </ul>  )}
<script>  import { attach } from '@master/ui/behaviors'  import ChevronRightIcon from '@master/icons-svelte/native/16/soft/chevron-right'</script><ul class="tree max-w(260) mx-auto" role="tree" aria-label="Files to publish" aria-multiselectable="true" {@attach attach}>  <li role="none">    <div class="tree-item tree-toggle" role="treeitem" aria-expanded="true" aria-checked="mixed">      <ChevronRightIcon class="icon tree-chevron" /><span class="tree-check" aria-hidden="true"></span>components    </div>    <ul class="tree-sub" role="group">      <li role="none">        <div class="tree-item" role="treeitem" aria-checked="true">          <span class="tree-check" aria-hidden="true"></span>button.css        </div>      </li>      <li role="none">        <div class="tree-item" role="treeitem" aria-checked="true">          <span class="tree-check" aria-hidden="true"></span>input.css        </div>      </li>      <li role="none">        <div class="tree-item" role="treeitem" aria-checked="false">          <span class="tree-check" aria-hidden="true"></span>theme.css        </div>      </li>      <li role="none">        <div class="tree-item" role="treeitem" aria-checked="false">          <span class="tree-check" aria-hidden="true"></span>tree.css        </div>      </li>    </ul>  </li>  <li role="none">    <div class="tree-item tree-toggle" role="treeitem" aria-expanded="false" aria-checked="false">      <ChevronRightIcon class="icon tree-chevron" /><span class="tree-check" aria-hidden="true"></span>themes    </div>    <ul class="tree-sub" role="group">      <li role="none">        <div class="tree-item" role="treeitem" aria-checked="false">          <span class="tree-check" aria-hidden="true"></span>paper.css        </div>      </li>    </ul>  </li>  <li role="none">    <div class="tree-item" role="treeitem" aria-checked="true">      <span class="tree-check" aria-hidden="true"></span>README.md    </div>  </li></ul>
<script setup>import { onMounted, onUnmounted, ref } from 'vue'import { attach } from '@master/ui/behaviors'import ChevronRightIcon from '@master/icons-vue/native/16/soft/chevron-right'const root = ref(null)let detachonMounted(() => { detach = attach(root.value) })onUnmounted(() => detach?.())</script><template>  <ul ref="root" class="tree max-w(260) mx-auto" role="tree" aria-label="Files to publish" aria-multiselectable="true">    <li role="none">      <div class="tree-item tree-toggle" role="treeitem" aria-expanded="true" aria-checked="mixed">        <ChevronRightIcon class="icon tree-chevron" /><span class="tree-check" aria-hidden="true"></span>components      </div>      <ul class="tree-sub" role="group">        <li role="none">          <div class="tree-item" role="treeitem" aria-checked="true">            <span class="tree-check" aria-hidden="true"></span>button.css          </div>        </li>        <li role="none">          <div class="tree-item" role="treeitem" aria-checked="true">            <span class="tree-check" aria-hidden="true"></span>input.css          </div>        </li>        <li role="none">          <div class="tree-item" role="treeitem" aria-checked="false">            <span class="tree-check" aria-hidden="true"></span>theme.css          </div>        </li>        <li role="none">          <div class="tree-item" role="treeitem" aria-checked="false">            <span class="tree-check" aria-hidden="true"></span>tree.css          </div>        </li>      </ul>    </li>    <li role="none">      <div class="tree-item tree-toggle" role="treeitem" aria-expanded="false" aria-checked="false">        <ChevronRightIcon class="icon tree-chevron" /><span class="tree-check" aria-hidden="true"></span>themes      </div>      <ul class="tree-sub" role="group">        <li role="none">          <div class="tree-item" role="treeitem" aria-checked="false">            <span class="tree-check" aria-hidden="true"></span>paper.css          </div>        </li>      </ul>    </li>    <li role="none">      <div class="tree-item" role="treeitem" aria-checked="true">        <span class="tree-check" aria-hidden="true"></span>README.md      </div>    </li>  </ul></template>
勾選 themes:其中的 paper.css 也會被勾選。取消勾選 components 中的一個檔案,該分支就會變成部分勾選。
  • Space 會勾選或取消勾選取得焦點的列;若是分支,則連同其下所有列一起變更。部分勾選的分支會被勾選;若其下所有可變更的列都已勾選,則改為取消勾選。
  • 點擊末端項目會勾選它;點擊分支時,點擊勾選框會勾選分支,點擊該列其他位置則會展開或收合。Enter 的作用與點擊該列相同。
  • 停用的列在其分支被勾選時會維持原本的勾選狀態,因此分支可能仍為部分勾選。
  • 值是已勾選的末端項目組成的清單,依樹狀檢視中的順序排列。分支不包含在內:分支的勾選狀態只是反映其下各列,因此值會列出每個檔案,而不會列出資料夾。尚未載入列的分支也不包含在內;當它的列在已勾選的分支下送達時,這些列會被勾選,樹狀檢視也會依序派發 input 和 change。

也請在樹狀檢視上設定 aria-multiselectable="true":核取方塊樹狀檢視可以選擇任意數量的列,也應該被這樣播報。

#稍後載入列

列來自伺服器的分支,是一個帶有 aria-expanded="false" 和空白 tree-sub 的列。當它展開而其下沒有任何列時,行為會將它標記為 aria-busy="true" 並派發 expand。請在該列最前面放一個旋轉指示器:平時不會繪製,該列忙碌時則會取代箭頭的位置,因此版面不會移動。

稍後載入列

<ul class="tree max-w(260) mx-auto" role="tree" aria-label="Repositories" id="repositories">  <li role="none">    <div class="tree-item tree-toggle" role="treeitem" aria-expanded="false">      <svg class="icon tree-chevron" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">        <path d="M6 3.5L10.3586 7.8586A0.2 0.2 0 0 1 10.3586 8.1414L6 12.5" />      </svg><span class="spinner" aria-hidden="true"></span>master-co/ui    </div>    <ul class="tree-sub" role="group"></ul>  </li>  <li role="none">    <div class="tree-item tree-toggle" role="treeitem" aria-expanded="false">      <svg class="icon tree-chevron" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">        <path d="M6 3.5L10.3586 7.8586A0.2 0.2 0 0 1 10.3586 8.1414L6 12.5" />      </svg><span class="spinner" aria-hidden="true"></span>master-co/css    </div>    <ul class="tree-sub" role="group"></ul>  </li></ul><script>addEventListener('DOMContentLoaded', function () {document.getElementById('repositories').addEventListener('expand', function (event) {  var row = event.detail.item  if (row.getAttribute('aria-busy') !== 'true') return  // A page fetches the rows here; this one waits a moment instead.  setTimeout(function () {    row.nextElementSibling.innerHTML = ['packages', 'site', 'README.md'].map(function (name) {      return '<li role="none"><div class="tree-item" role="treeitem">' + name + '</div></li>'    }).join('')  }, 1200)})})</script><script type="module">  import { attach } from '@master/ui/behaviors'  attach()</script>
展開儲存庫後,載入指示器會持續旋轉,直到子項目載入完成。接著即可用方向鍵移到這些子項目。

頁面監聽 expand,並依照契約的類別與角色,把列寫入分支的 tree-sub。行為會察覺列已送達:它會移除 aria-busy,並立即讓這些列支援按鍵操作,不列入 Tab 順序,以方向鍵瀏覽。行為展開的每個分支都會派發 expand,因此請檢查 aria-busy 來判斷是否需要載入列。如果分支最後其實沒有任何列,請移除它的 aria-busy 和 aria-expanded:它是末端項目。

app.js
root.addEventListener('expand', async (event) => {  const row = event.detail.item  if (row.getAttribute('aria-busy') !== 'true') return  const children = await fetch(`/api/folders/${event.detail.value}`).then((response) => response.json())  row.nextElementSibling.replaceChildren(...children.map(rowFor))})

#重新命名

設定 rename: true 後,在列上按 F2 會把它的標籤原地變成行內編輯欄位,並選取其中的文字。標籤必須是 tree-label,它也會讓過長的名稱以省略號結尾。Enter 會儲存,Escape 會還原標籤;兩種情況下焦點都會回到該列。離開欄位時會儲存,空白的標籤則不會儲存。

重新命名

<ul class="tree max-w(260) mx-auto" role="tree" aria-label="Pages" id="pages">  <li role="none">    <div class="tree-item tree-toggle" role="treeitem" aria-expanded="true">      <svg class="icon tree-chevron" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">        <path d="M6 3.5L10.3586 7.8586A0.2 0.2 0 0 1 10.3586 8.1414L6 12.5" />      </svg><span class="tree-label">Guide</span>    </div>    <ul class="tree-sub" role="group">      <li role="none">        <div class="tree-item" role="treeitem" data-value="introduction">          <span class="tree-label">Introduction</span>        </div>      </li>      <li role="none">        <div class="tree-item" role="treeitem" data-value="installation">          <span class="tree-label">Installation</span>        </div>      </li>    </ul>  </li>  <li role="none">    <div class="tree-item" role="treeitem" data-value="changelog">      <span class="tree-label">Changelog</span>    </div>  </li></ul><script>addEventListener('DOMContentLoaded', function () {MasterUI.tree(document.getElementById('pages'), { rename: true })})</script><script type="module">  import { attach } from '@master/ui/behaviors'  attach()</script>
import { useEffect, useRef } from 'react'import { attach, tree } from '@master/ui/behaviors'import ChevronRightIcon from '@master/icons-react/native/16/soft/chevron-right'export function Example() {  const root = useRef<HTMLUListElement>(null)  useEffect(() => {    const detach = [      attach(root.current!),      tree(document.getElementById('pages')!, { rename: true }),    ]    return () => detach.forEach(stop => stop())  }, [])  return (    <ul ref={root} className="tree max-w(260) mx-auto" role="tree" aria-label="Pages" id="pages">      <li role="none">        <div className="tree-item tree-toggle" role="treeitem" aria-expanded="true">          <ChevronRightIcon className="icon tree-chevron" /><span className="tree-label">Guide</span>        </div>        <ul className="tree-sub" role="group">          <li role="none">            <div className="tree-item" role="treeitem" data-value="introduction">              <span className="tree-label">Introduction</span>            </div>          </li>          <li role="none">            <div className="tree-item" role="treeitem" data-value="installation">              <span className="tree-label">Installation</span>            </div>          </li>        </ul>      </li>      <li role="none">        <div className="tree-item" role="treeitem" data-value="changelog">          <span className="tree-label">Changelog</span>        </div>      </li>    </ul>  )}
<script>  import { attach, tree } from '@master/ui/behaviors'  import ChevronRightIcon from '@master/icons-svelte/native/16/soft/chevron-right'  function mounted(root) {    const detach = [      attach(root),      tree(document.getElementById('pages'), { rename: true }),    ]    return () => detach.forEach(stop => stop())  }</script><ul class="tree max-w(260) mx-auto" role="tree" aria-label="Pages" id="pages" {@attach mounted}>  <li role="none">    <div class="tree-item tree-toggle" role="treeitem" aria-expanded="true">      <ChevronRightIcon class="icon tree-chevron" /><span class="tree-label">Guide</span>    </div>    <ul class="tree-sub" role="group">      <li role="none">        <div class="tree-item" role="treeitem" data-value="introduction">          <span class="tree-label">Introduction</span>        </div>      </li>      <li role="none">        <div class="tree-item" role="treeitem" data-value="installation">          <span class="tree-label">Installation</span>        </div>      </li>    </ul>  </li>  <li role="none">    <div class="tree-item" role="treeitem" data-value="changelog">      <span class="tree-label">Changelog</span>    </div>  </li></ul>
<script setup>import { onMounted, onUnmounted, ref } from 'vue'import { attach, tree } from '@master/ui/behaviors'import ChevronRightIcon from '@master/icons-vue/native/16/soft/chevron-right'const root = ref(null)let detach = []onMounted(() => {  detach = [    attach(root.value),    tree(document.getElementById('pages'), { rename: true }),  ]})onUnmounted(() => detach.forEach(stop => stop()))</script><template>  <ul ref="root" class="tree max-w(260) mx-auto" role="tree" aria-label="Pages" id="pages">    <li role="none">      <div class="tree-item tree-toggle" role="treeitem" aria-expanded="true">        <ChevronRightIcon class="icon tree-chevron" /><span class="tree-label">Guide</span>      </div>      <ul class="tree-sub" role="group">        <li role="none">          <div class="tree-item" role="treeitem" data-value="introduction">            <span class="tree-label">Introduction</span>          </div>        </li>        <li role="none">          <div class="tree-item" role="treeitem" data-value="installation">            <span class="tree-label">Installation</span>          </div>        </li>      </ul>    </li>    <li role="none">      <div class="tree-item" role="treeitem" data-value="changelog">        <span class="tree-label">Changelog</span>      </div>    </li>  </ul></template>
將焦點移到某一列,按 F2 並輸入名稱,再按 Enter 儲存或按 Escape 還原。

attach() 不會傳入任何選項,因此這個預覽會在 attach() 執行後自行呼叫 tree;對同一個根元素再次呼叫會取代前一次。

行為會把新標籤寫入 tree-label,並從根元素派發 rename,在 detail.value 中提供該列重新命名前所代表的值,在 detail.label 中提供新標籤;欄位本身的 input 和 change 不會傳出欄位。儲存標籤由頁面負責:如果伺服器拒絕,請把舊標籤寫回去。請為每列設定不會隨名稱改變的 data-value,讓它所代表的值在重新命名後仍然有效。

沒有 F2 的讀者(例如使用觸控螢幕)需要其他入口:無論有沒有設定這個選項,對 tree 傳回的函式呼叫 edit(row) 都能達到與 F2 相同的效果,適合用於快顯選單中的「重新命名」項目或按鈕。

app.js
const pages = tree(root, { rename: true })root.addEventListener('rename', async (event) => {  const { item, value, label } = event.detail  const saved = await fetch(`/api/pages/${value}`, { method: 'PATCH', body: JSON.stringify({ title: label }) })  if (!saved.ok) item.querySelector('.tree-label').textContent = previous(value)})renameItem.addEventListener('click', () => pages.edit(selectedRow))

#樹狀檢視或導覽

導覽清單 是一份簡短的目的地清單,只有一層分組,目前所在列會以帶有層次感的面板樣式突顯。樹狀檢視適合呈現多層結構,列與列之間排列緊湊,每個分支前方都有箭頭。

#鍵盤操作

按鍵動作
Tab使用 details 時,每列都是 Tab 停駐點。使用腳本時,整棵樹只有一個 Tab 停駐點,落在目前列或選取列,兩者皆無時則落在第一列
Enter, Space展開或收合分支,或前往該列的連結。操作 summary 時,由瀏覽器直接處理,無須腳本。啟用腳本後,操作設有 role="treeitem" 的葉節點列會選取該列;在核取方塊樹狀檢視中,Enter 會勾選葉節點
ArrowDown, ArrowUp使用腳本時,移至下一個或上一個可見列
ArrowRight使用腳本時,展開已收合的分支。若分支已展開,則移至其第一個子列。介面方向為從右至左時,改用 ArrowLeft。
ArrowLeft使用腳本時,收合已展開的分支。若焦點位於已收合的分支或葉節點,則移至其父節點所在列。介面方向為從右至左時,改用 ArrowRight。
Home, End使用腳本時,移至第一個或最後一個可見列
A character使用腳本時,移至下一個以輸入字元開頭的可見列。快速連續輸入的字元會合併為同一個搜尋詞
*使用腳本時,展開與焦點列同一層級的所有已收合分支
Space啟用腳本且樹設有 aria-multiselectable="true" 時:選取或取消選取取得焦點的列。在核取方塊樹狀檢視中:勾選或取消勾選該列及其下所有列
Control+click在可多選的樹狀檢視中:選取或取消選取單一列,同時保留其他列的選取狀態;單獨點擊則只選取該列。在 Mac 上使用 Command
Shift+ArrowDown, Shift+ArrowUp在可多選的樹狀檢視中:移動焦點,並選取從最後選取的列到新列之間所有可見的列
Shift+Space, Shift+click在可多選的樹狀檢視中:選取從最後選取的列到此列之間所有可見的列
Control+Shift+Home, Control+Shift+End在可多選的樹狀檢視中:選取從目前位置到第一列或最後一列之間所有可見的列。在 Mac 上使用 Command
Control+A在可多選的樹狀檢視中:選取所有可見的列;若已全部選取,則全部取消選取。在 Mac 上使用 Command
F2使用腳本的 rename 選項時:將取得焦點的列的標籤轉為欄位,並選取其中的文字
Enter, Escape在重新命名欄位中:儲存標籤,或還原原本的標籤;兩種情況焦點都會回到該列。離開欄位時會儲存

#無障礙

  • 使用 <details> 時,請勿加上樹狀檢視角色。輔助技術已能正確讀出原生展開與收合控制項。未套用行為時,每列都是 Tab 停駐點。
  • 使用 aria-current="page" 標記目前所在列。在樹狀檢視模式中則使用 aria-selected="true"。
  • 多列選取、勾選、載入和重新命名都屬於樹狀檢視模式,需要 role="tree" 和行為。
  • 在核取方塊樹狀檢視中,由列標示 aria-checked;它的 tree-check 設為 aria-hidden="true",因此狀態只會播報一次。
  • 忙碌中的分支以 aria-busy="true" 表示;它的旋轉指示器設為 aria-hidden="true"。
  • 重新命名欄位的名稱為 Rename <label>;請以頁面的語言傳入 renameLabel。
  • 在觸控螢幕上,每列維持原本的高度,連接各層的線條也保有原來的節奏。

#React、Svelte 和 Vue

tree 元件會依資料呈現各列、附加行為,並支援傳入及傳出已選取的列。執行 npx master-ui add tree 即可將元件複製到專案中。元件的實作與用法請見 React、Svelte 和 Vue。

  • multiple 可選取任意數量的列,checkable 則讓每列都有勾選框;兩者任一設定時,值都是清單。初始值會在第一次算繪時就畫出來,包括分支的部分勾選狀態。
  • 帶有 children: [] 的節點是稍後才載入列的分支:它會顯示旋轉指示器,展開時會以它的值呼叫 onExpand(Vue 中為 @expand)。請在 nodes 中提供它的子節點。
  • onRename(Vue 中為 @rename)會開啟重新命名功能,並以該列的值和新標籤呼叫。請在 nodes 中保存標籤。
ui/tree.tsx
'use client'import { useState, type ComponentProps, type ReactNode } from 'react'import { tree } from '@master/ui/behaviors/tree.js'import { useBehavior } from './use-behavior'export interface TreeNode {  label: ReactNode  /**   * What the row stands for: what selecting or checking a leaf gives `onValueChange`, and what `onExpand`   * and `onRename` are called with. A row with neither `href` nor `children` can be selected.   */  value?: string  /** A row that is a link. `current` marks the page that is open. */  href?: string  current?: boolean  /** The rows under a branch, and whether it starts open. `[]` is a branch whose rows are loaded when it opens. */  children?: TreeNode[]  open?: boolean  disabled?: boolean}interface Shared extends Omit<ComponentProps<'ul'>, 'defaultValue' | 'children' | 'role' | 'ref'> {  nodes: TreeNode[]  /**   * Called with a branch's value each time it opens. A branch with `children: []` is busy until its rows   * arrive: give it its children in `nodes`.   */  onExpand?: (value: string) => void  /** Makes F2 rename the row in focus; called with the row's value and its new label. Keep the label in `nodes`. */  onRename?: (value: string, label: string) => void  /** The accessible name of the rename field. "Rename" and the label by default. */  renameLabel?: (label: string) => string}interface One extends Shared {  multiple?: false  checkable?: false  /** The value of the selected row, null for none. */  value?: string | null  defaultValue?: string | null  onValueChange?: (value: string | null) => void}interface Several extends Shared {  /** Any number of rows are selected. */  multiple: true  checkable?: false  /** The values of the selected rows, in the order of the tree. */  value?: string[]  defaultValue?: string[]  onValueChange?: (value: string[]) => void}interface Checks extends Shared {  /** Every row has a check; checking a branch checks the rows under it. */  checkable: true  multiple?: false  /** The values of the checked leaves, in the order of the tree. A branch is not in it: its check says its rows'. */  value?: string[]  defaultValue?: string[]  onValueChange?: (value: string[]) => void}export type TreeProps = One | Several | Checks/** A tree walked and folded with the arrow keys. Name it with `aria-label` or `aria-labelledby`. */export function Tree({ nodes, value, defaultValue, onValueChange, multiple, checkable, onExpand, onRename, renameLabel, className, ...root }: TreeProps) {  const ref = useBehavior(tree, { rename: Boolean(onRename), renameLabel }, {    value,    defaultValue,    onValueChange,    on: {      expand: (event: Event) => onExpand?.((event as CustomEvent<{ value: string }>).detail.value),      rename: (event: Event) => {        const { value, label } = (event as CustomEvent<{ value: string, label: string }>).detail        onRename?.(value, label)      },    },  } as never)  // The first value's rows are selected or checked in the first render; after that the behavior writes them.  const [first] = useState(value ?? defaultValue)  const chosen = [first ?? []].flat() as string[]  const has = (node: TreeNode) => node.value !== undefined && chosen.includes(node.value)  /** A branch says what the rows under it say: all checked, none, or mixed. */  const checked = (node: TreeNode): 'true' | 'false' | 'mixed' => {    if (!node.children?.length) return has(node) ? 'true' : 'false'    const states = node.children.map(checked)    return states.every(state => state === 'true') ? 'true' : states.every(state => state === 'false') ? 'false' : 'mixed'  }  const inside = (node: TreeNode) => (    <>      {node.children?.length === 0 && <span className="spinner" aria-hidden="true" />}      {checkable && <span className="tree-check" aria-hidden="true" />}      <span className="tree-label">{node.label}</span>    </>  )  // A row is the node it stands for, not its place: a branch the reader opened stays open when a row is written above it.  const rows = (list: TreeNode[]): ReactNode => list.map((node, index) => (    <li role="none" key={node.value ?? index}>      {node.children        ? (            <>              <div className="tree-item tree-toggle" role="treeitem" data-value={node.value} aria-expanded={node.open ?? false} aria-checked={checkable ? checked(node) : undefined} aria-disabled={node.disabled || undefined}><svg className="icon tree-chevron" viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="1" strokeLinecap="round" strokeLinejoin="round" aria-hidden="true"><path d="M6 3.5L10.3586 7.8586A0.2 0.2 0 0 1 10.3586 8.1414L6 12.5" /></svg>{inside(node)}</div>              <ul className="tree-sub" role="group">{rows(node.children)}</ul>            </>          )        : node.href          ? <a className="tree-item" role="treeitem" href={node.href} aria-current={node.current ? 'page' : undefined} aria-checked={checkable ? checked(node) : undefined} aria-disabled={node.disabled || undefined}>{inside(node)}</a>          : <div className="tree-item" role="treeitem" data-value={node.value} aria-selected={checkable ? undefined : multiple ? has(node) : (node.value !== undefined && node.value === first) || undefined} aria-checked={checkable ? checked(node) : undefined} aria-disabled={node.disabled || undefined}>{inside(node)}</div>}    </li>  ))  return <ul className={className ? `tree ${className}` : 'tree'} role="tree" aria-multiselectable={multiple || checkable || undefined} ref={ref} {...root}>{rows(nodes)}</ul>}
ui/Tree.svelte
<script lang="ts" module>  import type { HTMLAttributes } from 'svelte/elements'  export interface TreeNode {    label: string    /**     * What the row stands for: what selecting or checking a leaf gives `onValueChange`, and what `onExpand`     * and `onRename` are called with. A row with neither `href` nor `children` can be selected.     */    value?: string    /** A row that is a link. `current` marks the page that is open. */    href?: string    current?: boolean    /** The rows under a branch, and whether it starts open. `[]` is a branch whose rows are loaded when it opens. */    children?: TreeNode[]    open?: boolean    disabled?: boolean  }  export interface TreeProps extends Omit<HTMLAttributes<HTMLUListElement>, 'role' | 'children'> {    nodes: TreeNode[]    /** Any number of rows are selected; the value is then the list of them. */    multiple?: boolean    /** Every row has a check, and checking a branch checks the rows under it; the value is the list of checked leaves. */    checkable?: boolean    /**     * The value of the selected row, null for none. With `multiple`, the selected rows; with `checkable`, the     * checked leaves, branches left out; both in the order of the tree. Bind it, or leave it out.     */    value?: string | string[] | null    onValueChange?: (value: string | string[] | null) => void    /**     * Called with a branch's value each time it opens. A branch with `children: []` is busy until its rows     * arrive: give it its children in `nodes`.     */    onExpand?: (value: string) => void    /** Makes F2 rename the row in focus; called with the row's value and its new label. Keep the label in `nodes`. */    onRename?: (value: string, label: string) => void    /** The accessible name of the rename field. "Rename" and the label by default. */    renameLabel?: (label: string) => string  }</script><script lang="ts">  import { untrack } from 'svelte'  import { tree } from '@master/ui/behaviors/tree.js'  import { behavior } from './behavior.svelte.js'  let { nodes, multiple, checkable, value = $bindable(), onValueChange, onExpand, onRename, renameLabel, class: className, ...root }: TreeProps = $props()  // The first value's rows are selected or checked in the first render; after that the behavior writes them. A  // row written later, from new nodes, is drawn with the value as it is then, which is not read as state.  const chosen = { values: [untrack(() => value) ?? []].flat() as string[] }  const has = (node: TreeNode) => node.value !== undefined && chosen.values.includes(node.value)  /** A branch says what the rows under it say: all checked, none, or mixed. */  const checked = (node: TreeNode): 'true' | 'false' | 'mixed' => {    if (!node.children?.length) return has(node) ? 'true' : 'false'    const states = node.children.map(checked)    return states.every(state => state === 'true') ? 'true' : states.every(state => state === 'false') ? 'false' : 'mixed'  }  const attach = behavior(tree, () => ({ rename: Boolean(onRename), renameLabel }), {    value: () => value,    change: (next) => { chosen.values = [next ?? []].flat(); value = next; onValueChange?.(next) },    settled: (held) => { chosen.values = [held ?? []].flat() },    on: {      expand: (event) => onExpand?.((event as CustomEvent<{ value: string }>).detail.value),      rename: (event) => {        const { value, label } = (event as CustomEvent<{ value: string, label: string }>).detail        onRename?.(value, label)      },    },  })</script>{#snippet inside(node: TreeNode)}  {#if node.children?.length === 0}<span class="spinner" aria-hidden="true"></span>{/if}  {#if checkable}<span class="tree-check" aria-hidden="true"></span>{/if}  <span class="tree-label">{node.label}</span>{/snippet}{#snippet rows(list: TreeNode[])}  <!-- svelte-ignore a11y_role_has_required_aria_props -->  <!-- A row is the node it stands for, not its place: a branch the reader opened stays open when a row is written above it. -->  {#each list as node, index (node.value ?? index)}    <li role="none">      {#if node.children}        <div class="tree-item tree-toggle" role="treeitem" data-value={node.value} aria-expanded={node.open ?? false} aria-checked={checkable ? checked(node) : undefined} aria-disabled={node.disabled || undefined}><svg class="icon tree-chevron" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M6 3.5L10.3586 7.8586A0.2 0.2 0 0 1 10.3586 8.1414L6 12.5" /></svg>{@render inside(node)}</div>        <ul class="tree-sub" role="group">{@render rows(node.children)}</ul>      {:else if node.href}        <a class="tree-item" role="treeitem" href={node.href} aria-current={node.current ? 'page' : undefined} aria-checked={checkable ? checked(node) : undefined} aria-disabled={node.disabled || undefined}>{@render inside(node)}</a>      {:else}        <div class="tree-item" role="treeitem" data-value={node.value} aria-selected={checkable ? undefined : multiple ? has(node) : has(node) || undefined} aria-checked={checkable ? checked(node) : undefined} aria-disabled={node.disabled || undefined}>{@render inside(node)}</div>      {/if}    </li>  {/each}{/snippet}<!--  @component  A tree walked and folded with the arrow keys. Name it with `aria-label` or `aria-labelledby`.--><ul class={['tree', className]} role="tree" aria-multiselectable={multiple || checkable || undefined} {@attach attach} {...root}>{@render rows(nodes)}</ul>
ui/Tree.vue
<script lang="ts">export interface TreeNode {  label: string  /**   * What the row stands for: what selecting or checking a leaf gives the model, and what `expand` and   * `rename` say. A row with neither `href` nor `children` can be selected.   */  value?: string  /** A row that is a link. `current` marks the page that is open. */  href?: string  current?: boolean  /** The rows under a branch, and whether it starts open. `[]` is a branch whose rows are loaded when it opens. */  children?: TreeNode[]  open?: boolean  disabled?: boolean}</script><script setup lang="ts">import { h, useTemplateRef, type FunctionalComponent, type VNode } from 'vue'import { tree } from '@master/ui/behaviors/tree.js'import { useBehavior } from './use-behavior'defineOptions({ inheritAttrs: false })const { multiple, checkable, onRename, renameLabel } = defineProps<{  nodes: TreeNode[]  /** Any number of rows are selected; the model is then the list of them. */  multiple?: boolean  /** Every row has a check, and checking a branch checks the rows under it; the model is the list of checked leaves. */  checkable?: boolean  /**   * `@rename`: makes F2 rename the row in focus, and hears the row's value and its new label. Keep the label   * in `nodes`. It is a prop, not an emit, so the tree offers renaming only when it is listened to.   */  onRename?: (value: string, label: string) => void  /** The accessible name of the rename field. "Rename" and the label by default. */  renameLabel?: (label: string) => string}>()/** * The value of the selected row, null for none. With `multiple`, the selected rows; with `checkable`, the * checked leaves, branches left out; both in the order of the tree: `v-model`. */const model = defineModel<string | string[] | null>()const emit = defineEmits<{  /** A branch opened. One with `children: []` is busy until its rows arrive: give it its children in `nodes`. */  expand: [value: string]}>()/* A tree walked and folded with the arrow keys. Name it with `aria-label` or `aria-labelledby`. */const root = useTemplateRef<HTMLUListElement>('root')useBehavior(root, tree, () => ({ rename: Boolean(onRename), renameLabel }), model, {  expand: (event) => emit('expand', (event as CustomEvent<{ value: string }>).detail.value),  rename: (event) => {    const { value, label } = (event as CustomEvent<{ value: string, label: string }>).detail    onRename?.(value, label)  },})/** The first value's rows are selected or checked in the first render; after that the behavior writes them. */const first = model.valueconst chosen = [first ?? []].flat() as string[]const has = (node: TreeNode) => node.value !== undefined && chosen.includes(node.value)/** A branch says what the rows under it say: all checked, none, or mixed. */const checked = (node: TreeNode): 'true' | 'false' | 'mixed' => {  if (!node.children?.length) return has(node) ? 'true' : 'false'  const states = node.children.map(checked)  return states.every(state => state === 'true') ? 'true' : states.every(state => state === 'false') ? 'false' : 'mixed'}/** The chevron a branch's row holds in its column: Master Icons' chevron-right at 16, turned by the stylesheet when the branch is open. */const chevron = () => h('svg', { 'class': 'icon tree-chevron', 'viewBox': '0 0 16 16', 'fill': 'none', 'stroke': 'currentColor', 'stroke-width': '1', 'stroke-linecap': 'round', 'stroke-linejoin': 'round', 'aria-hidden': 'true' }, [h('path', { d: 'M6 3.5L10.3586 7.8586A0.2 0.2 0 0 1 10.3586 8.1414L6 12.5' })])/** What a row holds: a spinner while its rows are on their way, its check, and its label. */const inside = (node: TreeNode): VNode[] => [  ...(node.children?.length === 0 ? [h('span', { 'class': 'spinner', 'aria-hidden': 'true' })] : []),  ...(checkable ? [h('span', { 'class': 'tree-check', 'aria-hidden': 'true' })] : []),  h('span', { class: 'tree-label' }, node.label),]/** The rows of one level, and under each branch the rows of the next. */const Rows: FunctionalComponent<{ nodes: TreeNode[] }> = ({ nodes }) => nodes.map((node, index) => {  const state = { 'aria-checked': checkable ? checked(node) : undefined, 'aria-disabled': node.disabled || undefined }  // A row is the node it stands for, not its place: a branch the reader opened stays open when a row is written above it.  return h('li', { role: 'none', key: node.value ?? index }, node.children    ? [        h('div', { 'class': 'tree-item tree-toggle', 'role': 'treeitem', 'data-value': node.value, 'aria-expanded': node.open ?? false, ...state }, [chevron(), ...inside(node)]),        h('ul', { class: 'tree-sub', role: 'group' }, h(Rows, { nodes: node.children })),      ]    : node.href      ? h('a', { 'class': 'tree-item', 'role': 'treeitem', 'href': node.href, 'aria-current': node.current ? 'page' : undefined, ...state }, inside(node))      : h('div', { 'class': 'tree-item', 'role': 'treeitem', 'data-value': node.value, 'aria-selected': checkable ? undefined : multiple ? has(node) : (node.value !== undefined && node.value === first) || undefined, ...state }, inside(node)))})Rows.props = ['nodes']</script><template>  <ul class="tree" role="tree" :aria-multiselectable="multiple || checkable || undefined" ref="root" v-bind="$attrs"><Rows :nodes="nodes" /></ul></template>

#類別

類別說明
tree樹狀檢視的清單。由腳本控制時,請設定 role="tree" 並提供標籤。
tree-item一列可使用連結、按鈕、分支的 summary,或設有 role="treeitem" 的元素。保留箭頭圖示的欄位,讓末端項目與同層分支對齊。
tree-label列的文字,過長時以刪節號結尾。腳本的 rename 會將這部分轉為行內編輯元件
tree-check在核取方塊樹狀檢視中,列的核取方塊位於箭頭欄之後,並依該列的 aria-checked 顯示狀態。請為核取方塊設定 aria-hidden="true",由該列本身提供狀態資訊。
tree-toggle用於分支所在的列,在起始欄放置分支的箭頭。分支展開時,箭頭轉向下方。搭配 tree-item 使用
tree-chevron分支列起始欄中的箭頭圖示,分支展開時轉向下方
tree-group使用 details 容納樹狀分支,summary 是分支本身的列,tree-sub 則包含下層的列
tree-sub分支下方的列縮排一層,沿著從箭頭圖示向下延伸的細線排列。由腳本控制樹狀檢視時,請設定 role="group"。

#自訂屬性

屬性預設值說明
--tree-item-height1.75rem列高,所有指標類型皆相同
--tree-indent1rem每一層的縮排距離

#原始碼

@master/ui/components/tree.css
/* * Tree: nested rows for things that contain things: folders, outlines, categories. * Without script a branch is a native <details> whose <summary> is the row, so it folds and unfolds with the * keys the browser already gives it. With a script that provides the tree pattern, the same classes read * aria-expanded instead. Rows are dense and quiet; the one under the pointer takes a chip tone, the current one * rises as a small sheet, and each level is set in on a hairline guide. * * The tree pattern can say more, and the stylesheet draws it from the same attributes: a tree with * aria-multiselectable="true" marks each selected row as it marks one; a checkbox tree gives each row a * tree-check, the checkbox's own well drawn from the row's aria-checked; a branch whose rows are on their way * says aria-busy="true", and a spinner it holds takes the chevron's place; and a label being renamed is an * editable in place, its text where the label's was. */@layer components.ui {  .tree {    --tree-item-height: 1.75rem;    --tree-indent: 1rem;    --icon-size: 1rem;    display: flex;    flex-direction: column;    gap: 1px;    min-width: 0;    margin: 0;    padding: 0;    color: var(--color-text-body);    font: 400 .8125rem/1.2 var(--font-family-sans);    font-feature-settings: inherit;    list-style: none;    /* The script listens on the whole tree: a tap on a row must not flash the tree grey on iOS. */    -webkit-tap-highlight-color: transparent;  }  /* A row: a link, a button, the <summary> of a branch, or an element with role="treeitem". */  .tree-item {    --icon-offset: -.0625rem;    --icon-color: var(--color-text-muted);    display: flex;    align-items: center;    gap: .375rem;    min-width: 0;    min-height: var(--tree-item-height);    margin: 0;    padding-block: 0;    padding-inline: .25rem .5rem;    border: 0;    border-radius: calc(var(--radius-chip) * var(--corner-scale));    corner-shape: var(--corner-shape);    outline: none;    background: transparent;    color: inherit;    font: inherit;    font-feature-settings: inherit;    text-align: start;    text-decoration: none;    white-space: nowrap;    list-style: none;    appearance: none;    user-select: none;    -webkit-user-select: none;    -webkit-tap-highlight-color: transparent;    cursor: pointer;    touch-action: manipulation;    &::-webkit-details-marker {      display: none;    }    /* Every row keeps the chevron's column, so a leaf lines up with the branches beside it. */    &::before {      content: '';      flex: none;      width: .875rem;      height: .875rem;    }    /* The icon takes the row's ink under the pointer and the press, as a nav's and a menu's rows do. */    @media (hover: hover) {      &:hover:not([aria-current="page"], [aria-current="true"], [aria-selected="true"], [aria-disabled="true"]) {        --icon-color: currentColor;        background: var(--color-surface-chip);        color: var(--color-text-strong);      }    }    /* Pressed: a step deeper than the pointer's tone. */    &:active:not([aria-current="page"], [aria-current="true"], [aria-selected="true"], [aria-disabled="true"]) {      --icon-color: currentColor;      background: var(--color-surface-inset-hover);      color: var(--color-text-strong);    }    /* Current or selected: the row rises one step as a small sheet, as a nav's current row does. */    &:is([aria-current="page"], [aria-current="true"], [aria-selected="true"]) {      --icon-color: currentColor;      @apply --surface-sheet;      color: var(--color-text-strong);      box-shadow: var(--shadow-raised);    }    &:focus-visible {      outline: 2px solid var(--color-focus);      outline-offset: -2px;    }    &[aria-disabled="true"] {      --icon-color: currentColor;      color: var(--color-text-disabled);      cursor: not-allowed;    }    @media (forced-colors: active) {      &:is([aria-current="page"], [aria-current="true"], [aria-selected="true"]) {        outline: 2px solid Highlight;        outline-offset: -2px;      }      /* Written after the current row's outline, of equal weight, so the row the keys are on still says so. */      &:focus-visible {        outline-color: CanvasText;      }      &[aria-disabled="true"] {        color: GrayText;      }    }  }  /* The row of a branch: the chevron it holds stands in the column, and turns down when the branch is open. */  .tree-toggle::before {    content: none;  }  .tree-toggle .tree-chevron {    flex: none;    width: .875rem;    height: .875rem;    /* The column is the chevron's own: it takes none of the offset a row gives its icon. */    margin: 0;    color: var(--color-text-muted);    transition: rotate var(--duration-quick) var(--easing-glide);    @media (prefers-reduced-motion: reduce) {      transition: none;    }    @media (forced-colors: active) {      color: CanvasText;    }  }  /* From right to left the chevron is mirrored, so it turns the other way to point down. */  .tree-toggle:dir(rtl) .tree-chevron {    --tree-turn: -90deg;    scale: -1 1;  }  /* Open: by its <details>, or by aria-expanded. A closed branch inside an open one keeps its own chevron. */  .tree-group[open] .tree-toggle .tree-chevron, .tree-toggle[aria-expanded="true"] .tree-chevron {    rotate: var(--tree-turn, 90deg);  }  .tree-group[open] .tree-group:not([open]) .tree-toggle .tree-chevron {    rotate: none;  }  /* A <details> that holds a branch. */  .tree-group {    display: flex;    flex-direction: column;    gap: 1px;    min-width: 0;  }  /* The rows under a branch, one level in, on a hairline that runs down from the chevron. */  .tree-sub {    display: flex;    flex-direction: column;    gap: 1px;    margin: 0;    margin-inline-start: calc(.25rem + .4375rem - .5px);    padding: 0;    padding-inline-start: calc(var(--tree-indent) - .25rem - .4375rem - .5px);    border-inline-start: 1px solid var(--color-line-hair);    list-style: none;  }  /* Driven by script: a row that says it is collapsed hides the rows that follow it. */  .tree-item[aria-expanded="false"] + .tree-sub {    display: none;  }  /* The words of a row: a long one ends in an ellipsis instead of widening the tree. */  .tree-label {    min-width: 0;    overflow: hidden;    text-overflow: ellipsis;  }  /*   * A row's check, in a checkbox tree: the checkbox's well at a row's scale, empty, checked or mixed as the   * row's aria-checked says. It stands after the chevron's column, so checks line up level by level.   */  .tree-check {    display: inline-grid;    flex: none;    place-items: center;    width: 1rem;    height: 1rem;    border-radius: calc((var(--radius-chip) - 1px) * var(--corner-scale));    corner-shape: var(--corner-shape);    @apply --check-well;    transition-property: background-color, box-shadow;    transition-duration: var(--duration-quick);    transition-timing-function: var(--easing-glide);    &::after {      content: '';      width: .5625rem;      height: .5625rem;      opacity: 0;      scale: .6;      background: var(--color-text-inverse);      @apply --check-mark;      /* The mark leaves at once and arrives with a short settle, as the checkbox's does. */      transition: opacity var(--duration-press) linear, scale var(--duration-press) linear;    }    @media (prefers-reduced-motion: reduce) {      &::after {        scale: 1;      }    }  }  @media (hover: hover) {    .tree-item:hover:not([aria-checked="true"], [aria-checked="mixed"], [aria-disabled="true"]) .tree-check {      @apply --check-well-hover;      /* The check opts out of forced colors, so its hover is written in system colors too. */      @media (forced-colors: active) {        background: Field;        box-shadow: inset 0 0 0 1px Highlight;      }    }  }  .tree-item:is([aria-checked="true"], [aria-checked="mixed"]) .tree-check {    @apply --check-ink;    &::after {      opacity: 1;      scale: 1;      transition: opacity var(--duration-quick) var(--easing-glide), scale var(--duration-base) var(--easing-glide);    }  }  .tree-item[aria-checked="mixed"] .tree-check::after {    @apply --check-dash;  }  .tree-item[aria-disabled="true"] .tree-check {    opacity: .5;  }  @media (forced-colors: active) {    .tree-check {      forced-color-adjust: none;      background: Field;      box-shadow: inset 0 0 0 1px FieldText;      &::after {        background: HighlightText;      }    }    .tree-item:is([aria-checked="true"], [aria-checked="mixed"]) .tree-check {      background: Highlight;      box-shadow: none;    }    .tree-item[aria-disabled="true"] .tree-check {      opacity: 1;      box-shadow: inset 0 0 0 1px GrayText;    }  }  /*   * A branch whose rows are on their way: the spinner it holds stands in the chevron's column until they   * arrive, and is not drawn otherwise. Put the spinner first in the row.   */  .tree-item .spinner {    --spinner-size: .875rem;    display: none;  }  .tree-item[aria-busy="true"] {    cursor: progress;    &::before {      display: none;    }    .spinner {      display: inline-block;    }  }  /*   * Renaming: the editable that takes the label's place reaches out by its padding, so its text stands where   * the label's stood. The row around it takes no selection; the field does.   */  .tree-item .editable {    flex: 0 1 auto;    min-width: 0;  }  .tree-item .editable-field {    cursor: text;    user-select: text;    -webkit-user-select: text;  }}