ナビゲーション

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 は枝の子の行を1段下げてまとめます。シェブロンから下に伸びる細い線に沿って、子の行が並びます。

#アイコン付き

シェブロン用の列の後ろにアイコンを置き、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 キーで止まる行は1つで、矢印キーでツリー内を移動し、枝を開閉します。この操作にはスクリプトの 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、Space、またはクリックで枝を展開し、末端の行ではその行を選択します。

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 キーで止まる行は1つ。 ビヘイビアは tabindex を使って、Tab 順序に1行だけを含めます。マークアップで tabindex="0" が指定された行を優先し、該当する行がなければ選択中または現在の行、いずれもなければ先頭行を使います。フォーカスを受けた行が、Tab 順序に含まれる唯一の行になります。ビヘイビアを解除すると、各行の tabindex が元に戻ります。
  • 移動。 矢印キーで移動すると、閉じた枝の中の行、hidden の行、無効な行はスキップされます。
  • アクティブ化。 summary、リンク、ボタンには、ブラウザー標準の操作が適用されます。標準の操作を持たない行(div に role="treeitem" を指定した行)は、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" が指定された行は操作を受け付けません。リンク先への移動も、枝の開閉も行わず、選択、チェック、名前の変更もできません。

このビヘイビアは、最初の2つのプレビューで使っている <details> のツリーにも適用できます。これらのプレビューはビヘイビアなしで動作します。適用すると、open を介して tree-group を開閉し、Tab キーで止まる行が1つになります。ツリー内は同じキーで移動できます。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 キーを押しながらクリックすると 1 行追加されます。キー操作では Space キーで同じことができます。
  • クリックすると、その行だけが選択されます。Control または Command を押しながらクリックすると、ほかの行の選択を保ったまま 1 行の選択を切り替えます。Enter はクリックと同じ動作です。
  • Space は、フォーカスのある行の選択を切り替えます。矢印キーは移動だけを行います。
  • 範囲選択。 最後に選んだ行が起点になります。Shift を押したままクリックするか、Space、ArrowDown、ArrowUp のいずれかを押すと、起点から対象の行までの表示中の行をすべて選択します。Control または Command と Shift を押しながら Home または End を押すと、先頭または末尾の行まで選択します。閉じた枝の中の行と無効な行はスキップされます。
  • すべて。 Control または Command を押しながら A を押すと、表示されているすべての行を選択します。すべてが選択済みの場合は、選択を解除します。

#チェックボックスツリー

行が aria-checked を持つ場合、ツリーはチェックボックスツリーになります。各行にチェックが付き、枝をチェックすると、閉じた枝の中も含めて、その下のすべての行がチェックされます。枝は配下の行の状態を示します。すべてチェックされていれば true、1 つもなければ 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 内のファイルを 1 つチェック解除すると、枝は mixed になります。
  • Space は、フォーカスのある行のチェックを切り替えます。枝の場合は配下のすべての行も切り替えます。mixed の枝はチェックされます。ただし、配下の変更できる行がすべてチェック済みの場合は、チェックが外れます。
  • クリックすると、末端の行はチェックされます。枝の行では、チェックをクリックするとチェックされ、行のそれ以外の部分をクリックすると開閉します。Enter は行のクリックと同じ動作です。
  • 無効な行は、枝がチェックされてもチェックの状態を保ちます。そのため、枝が mixed のまま残ることがあります。
  • 値は、チェックされた末端の行のリストで、ツリー内の順序に並びます。枝は値に含まれません。 枝のチェックは配下の行の状態を示すだけなので、値はフォルダーではなく各ファイルを示します。行がまだ読み込まれていない枝も含まれません。チェックされた枝の下に行が読み込まれると、それらの行はチェックされ、ツリーは 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 を明示的に呼び出します。同じルートに対して2回呼び出すと、2回目が1回目を置き換えます。

ビヘイビアは新しいラベルを 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))

#ツリーとナビゲーションの使い分け

ナビゲーションは、移動先を1階層のグループにまとめた短いリストです。現在の行は浮き上がった面で示します。ツリーは複数の階層をコンパクトな行で表示し、各枝の先頭にシェブロンを置きます。

#キーボード操作

キー操作
Tabdetails を使う場合は、Tab キーで各行にフォーカスを移せます。スクリプトを使う場合、ツリー内で Tab キーによる移動先は1か所だけで、現在の行、選択中の行、または先頭行です。
Enter, Space枝を開閉するか、行のリンク先に移動します。summary ではブラウザーがスクリプトなしで処理します。スクリプト使用時は、role="treeitem" が付いた枝以外の行を選択します。チェックボックスツリーでは、Enter で末端の行をチェックします。
ArrowDown, ArrowUpスクリプト使用時は、表示されている次または前の行に移動する
ArrowRightスクリプト使用時は、閉じている枝を開く。開いている枝では最初の子の行に移動する。右から左に表示する場合は ArrowLeft
ArrowLeftスクリプト使用時は、開いている枝を閉じる。閉じた枝または葉では親の行に移動する。右から左に表示する場合は ArrowRight
Home, Endスクリプト使用時は、表示されている最初または最後の行に移動する
A characterスクリプト使用時は、入力した文字で始まる次の表示行に移動する。すばやく続けて入力した文字は1語として扱う
*スクリプト使用時は、フォーカス中の行と同じ階層にある閉じた枝をすべて開く
Spaceスクリプト使用時、aria-multiselectable="true" のツリーで、フォーカスのある行を選択または選択解除します。チェックボックスツリーでは、その行と配下のすべての行をチェックまたはチェック解除します。
Control+click複数選択できるツリーで、ほかの行の選択を保ったまま 1 行を選択または選択解除します。クリックだけの場合は、その行だけを選択します。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 を指定するとすべての行にチェックが付きます。どちらの場合も、値はリストになります。初期値は初回のレンダリングに反映され、枝のチェックが mixed 状態なら、その時点で表示されます。
  • 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枝の下に1階層インデントした行。シェブロンから下に伸びる極細の罫線に沿って配置します。スクリプトでツリーを制御する場合は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;  }}