导航

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 属性的行和禁用行。
  • 激活。 summary、链接和按钮沿用浏览器原生的激活方式。对于没有原生激活方式的行,也就是在 div 上使用 role="treeitem" 的行,可按 Enter、空格键 或点击来激活。激活分支会切换其展开或折叠状态,激活叶节点会选中该行。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 点击可添加一行;用键盘时按空格键效果相同。
  • 点击只选中该行。按住 Control 或 Command 点击,会选中或取消选中一行,同时保留其他已选行。Enter 的效果与点击相同。
  • 空格键 选中或取消选中当前获得焦点的行。方向键只移动焦点。
  • 连续选择。 最后选择的行是锚点。按住 Shift 点击,或按 Shift 加空格键、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 中的一个文件,该分支会变为部分勾选。
  • 空格键 勾选或取消勾选当前获得焦点的行;对于分支,会连同其下所有行一起勾选或取消勾选。部分勾选的分支会被勾选;如果其下所有可更改的行都已勾选,则取消勾选。
  • 点击叶节点会勾选它;对于分支,点击其勾选框会勾选它,点击该行的其他部分会展开或折叠它。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行内文字:过长时以省略号结尾。脚本的重命名功能会将这部分变为可编辑元素
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;  }}