ツリー
概要
ツリーは、アイテムを展開して子を表示したり、折りたたんで非表示にしたりできる階層データを表示します。ユーザーは矢印キーで移動し、ノードを展開・折りたたみ、ナビゲーションやデータ選択のシナリオのためにアイテムを選択できます。
TS
import {Component, signal} from '@angular/core';import {NgTemplateOutlet} from '@angular/common';import {Tree, TreeItem, TreeItemGroup} from '@angular/aria/tree';type TreeNode = { name: string; value: string; children?: TreeNode[]; disabled?: boolean; expanded?: boolean;};@Component({ selector: 'app-root', templateUrl: 'app.component.html', styleUrl: 'app.component.css', imports: [Tree, TreeItem, TreeItemGroup, NgTemplateOutlet],})export class App { readonly nodes: TreeNode[] = [ { name: 'public', value: 'public', children: [ {name: 'index.html', value: 'public/index.html'}, {name: 'favicon.ico', value: 'public/favicon.ico'}, {name: 'styles.css', value: 'public/styles.css'}, ], expanded: true, }, { name: 'src', value: 'src', children: [ { name: 'app', value: 'src/app', children: [ {name: 'app.component.ts', value: 'src/app/app.component.ts'}, {name: 'app.module.ts', value: 'src/app/app.module.ts', disabled: true}, {name: 'app.css', value: 'src/app/app.css'}, ], expanded: false, }, { name: 'assets', value: 'src/assets', children: [{name: 'logo.png', value: 'src/assets/logo.png'}], expanded: false, }, { name: 'environments', value: 'src/environments', children: [ { name: 'environment.prod.ts', value: 'src/environments/environment.prod.ts', expanded: false, }, {name: 'environment.ts', value: 'src/environments/environment.ts'}, ], expanded: false, }, {name: 'main.ts', value: 'src/main.ts'}, {name: 'polyfills.ts', value: 'src/polyfills.ts'}, {name: 'styles.css', value: 'src/styles.css', disabled: true}, {name: 'test.ts', value: 'src/test.ts'}, ], expanded: false, }, {name: 'angular.json', value: 'angular.json'}, {name: 'package.json', value: 'package.json'}, {name: 'README.md', value: 'README.md'}, ]; readonly selected = signal(['angular.json']);}
HTML
<ul ngTree #tree="ngTree" [(values)]="selected" class="basic-tree"> <ng-template [ngTemplateOutlet]="treeNodes" [ngTemplateOutletContext]="{nodes: nodes, parent: tree}" /></ul><ng-template #treeNodes let-nodes="nodes" let-parent="parent"> @for (node of nodes; track node.value) { <li ngTreeItem [parent]="parent" [value]="node.value" [label]="node.name" [disabled]="node.disabled" [(expanded)]="node.expanded" #treeItem="ngTreeItem" > <span aria-hidden="true" class="material-symbols-outlined expand-icon" translate="no">{{node.children ? 'chevron_right' : ''}}</span > <span aria-hidden="true" class="material-symbols-outlined" translate="no">{{node.children ? 'folder' : 'docs'}}</span > {{ node.name }} <span aria-hidden="true" class="material-symbols-outlined selected-icon" translate="no">check</span> </li> @if (node.children) { <ul role="group"> <ng-template ngTreeItemGroup [ownedBy]="treeItem" #group="ngTreeItemGroup"> <ng-template [ngTemplateOutlet]="treeNodes" [ngTemplateOutletContext]="{nodes: node.children, parent: group}" /> </ng-template> </ul> } }</ng-template>
CSS
@import url('https://fonts.googleapis.com/icon?family=Material+Symbols+Outlined');:host { display: flex; justify-content: center; user-select: none; font-family: var(--inter-font);}[ngTree] { min-width: 24rem; background-color: var(--septenary-contrast); border-radius: 0.5rem; padding: 0.5rem;}[ngTreeItem] { cursor: pointer; list-style: none; text-decoration: none; display: flex; align-items: center; gap: 1rem; padding: 0.3rem 1rem;}[ngTreeItem][aria-disabled='true'] { opacity: 0.5; cursor: default;}[ngTreeItem]:focus,[ngTreeItem]:hover { background-color: var(--quinary-contrast);}[ngTreeItem]:focus { outline: 1px solid color-mix(in srgb, var(--hot-pink) 60%, transparent);}[ngTreeItem][aria-selected='true'],[ngTreeItem][aria-selected='true'] .expand-icon { background-image: var(--pink-to-purple-horizontal-gradient); background-clip: text; color: transparent;}.material-symbols-outlined { margin: 0; width: 24px;}.expand-icon { transition: transform 0.2s ease;}[ngTreeItem][aria-expanded='true'] .expand-icon { transform: rotate(90deg);}.selected-icon { visibility: hidden; margin-left: auto;}[ngTreeItem][aria-current] .selected-icon,[ngTreeItem][aria-selected='true'] .selected-icon { visibility: visible;}li[aria-expanded='false'] + ul[role='group'] { display: none;}
使用法
ツリーは、ユーザーがネストされた構造をナビゲートする必要がある階層データを表示するのに適しています。
ツリーを使用する場合:
- ファイルシステムのナビゲーションを構築する
- フォルダーとドキュメントの階層を表示する
- ネストされたメニュー構造を作成する
- 組織図を表示する
- 階層データを閲覧する
- ネストされたセクションを持つサイトナビゲーションを実装する
ツリーを避ける場合:
- フラットなリストを表示する場合(代わりにListboxを使用)
- データテーブルを表示する場合(代わりにGridを使用)
- シンプルなドロップダウンを作成する場合(代わりにSelectを使用)
- パンくずナビゲーションを構築する場合(パンくずパターンを使用)
機能
- 階層ナビゲーション - 展開・折りたたみ機能付きのネストされたツリー構造
- 選択モード - 明示的またはフォーカス追従動作による単一選択または複数選択
- フォーカス追従選択 - フォーカス変更時に任意で自動選択
- キーボードナビゲーション - 矢印キー、Home、End、先行入力による検索
- 展開/折りたたみ - 右/左矢印キーまたはEnterキーで親ノードを切り替え
- 無効化されたアイテム - フォーカス管理付きで特定のノードを無効化
- フォーカスモード - Roving tabindexまたはactivedescendantフォーカス戦略
- RTLサポート - 右から左へ記述する言語のナビゲーション
例
ナビゲーションツリー
アイテムを選択するのではなく、クリックすることでアクションをトリガーするナビゲーションにはツリーを使用します。
TS
import {Component, signal} from '@angular/core';import {NgTemplateOutlet} from '@angular/common';import {Tree, TreeItem, TreeItemGroup} from '@angular/aria/tree';type TreeNode = { name: string; value: string; icon: string; children?: TreeNode[]; disabled?: boolean; expanded?: boolean;};@Component({ selector: 'app-root', templateUrl: 'app.component.html', styleUrl: 'app.component.css', imports: [Tree, TreeItem, TreeItemGroup, NgTemplateOutlet],})export class App { readonly nodes: TreeNode[] = [ { name: 'Inbox', value: 'inbox', icon: 'inbox', }, { name: 'Sent', value: 'sent', icon: 'send', }, { name: 'Drafts', value: 'drafts', icon: 'draft', }, { name: 'Spam', value: 'spam', icon: 'report', }, { name: 'Trash', value: 'trash', icon: 'delete', }, { name: 'Labels', value: 'labels', expanded: true, icon: 'label', children: [ {name: 'Personal', value: 'folders/personal', icon: 'label'}, {name: 'Work', value: 'folders/work', icon: 'label'}, {name: 'Travel', value: 'folders/travel', icon: 'label'}, {name: 'Receipts', value: 'folders/receipts', icon: 'label'}, ], }, ]; readonly selected = signal(['inbox']);}
HTML
<ul ngTree #tree="ngTree" [nav]="true" [(values)]="selected" class="basic-tree"> <ng-template [ngTemplateOutlet]="treeNodes" [ngTemplateOutletContext]="{nodes: nodes, parent: tree}" /></ul><ng-template #treeNodes let-nodes="nodes" let-parent="parent"> @for (node of nodes; track node.value) { <a ngTreeItem [parent]="parent" [value]="node.value" [label]="node.name" [disabled]="node.disabled" [selectable]="!node.children" [(expanded)]="node.expanded" #treeItem="ngTreeItem" href="#{{ node.name }}" (click)="$event.preventDefault()" > <span aria-hidden="true" class="material-symbols-outlined" translate="no">{{node.icon}}</span> {{ node.name }} <span aria-hidden="true" class="material-symbols-outlined expand-icon" translate="no">{{node.children ? 'keyboard_arrow_up' : ''}}</span > </a> @if (node.children) { <ul role="group"> <ng-template ngTreeItemGroup [ownedBy]="treeItem" #group="ngTreeItemGroup"> <ng-template [ngTemplateOutlet]="treeNodes" [ngTemplateOutletContext]="{nodes: node.children, parent: group}" /> </ng-template> </ul> } }</ng-template>
CSS
@import url('https://fonts.googleapis.com/icon?family=Material+Symbols+Outlined');:host { display: flex; justify-content: center; user-select: none; font-family: var(--inter-font);}[ngTree] { min-width: 24rem; background-color: var(--septenary-contrast); border-radius: 0.5rem; padding: 0.5rem;}[ngTreeItem] { cursor: pointer; list-style: none; text-decoration: none; display: flex; align-items: center; gap: 1rem; padding: 0.3rem 1rem; color: var(--primary-contrast);}[ngTreeItem][aria-disabled='true'] { opacity: 0.5; cursor: default;}[ngTreeItem]:focus,[ngTreeItem]:hover { background-color: var(--quinary-contrast);}[ngTreeItem]:focus { outline: 1px solid color-mix(in srgb, var(--hot-pink) 60%, transparent);}[ngTreeItem][aria-current] { background-image: var(--pink-to-purple-horizontal-gradient); background-clip: text; color: transparent;}.material-symbols-outlined { margin: 0; width: 24px;}.expand-icon { transition: transform 0.2s ease;}[ngTreeItem][aria-expanded='true'] .expand-icon { transform: rotate(180deg);}li[aria-expanded='false'] + ul[role='group'] { display: none;}
[nav]="true"を設定してナビゲーションモードを有効にします。これにより、選択の代わりにaria-currentを使用して現在のページを示します。
単一選択
ユーザーがツリーから1つのアイテムを選択するシナリオでは、単一選択を有効にします。
TS
import {Component, signal} from '@angular/core';import {NgTemplateOutlet} from '@angular/common';import {Tree, TreeItem, TreeItemGroup} from '@angular/aria/tree';type TreeNode = { name: string; value: string; children?: TreeNode[]; disabled?: boolean; expanded?: boolean;};@Component({ selector: 'app-root', templateUrl: 'app.component.html', styleUrl: 'app.component.css', imports: [Tree, TreeItem, TreeItemGroup, NgTemplateOutlet],})export class App { readonly nodes: TreeNode[] = [ { name: 'public', value: 'public', children: [ {name: 'index.html', value: 'public/index.html'}, {name: 'favicon.ico', value: 'public/favicon.ico'}, {name: 'styles.css', value: 'public/styles.css'}, ], expanded: true, }, { name: 'src', value: 'src', children: [ { name: 'app', value: 'src/app', children: [ {name: 'app.component.ts', value: 'src/app/app.component.ts'}, {name: 'app.module.ts', value: 'src/app/app.module.ts', disabled: true}, {name: 'app.css', value: 'src/app/app.css'}, ], expanded: false, }, { name: 'assets', value: 'src/assets', children: [{name: 'logo.png', value: 'src/assets/logo.png'}], expanded: false, }, { name: 'environments', value: 'src/environments', children: [ { name: 'environment.prod.ts', value: 'src/environments/environment.prod.ts', expanded: false, }, {name: 'environment.ts', value: 'src/environments/environment.ts'}, ], expanded: false, }, {name: 'main.ts', value: 'src/main.ts'}, {name: 'polyfills.ts', value: 'src/polyfills.ts'}, {name: 'styles.css', value: 'src/styles.css', disabled: true}, {name: 'test.ts', value: 'src/test.ts'}, ], expanded: false, }, {name: 'angular.json', value: 'angular.json'}, {name: 'package.json', value: 'package.json'}, {name: 'README.md', value: 'README.md'}, ]; readonly selected = signal(['angular.json']);}
HTML
<ul ngTree #tree="ngTree" [(values)]="selected" class="basic-tree"> <ng-template [ngTemplateOutlet]="treeNodes" [ngTemplateOutletContext]="{nodes: nodes, parent: tree}" /></ul><ng-template #treeNodes let-nodes="nodes" let-parent="parent"> @for (node of nodes; track node.value) { <li ngTreeItem [parent]="parent" [value]="node.value" [label]="node.name" [disabled]="node.disabled" [(expanded)]="node.expanded" #treeItem="ngTreeItem" > <span aria-hidden="true" class="material-symbols-outlined expand-icon" translate="no">{{node.children ? 'chevron_right' : ''}}</span > <span aria-hidden="true" class="material-symbols-outlined" translate="no">{{node.children ? 'folder' : 'docs'}}</span > {{ node.name }} <span aria-hidden="true" class="material-symbols-outlined selected-icon" translate="no">check</span> </li> @if (node.children) { <ul role="group"> <ng-template ngTreeItemGroup [ownedBy]="treeItem" #group="ngTreeItemGroup"> <ng-template [ngTemplateOutlet]="treeNodes" [ngTemplateOutletContext]="{nodes: node.children, parent: group}" /> </ng-template> </ul> } }</ng-template>
CSS
@import url('https://fonts.googleapis.com/icon?family=Material+Symbols+Outlined');:host { display: flex; justify-content: center; user-select: none; font-family: var(--inter-font);}[ngTree] { min-width: 24rem; background-color: var(--septenary-contrast); border-radius: 0.5rem; padding: 0.5rem;}[ngTreeItem] { cursor: pointer; list-style: none; text-decoration: none; display: flex; align-items: center; gap: 1rem; padding: 0.3rem 1rem;}[ngTreeItem][aria-disabled='true'] { opacity: 0.5; cursor: default;}[ngTreeItem]:focus,[ngTreeItem]:hover { background-color: var(--quinary-contrast);}[ngTreeItem]:focus { outline: 1px solid color-mix(in srgb, var(--hot-pink) 60%, transparent);}[ngTreeItem][aria-selected='true'],[ngTreeItem][aria-selected='true'] .expand-icon { background-image: var(--pink-to-purple-horizontal-gradient); background-clip: text; color: transparent;}.material-symbols-outlined { margin: 0; width: 24px;}.expand-icon { transition: transform 0.2s ease;}[ngTreeItem][aria-expanded='true'] .expand-icon { transform: rotate(90deg);}.selected-icon { visibility: hidden; margin-left: auto;}[ngTreeItem][aria-current] .selected-icon,[ngTreeItem][aria-selected='true'] .selected-icon { visibility: visible;}li[aria-expanded='false'] + ul[role='group'] { display: none;}
TS
import {Component, computed, signal} from '@angular/core';import {NgTemplateOutlet} from '@angular/common';import {Tree, TreeItem, TreeItemGroup} from '@angular/aria/tree';type TreeNode = { name: string; value: string; children?: TreeNode[]; disabled?: boolean; expanded?: boolean;};@Component({ selector: 'app-root', templateUrl: 'app.component.html', styleUrl: 'app.component.css', imports: [Tree, TreeItem, TreeItemGroup, NgTemplateOutlet],})export class App { readonly nodes: readonly TreeNode[] = [ { name: 'C:', value: 'C:', expanded: true, children: [ { name: 'Program Files/', value: 'C:/Program Files', children: [ {name: 'Common Files', value: 'C:/Program Files/Common Files'}, {name: 'Internet Explorer', value: 'C:/Program Files/Internet Explorer'}, ], expanded: false, }, { name: 'Users/', value: 'C:/Users', children: [ {name: 'Default', value: 'C:/Users/Default'}, {name: 'Public', value: 'C:/Users/Public'}, ], expanded: false, }, { name: 'Windows/', value: 'C:/Windows', children: [ {name: 'System32', value: 'C:/Windows/System32'}, {name: 'Web', value: 'C:/Windows/Web'}, ], expanded: false, }, {name: 'pagefile.sys', value: 'C:/pagefile.sys'}, {name: 'swapfile.sys', value: 'C:/swapfile.sys', disabled: true}, ], }, ]; readonly selected = signal([]); readonly selectedCount = computed(() => this.selected().length);}
HTML
<div class="win95-file-explorer"> <div class="title-bar"> <div class="title-bar-text">Exploring - (C:)</div> <div class="title-bar-controls"> <button tabindex="-1" aria-label="Minimize"><span>-</span></button> <button tabindex="-1" aria-label="Maximize"><span>□</span></button> <button tabindex="-1" aria-label="Close"><span>×</span></button> </div> </div> <div class="menu-bar"> <div class="menu-item"><u>F</u>ile</div> <div class="menu-item"><u>E</u>dit</div> <div class="menu-item"><u>V</u>iew</div> <div class="menu-item"><u>T</u>ools</div> <div class="menu-item"><u>H</u>elp</div> </div> <div class="toolbar"> <button tabindex="-1" class="win95-btn"><span class="icon">←</span> Back</button> <button tabindex="-1" class="win95-btn"><span class="icon">→</span> Forward</button> <button tabindex="-1" class="win95-btn"><span class="icon">↑</span> Up</button> </div> <div class="tree-view"> <ul ngTree #tree="ngTree" class="retro-tree" [(values)]="selected"> <ng-template [ngTemplateOutlet]="treeNodes" [ngTemplateOutletContext]="{nodes: nodes, parent: tree}" /> </ul> </div> <div class="status-bar"> <div class="status-panel status-panel-grow">{{selectedCount()}} object(s) selected</div> <div class="status-panel status-panel-right">4.5MB</div> </div></div><ng-template #treeNodes let-nodes="nodes" let-parent="parent"> @for (node of nodes; track node.value) { <li ngTreeItem [parent]="parent" [value]="node.value" [label]="node.name" [(expanded)]="node.expanded" [disabled]="node.disabled" #treeItem="ngTreeItem" > <span aria-hidden="true"> @if (node.children) { {{treeItem.expanded() ? '📂' : '📁'}} } @else { 📄 } </span> {{ node.name }} <span aria-hidden="true" class="material-symbols-outlined selected-icon" translate="no">check</span> </li> @if (node.children) { <ul role="group"> <ng-template ngTreeItemGroup [ownedBy]="treeItem" #group="ngTreeItemGroup"> <ng-template [ngTemplateOutlet]="treeNodes" [ngTemplateOutletContext]="{nodes: node.children, parent: group}" /> </ng-template> </ul> } }</ng-template>
CSS
@import url('https://fonts.googleapis.com/icon?family=Material+Symbols+Outlined');@import url('https://fonts.googleapis.com/css2?family=Jersey+20&display=swap');:host { display: flex; justify-content: center; user-select: none; --win95-gray: #c0c0c0; --win95-dark-gray: #808080; --win95-light: #ffffff; --win95-shadow: #000000; --win95-blue: #000080; --win95-active-blue: linear-gradient(to right, #000080, #1084d0); --win95-font: "Jersey 20", sans-serif; font-family: var(--win95-font); font-size: 1.1rem;}.win95-file-explorer { border-style: solid; border-width: 1px; border-color: var(--win95-light) var(--win95-dark-gray) var(--win95-dark-gray) var(--win95-light); box-shadow: 1px 1px 0 var(--win95-shadow); background-color: var(--win95-gray); min-width: 350px;}.win95-btn { background-color: var(--win95-gray); padding: 3px 8px; cursor: pointer; border-style: solid; border-width: 1px; border-color: var(--win95-light) var(--win95-dark-gray) var(--win95-dark-gray) var(--win95-light); box-shadow: 1px 1px 0 var(--win95-shadow);}.win95-btn:active { box-shadow: 1px 1px 0 var(--win95-shadow) inset; padding: 4px 7px 2px 9px; border-style: solid; border-width: 1px; border-color: var(--win95-dark-gray) var(--win95-light) var(--win95-light) var(--win95-dark-gray);}.title-bar { background: var(--win95-active-blue); color: var(--win95-light); padding: 3px 6px; height: 20px; display: flex; justify-content: space-between; align-items: center;}.title-bar-text { font-weight: bold;}.title-bar-controls button { background: var(--win95-gray); border: 1px solid var(--win95-light); color: var(--win95-shadow); width: 16px; height: 14px; line-height: 8px; font-size: 14px; padding: 0; margin-left: 1px; border-style: solid; border-width: 1px; border-color: var(--win95-light) var(--win95-dark-gray) var(--win95-dark-gray) var(--win95-light); box-shadow: 1px 1px 0 var(--win95-shadow);}.title-bar-controls button span { display: block; margin-top: -3px;}.menu-bar { display: flex; background-color: var(--win95-gray); border-bottom: 1px solid var(--win95-dark-gray); padding: 1px 2px;}.menu-item { padding: 0 6px; cursor: default; margin-right: 4px;}.menu-item:hover { background-color: var(--win95-blue); color: var(--win95-light);}.toolbar { display: flex; align-items: center; padding: 4px; border-top: 1px solid var(--win95-light); border-bottom: 1px solid var(--win95-dark-gray);}.toolbar .win95-btn { display: flex; align-items: center; margin-right: 8px;}.toolbar .icon { font-size: 1.25rem; line-height: 1; margin-right: 4px;}.tree-view { background-color: var(--win95-light); min-height: 300px; padding: 4px; border-width: 1px; border-style: solid; border-color: var(--win95-dark-gray) var(--win95-light) var(--win95-light) var(--win95-dark-gray); color: var(--win95-shadow);}.status-bar { margin-top: 2px;}.status-panel-grow { flex-grow: 1;}.status-panel-right { text-align: right;}.status-bar { height: 18px; background-color: var(--win95-gray); border-top: 1px solid var(--win95-light); border-left: 1px solid var(--win95-light); border-right: 1px solid var(--win95-dark-gray); border-bottom: 1px solid var(--win95-dark-gray); display: flex; font-size: 14px;}.status-panel { padding: 0 4px; height: 100%; display: flex; align-items: center; margin-right: 1px; border-style: solid; border-color: var(--win95-dark-gray) var(--win95-light) var(--win95-light) var(--win95-dark-gray); border-width: 1px; flex-grow: 1;}.status-panel:last-child { flex-grow: 0; width: 120px;}[ngTree] { padding: 0; margin: 0;}[ngTreeItem] { cursor: pointer; list-style: none; text-decoration: none; display: flex; align-items: center; gap: 0.75rem; padding: 0.25rem 0.75rem;}[ngTreeItem][aria-disabled='true'] { opacity: 0.5; cursor: default;}[ngTreeItem]:focus,[ngTreeItem]:hover { background-color: var(--senary-contrast); color: var(--primary-contrast); outline: none;}[ngTreeItem][aria-selected='true'] { background-color: var(--win95-blue); color: var(--win95-light);}.selected-icon { visibility: hidden; margin-left: auto;}[ngTreeItem][aria-current] .selected-icon,[ngTreeItem][aria-selected='true'] .selected-icon { visibility: visible;}li[aria-expanded='false'] + ul[role='group'] { display: none;}
単一選択の場合は、[multi]="false"(デフォルト)のままにします。ユーザーはSpaceキーを押してフォーカスされているアイテムを選択します。
複数選択
ユーザーがツリーから複数のアイテムを選択できるようにします。
TS
import {Component, signal} from '@angular/core';import {NgTemplateOutlet} from '@angular/common';import {Tree, TreeItem, TreeItemGroup} from '@angular/aria/tree';type TreeNode = { name: string; value: string; children?: TreeNode[]; disabled?: boolean; expanded?: boolean;};@Component({ selector: 'app-root', templateUrl: 'app.component.html', styleUrl: 'app.component.css', imports: [Tree, TreeItem, TreeItemGroup, NgTemplateOutlet],})export class App { readonly nodes: TreeNode[] = [ { name: 'public', value: 'public', children: [ {name: 'index.html', value: 'public/index.html'}, {name: 'favicon.ico', value: 'public/favicon.ico'}, {name: 'styles.css', value: 'public/styles.css'}, ], expanded: true, }, { name: 'src', value: 'src', children: [ { name: 'app', value: 'src/app', children: [ {name: 'app.component.ts', value: 'src/app/app.component.ts'}, {name: 'app.module.ts', value: 'src/app/app.module.ts', disabled: true}, {name: 'app.css', value: 'src/app/app.css'}, ], expanded: false, }, { name: 'assets', value: 'src/assets', children: [{name: 'logo.png', value: 'src/assets/logo.png'}], expanded: false, }, { name: 'environments', value: 'src/environments', children: [ { name: 'environment.prod.ts', value: 'src/environments/environment.prod.ts', expanded: false, }, {name: 'environment.ts', value: 'src/environments/environment.ts'}, ], expanded: false, }, {name: 'main.ts', value: 'src/main.ts'}, {name: 'polyfills.ts', value: 'src/polyfills.ts'}, {name: 'styles.css', value: 'src/styles.css', disabled: true}, {name: 'test.ts', value: 'src/test.ts'}, ], expanded: false, }, {name: 'angular.json', value: 'angular.json'}, {name: 'package.json', value: 'package.json'}, {name: 'README.md', value: 'README.md'}, ]; readonly selected = signal(['angular.json', 'public/styles.css']);}
HTML
<ul ngTree #tree="ngTree" [multi]="true" [(values)]="selected" class="basic-tree"> <ng-template [ngTemplateOutlet]="treeNodes" [ngTemplateOutletContext]="{nodes: nodes, parent: tree}" /></ul><ng-template #treeNodes let-nodes="nodes" let-parent="parent"> @for (node of nodes; track node.value) { <li ngTreeItem [parent]="parent" [value]="node.value" [label]="node.name" [disabled]="node.disabled" [(expanded)]="node.expanded" #treeItem="ngTreeItem" > <span aria-hidden="true" class="material-symbols-outlined expand-icon" translate="no">{{node.children ? 'chevron_right' : ''}}</span > <span aria-hidden="true" class="material-symbols-outlined" translate="no">{{node.children ? 'folder' : 'docs'}}</span > {{ node.name }} <span aria-hidden="true" class="material-symbols-outlined selected-icon" translate="no">check</span> </li> @if (node.children) { <ul role="group"> <ng-template ngTreeItemGroup [ownedBy]="treeItem" #group="ngTreeItemGroup"> <ng-template [ngTemplateOutlet]="treeNodes" [ngTemplateOutletContext]="{nodes: node.children, parent: group}" /> </ng-template> </ul> } }</ng-template>
CSS
@import url('https://fonts.googleapis.com/icon?family=Material+Symbols+Outlined');:host { display: flex; justify-content: center; user-select: none; font-family: var(--inter-font);}[ngTree] { min-width: 24rem; background-color: var(--septenary-contrast); border-radius: 0.5rem; padding: 0.5rem;}[ngTreeItem] { cursor: pointer; list-style: none; text-decoration: none; display: flex; align-items: center; gap: 1rem; padding: 0.3rem 1rem;}[ngTreeItem][aria-disabled='true'] { opacity: 0.5; cursor: default;}[ngTreeItem]:focus,[ngTreeItem]:hover { background-color: var(--quinary-contrast);}[ngTreeItem]:focus { outline: 1px solid color-mix(in srgb, var(--hot-pink) 60%, transparent);}[ngTreeItem][aria-selected='true'],[ngTreeItem][aria-selected='true'] .expand-icon { background-image: var(--pink-to-purple-horizontal-gradient); background-clip: text; color: transparent;}.material-symbols-outlined { margin: 0; width: 24px;}.expand-icon { transition: transform 0.2s ease;}[ngTreeItem][aria-expanded='true'] .expand-icon { transform: rotate(90deg);}.selected-icon { visibility: hidden; margin-left: auto;}[ngTreeItem][aria-current] .selected-icon,[ngTreeItem][aria-selected='true'] .selected-icon { visibility: visible;}li[aria-expanded='false'] + ul[role='group'] { display: none;}
TS
import {Component, computed, signal} from '@angular/core';import {NgTemplateOutlet} from '@angular/common';import {Tree, TreeItem, TreeItemGroup} from '@angular/aria/tree';type TreeNode = { name: string; value: string; children?: TreeNode[]; disabled?: boolean; expanded?: boolean;};@Component({ selector: 'app-root', templateUrl: 'app.component.html', styleUrl: 'app.component.css', imports: [Tree, TreeItem, TreeItemGroup, NgTemplateOutlet],})export class App { readonly nodes: readonly TreeNode[] = [ { name: 'C:', value: 'C:', expanded: true, children: [ { name: 'Program Files/', value: 'C:/Program Files', children: [ {name: 'Common Files', value: 'C:/Program Files/Common Files'}, {name: 'Internet Explorer', value: 'C:/Program Files/Internet Explorer'}, ], expanded: false, }, { name: 'Users/', value: 'C:/Users', children: [ {name: 'Default', value: 'C:/Users/Default'}, {name: 'Public', value: 'C:/Users/Public'}, ], expanded: false, }, { name: 'Windows/', value: 'C:/Windows', children: [ {name: 'System32', value: 'C:/Windows/System32'}, {name: 'Web', value: 'C:/Windows/Web'}, ], expanded: false, }, {name: 'pagefile.sys', value: 'C:/pagefile.sys'}, {name: 'swapfile.sys', value: 'C:/swapfile.sys', disabled: true}, ], }, ]; readonly selected = signal([]); readonly selectedCount = computed(() => this.selected().length);}
HTML
<div class="win95-file-explorer"> <div class="title-bar"> <div class="title-bar-text">Exploring - (C:)</div> <div class="title-bar-controls"> <button tabindex="-1" aria-label="Minimize"><span>-</span></button> <button tabindex="-1" aria-label="Maximize"><span>□</span></button> <button tabindex="-1" aria-label="Close"><span>×</span></button> </div> </div> <div class="menu-bar"> <div class="menu-item"><u>F</u>ile</div> <div class="menu-item"><u>E</u>dit</div> <div class="menu-item"><u>V</u>iew</div> <div class="menu-item"><u>T</u>ools</div> <div class="menu-item"><u>H</u>elp</div> </div> <div class="toolbar"> <button tabindex="-1" class="win95-btn"><span class="icon">←</span> Back</button> <button tabindex="-1" class="win95-btn"><span class="icon">→</span> Forward</button> <button tabindex="-1" class="win95-btn"><span class="icon">↑</span> Up</button> </div> <div class="tree-view"> <ul ngTree #tree="ngTree" class="retro-tree" [(values)]="selected" [multi]="true"> <ng-template [ngTemplateOutlet]="treeNodes" [ngTemplateOutletContext]="{nodes: nodes, parent: tree}" /> </ul> </div> <div class="status-bar"> <div class="status-panel status-panel-grow">{{selectedCount()}} object(s) selected</div> <div class="status-panel status-panel-right">4.5MB</div> </div></div><ng-template #treeNodes let-nodes="nodes" let-parent="parent"> @for (node of nodes; track node.value) { <li ngTreeItem [parent]="parent" [value]="node.value" [label]="node.name" [(expanded)]="node.expanded" [disabled]="node.disabled" #treeItem="ngTreeItem" > <span aria-hidden="true"> @if (node.children) { {{treeItem.expanded() ? '📂' : '📁'}} } @else { 📄 } </span> {{ node.name }} <span aria-hidden="true" class="material-symbols-outlined selected-icon" translate="no">check</span> </li> @if (node.children) { <ul role="group"> <ng-template ngTreeItemGroup [ownedBy]="treeItem" #group="ngTreeItemGroup"> <ng-template [ngTemplateOutlet]="treeNodes" [ngTemplateOutletContext]="{nodes: node.children, parent: group}" /> </ng-template> </ul> } }</ng-template>
CSS
@import url('https://fonts.googleapis.com/icon?family=Material+Symbols+Outlined');@import url('https://fonts.googleapis.com/css2?family=Jersey+20&display=swap');:host { display: flex; justify-content: center; user-select: none; --win95-gray: #c0c0c0; --win95-dark-gray: #808080; --win95-light: #ffffff; --win95-shadow: #000000; --win95-blue: #000080; --win95-active-blue: linear-gradient(to right, #000080, #1084d0); --win95-font: "Jersey 20", sans-serif; font-family: var(--win95-font); font-size: 1.1rem;}.win95-file-explorer { border-style: solid; border-width: 1px; border-color: var(--win95-light) var(--win95-dark-gray) var(--win95-dark-gray) var(--win95-light); box-shadow: 1px 1px 0 var(--win95-shadow); background-color: var(--win95-gray); min-width: 350px;}.win95-btn { background-color: var(--win95-gray); padding: 3px 8px; cursor: pointer; border-style: solid; border-width: 1px; border-color: var(--win95-light) var(--win95-dark-gray) var(--win95-dark-gray) var(--win95-light); box-shadow: 1px 1px 0 var(--win95-shadow);}.win95-btn:active { box-shadow: 1px 1px 0 var(--win95-shadow) inset; padding: 4px 7px 2px 9px; border-style: solid; border-width: 1px; border-color: var(--win95-dark-gray) var(--win95-light) var(--win95-light) var(--win95-dark-gray);}.title-bar { background: var(--win95-active-blue); color: var(--win95-light); padding: 3px 6px; height: 20px; display: flex; justify-content: space-between; align-items: center;}.title-bar-text { font-weight: bold;}.title-bar-controls button { background: var(--win95-gray); border: 1px solid var(--win95-light); color: var(--win95-shadow); width: 16px; height: 14px; line-height: 8px; font-size: 14px; padding: 0; margin-left: 1px; border-style: solid; border-width: 1px; border-color: var(--win95-light) var(--win95-dark-gray) var(--win95-dark-gray) var(--win95-light); box-shadow: 1px 1px 0 var(--win95-shadow);}.title-bar-controls button span { display: block; margin-top: -3px;}.menu-bar { display: flex; background-color: var(--win95-gray); border-bottom: 1px solid var(--win95-dark-gray); padding: 1px 2px;}.menu-item { padding: 0 6px; cursor: default; margin-right: 4px;}.menu-item:hover { background-color: var(--win95-blue); color: var(--win95-light);}.toolbar { display: flex; align-items: center; padding: 4px; border-top: 1px solid var(--win95-light); border-bottom: 1px solid var(--win95-dark-gray);}.toolbar .win95-btn { display: flex; align-items: center; margin-right: 8px;}.toolbar .icon { font-size: 1.25rem; line-height: 1; margin-right: 4px;}.tree-view { background-color: var(--win95-light); min-height: 300px; padding: 4px; border-width: 1px; border-style: solid; border-color: var(--win95-dark-gray) var(--win95-light) var(--win95-light) var(--win95-dark-gray); color: var(--win95-shadow);}.status-bar { margin-top: 2px;}.status-panel-grow { flex-grow: 1;}.status-panel-right { text-align: right;}.status-bar { height: 18px; background-color: var(--win95-gray); border-top: 1px solid var(--win95-light); border-left: 1px solid var(--win95-light); border-right: 1px solid var(--win95-dark-gray); border-bottom: 1px solid var(--win95-dark-gray); display: flex; font-size: 14px;}.status-panel { padding: 0 4px; height: 100%; display: flex; align-items: center; margin-right: 1px; border-style: solid; border-color: var(--win95-dark-gray) var(--win95-light) var(--win95-light) var(--win95-dark-gray); border-width: 1px; flex-grow: 1;}.status-panel:last-child { flex-grow: 0; width: 120px;}[ngTree] { padding: 0; margin: 0;}[ngTreeItem] { cursor: pointer; list-style: none; text-decoration: none; display: flex; align-items: center; gap: 0.75rem; padding: 0.25rem 0.75rem;}[ngTreeItem][aria-disabled='true'] { opacity: 0.5; cursor: default;}[ngTreeItem]:focus,[ngTreeItem]:hover { background-color: var(--senary-contrast); color: var(--primary-contrast); outline: none;}[ngTreeItem][aria-selected='true'] { background-color: var(--win95-blue); color: var(--win95-light);}.selected-icon { visibility: hidden; margin-left: auto;}[ngTreeItem][aria-current] .selected-icon,[ngTreeItem][aria-selected='true'] .selected-icon { visibility: visible;}li[aria-expanded='false'] + ul[role='group'] { display: none;}
ツリーに[multi]="true"を設定します。ユーザーはSpaceキーで個別にアイテムを選択するか、Shift+矢印キーで範囲を選択します。
フォーカスに追従する選択
選択がフォーカスに追従する場合、フォーカスされたアイテムは自動的に選択されます。これにより、ナビゲーションシナリオでのインタラクションが簡素化されます。
TS
import {Component, signal} from '@angular/core';import {NgTemplateOutlet} from '@angular/common';import {Tree, TreeItem, TreeItemGroup} from '@angular/aria/tree';type TreeNode = { name: string; value: string; children?: TreeNode[]; disabled?: boolean; expanded?: boolean;};@Component({ selector: 'app-root', templateUrl: 'app.component.html', styleUrl: 'app.component.css', imports: [Tree, TreeItem, TreeItemGroup, NgTemplateOutlet],})export class App { readonly nodes: TreeNode[] = [ { name: 'public', value: 'public', children: [ {name: 'index.html', value: 'public/index.html'}, {name: 'favicon.ico', value: 'public/favicon.ico'}, {name: 'styles.css', value: 'public/styles.css'}, ], expanded: true, }, { name: 'src', value: 'src', children: [ { name: 'app', value: 'src/app', children: [ {name: 'app.component.ts', value: 'src/app/app.component.ts'}, {name: 'app.module.ts', value: 'src/app/app.module.ts', disabled: true}, {name: 'app.css', value: 'src/app/app.css'}, ], expanded: false, }, { name: 'assets', value: 'src/assets', children: [{name: 'logo.png', value: 'src/assets/logo.png'}], expanded: false, }, { name: 'environments', value: 'src/environments', children: [ { name: 'environment.prod.ts', value: 'src/environments/environment.prod.ts', expanded: false, }, {name: 'environment.ts', value: 'src/environments/environment.ts'}, ], expanded: false, }, {name: 'main.ts', value: 'src/main.ts'}, {name: 'polyfills.ts', value: 'src/polyfills.ts'}, {name: 'styles.css', value: 'src/styles.css', disabled: true}, {name: 'test.ts', value: 'src/test.ts'}, ], expanded: false, }, {name: 'angular.json', value: 'angular.json'}, {name: 'package.json', value: 'package.json'}, {name: 'README.md', value: 'README.md'}, ]; readonly selected = signal(['angular.json']);}
HTML
<ul ngTree #tree="ngTree" [(values)]="selected" selectionMode="follow" class="basic-tree"> <ng-template [ngTemplateOutlet]="treeNodes" [ngTemplateOutletContext]="{nodes: nodes, parent: tree}" /></ul><ng-template #treeNodes let-nodes="nodes" let-parent="parent"> @for (node of nodes; track node.value) { <li ngTreeItem [parent]="parent" [value]="node.value" [label]="node.name" [disabled]="node.disabled" [(expanded)]="node.expanded" #treeItem="ngTreeItem" > <span aria-hidden="true" class="material-symbols-outlined expand-icon" translate="no">{{node.children ? 'chevron_right' : ''}}</span > <span aria-hidden="true" class="material-symbols-outlined" translate="no">{{node.children ? 'folder' : 'docs'}}</span > {{ node.name }} <span aria-hidden="true" class="material-symbols-outlined selected-icon" translate="no">check</span> </li> @if (node.children) { <ul role="group"> <ng-template ngTreeItemGroup [ownedBy]="treeItem" #group="ngTreeItemGroup"> <ng-template [ngTemplateOutlet]="treeNodes" [ngTemplateOutletContext]="{nodes: node.children, parent: group}" /> </ng-template> </ul> } }</ng-template>
CSS
@import url('https://fonts.googleapis.com/icon?family=Material+Symbols+Outlined');:host { display: flex; justify-content: center; user-select: none; font-family: var(--inter-font);}[ngTree] { min-width: 24rem; background-color: var(--septenary-contrast); border-radius: 0.5rem; padding: 0.5rem;}[ngTreeItem] { cursor: pointer; list-style: none; text-decoration: none; display: flex; align-items: center; gap: 1rem; padding: 0.3rem 1rem;}[ngTreeItem][aria-disabled='true'] { opacity: 0.5; cursor: default;}[ngTreeItem]:focus,[ngTreeItem]:hover { background-color: var(--quinary-contrast);}[ngTreeItem]:focus { outline: 1px solid color-mix(in srgb, var(--hot-pink) 60%, transparent);}[ngTreeItem][aria-selected='true'],[ngTreeItem][aria-selected='true'] .expand-icon { background-image: var(--pink-to-purple-horizontal-gradient); background-clip: text; color: transparent;}.material-symbols-outlined { margin: 0; width: 24px;}.expand-icon { transition: transform 0.2s ease;}[ngTreeItem][aria-expanded='true'] .expand-icon { transform: rotate(90deg);}.selected-icon { visibility: hidden; margin-left: auto;}[ngTreeItem][aria-current] .selected-icon,[ngTreeItem][aria-selected='true'] .selected-icon { visibility: visible;}li[aria-expanded='false'] + ul[role='group'] { display: none;}
TS
import {Component, computed, signal} from '@angular/core';import {NgTemplateOutlet} from '@angular/common';import {Tree, TreeItem, TreeItemGroup} from '@angular/aria/tree';type TreeNode = { name: string; value: string; children?: TreeNode[]; disabled?: boolean; expanded?: boolean;};@Component({ selector: 'app-root', templateUrl: 'app.component.html', styleUrl: 'app.component.css', imports: [Tree, TreeItem, TreeItemGroup, NgTemplateOutlet],})export class App { readonly nodes: readonly TreeNode[] = [ { name: 'C:', value: 'C:', expanded: true, children: [ { name: 'Program Files/', value: 'C:/Program Files', children: [ {name: 'Common Files', value: 'C:/Program Files/Common Files'}, {name: 'Internet Explorer', value: 'C:/Program Files/Internet Explorer'}, ], expanded: false, }, { name: 'Users/', value: 'C:/Users', children: [ {name: 'Default', value: 'C:/Users/Default'}, {name: 'Public', value: 'C:/Users/Public'}, ], expanded: false, }, { name: 'Windows/', value: 'C:/Windows', children: [ {name: 'System32', value: 'C:/Windows/System32'}, {name: 'Web', value: 'C:/Windows/Web'}, ], expanded: false, }, {name: 'pagefile.sys', value: 'C:/pagefile.sys'}, {name: 'swapfile.sys', value: 'C:/swapfile.sys', disabled: true}, ], }, ]; readonly selected = signal([]); readonly selectedCount = computed(() => this.selected().length);}
HTML
<div class="win95-file-explorer"> <div class="title-bar"> <div class="title-bar-text">Exploring - (C:)</div> <div class="title-bar-controls"> <button tabindex="-1" aria-label="Minimize"><span>-</span></button> <button tabindex="-1" aria-label="Maximize"><span>□</span></button> <button tabindex="-1" aria-label="Close"><span>×</span></button> </div> </div> <div class="menu-bar"> <div class="menu-item"><u>F</u>ile</div> <div class="menu-item"><u>E</u>dit</div> <div class="menu-item"><u>V</u>iew</div> <div class="menu-item"><u>T</u>ools</div> <div class="menu-item"><u>H</u>elp</div> </div> <div class="toolbar"> <button tabindex="-1" class="win95-btn"><span class="icon">←</span> Back</button> <button tabindex="-1" class="win95-btn"><span class="icon">→</span> Forward</button> <button tabindex="-1" class="win95-btn"><span class="icon">↑</span> Up</button> </div> <div class="tree-view"> <ul ngTree #tree="ngTree" class="retro-tree" selectionMode="follow" [(values)]="selected"> <ng-template [ngTemplateOutlet]="treeNodes" [ngTemplateOutletContext]="{nodes: nodes, parent: tree}" /> </ul> </div> <div class="status-bar"> <div class="status-panel status-panel-grow">{{selectedCount()}} object(s) selected</div> <div class="status-panel status-panel-right">4.5MB</div> </div></div><ng-template #treeNodes let-nodes="nodes" let-parent="parent"> @for (node of nodes; track node.value) { <li ngTreeItem [parent]="parent" [value]="node.value" [label]="node.name" [(expanded)]="node.expanded" [disabled]="node.disabled" #treeItem="ngTreeItem" > <span aria-hidden="true"> @if (node.children) { {{treeItem.expanded() ? '📂' : '📁'}} } @else { 📄 } </span> {{ node.name }} <span aria-hidden="true" class="material-symbols-outlined selected-icon" translate="no">check</span> </li> @if (node.children) { <ul role="group"> <ng-template ngTreeItemGroup [ownedBy]="treeItem" #group="ngTreeItemGroup"> <ng-template [ngTemplateOutlet]="treeNodes" [ngTemplateOutletContext]="{nodes: node.children, parent: group}" /> </ng-template> </ul> } }</ng-template>
CSS
@import url('https://fonts.googleapis.com/icon?family=Material+Symbols+Outlined');@import url('https://fonts.googleapis.com/css2?family=Jersey+20&display=swap');:host { display: flex; justify-content: center; user-select: none; --win95-gray: #c0c0c0; --win95-dark-gray: #808080; --win95-light: #ffffff; --win95-shadow: #000000; --win95-blue: #000080; --win95-active-blue: linear-gradient(to right, #000080, #1084d0); --win95-font: "Jersey 20", sans-serif; font-family: var(--win95-font); font-size: 1.1rem;}.win95-file-explorer { border-style: solid; border-width: 1px; border-color: var(--win95-light) var(--win95-dark-gray) var(--win95-dark-gray) var(--win95-light); box-shadow: 1px 1px 0 var(--win95-shadow); background-color: var(--win95-gray); min-width: 350px;}.win95-btn { background-color: var(--win95-gray); padding: 3px 8px; cursor: pointer; border-style: solid; border-width: 1px; border-color: var(--win95-light) var(--win95-dark-gray) var(--win95-dark-gray) var(--win95-light); box-shadow: 1px 1px 0 var(--win95-shadow);}.win95-btn:active { box-shadow: 1px 1px 0 var(--win95-shadow) inset; padding: 4px 7px 2px 9px; border-style: solid; border-width: 1px; border-color: var(--win95-dark-gray) var(--win95-light) var(--win95-light) var(--win95-dark-gray);}.title-bar { background: var(--win95-active-blue); color: var(--win95-light); padding: 3px 6px; height: 20px; display: flex; justify-content: space-between; align-items: center;}.title-bar-text { font-weight: bold;}.title-bar-controls button { background: var(--win95-gray); border: 1px solid var(--win95-light); color: var(--win95-shadow); width: 16px; height: 14px; line-height: 8px; font-size: 14px; padding: 0; margin-left: 1px; border-style: solid; border-width: 1px; border-color: var(--win95-light) var(--win95-dark-gray) var(--win95-dark-gray) var(--win95-light); box-shadow: 1px 1px 0 var(--win95-shadow);}.title-bar-controls button span { display: block; margin-top: -3px;}.menu-bar { display: flex; background-color: var(--win95-gray); border-bottom: 1px solid var(--win95-dark-gray); padding: 1px 2px;}.menu-item { padding: 0 6px; cursor: default; margin-right: 4px;}.menu-item:hover { background-color: var(--win95-blue); color: var(--win95-light);}.toolbar { display: flex; align-items: center; padding: 4px; border-top: 1px solid var(--win95-light); border-bottom: 1px solid var(--win95-dark-gray);}.toolbar .win95-btn { display: flex; align-items: center; margin-right: 8px;}.toolbar .icon { font-size: 1.25rem; line-height: 1; margin-right: 4px;}.tree-view { background-color: var(--win95-light); min-height: 300px; padding: 4px; border-width: 1px; border-style: solid; border-color: var(--win95-dark-gray) var(--win95-light) var(--win95-light) var(--win95-dark-gray); color: var(--win95-shadow);}.status-bar { margin-top: 2px;}.status-panel-grow { flex-grow: 1;}.status-panel-right { text-align: right;}.status-bar { height: 18px; background-color: var(--win95-gray); border-top: 1px solid var(--win95-light); border-left: 1px solid var(--win95-light); border-right: 1px solid var(--win95-dark-gray); border-bottom: 1px solid var(--win95-dark-gray); display: flex; font-size: 14px;}.status-panel { padding: 0 4px; height: 100%; display: flex; align-items: center; margin-right: 1px; border-style: solid; border-color: var(--win95-dark-gray) var(--win95-light) var(--win95-light) var(--win95-dark-gray); border-width: 1px; flex-grow: 1;}.status-panel:last-child { flex-grow: 0; width: 120px;}[ngTree] { padding: 0; margin: 0;}[ngTreeItem] { cursor: pointer; list-style: none; text-decoration: none; display: flex; align-items: center; gap: 0.75rem; padding: 0.25rem 0.75rem;}[ngTreeItem][aria-disabled='true'] { opacity: 0.5; cursor: default;}[ngTreeItem]:focus,[ngTreeItem]:hover { background-color: var(--senary-contrast); color: var(--primary-contrast); outline: none;}[ngTreeItem][aria-selected='true'] { background-color: var(--win95-blue); color: var(--win95-light);}.selected-icon { visibility: hidden; margin-left: auto;}[ngTreeItem][aria-current] .selected-icon,[ngTreeItem][aria-selected='true'] .selected-icon { visibility: visible;}li[aria-expanded='false'] + ul[role='group'] { display: none;}
ツリーに[selectionMode]="'follow'"を設定します。ユーザーが矢印キーでナビゲートすると、選択が自動的に更新されます。
無効化されたツリーアイテム
特定のツリーノードを無効にして、インタラクションを防ぎます。無効化されたアイテムがフォーカスを受け取れるかどうかを制御します。
TS
import {Component, signal} from '@angular/core';import {NgTemplateOutlet} from '@angular/common';import {Tree, TreeItem, TreeItemGroup} from '@angular/aria/tree';type TreeNode = { name: string; value: string; children?: TreeNode[]; disabled?: boolean; expanded?: boolean;};@Component({ selector: 'app-root', templateUrl: 'app.component.html', styleUrl: 'app.component.css', imports: [Tree, TreeItem, TreeItemGroup, NgTemplateOutlet],})export class App { readonly nodes: TreeNode[] = [ { name: 'public', value: 'public', children: [ {name: 'index.html', value: 'public/index.html'}, {name: 'favicon.ico', value: 'public/favicon.ico'}, {name: 'styles.css', value: 'public/styles.css'}, ], expanded: true, disabled: true, }, { name: 'src', value: 'src', children: [ { name: 'app', value: 'src/app', children: [ {name: 'app.component.ts', value: 'src/app/app.component.ts'}, {name: 'app.module.ts', value: 'src/app/app.module.ts', disabled: true}, {name: 'app.css', value: 'src/app/app.css'}, ], expanded: false, }, { name: 'assets', value: 'src/assets', children: [{name: 'logo.png', value: 'src/assets/logo.png'}], expanded: false, }, { name: 'environments', value: 'src/environments', children: [ { name: 'environment.prod.ts', value: 'src/environments/environment.prod.ts', expanded: false, }, {name: 'environment.ts', value: 'src/environments/environment.ts'}, ], expanded: false, }, {name: 'main.ts', value: 'src/main.ts'}, {name: 'polyfills.ts', value: 'src/polyfills.ts'}, {name: 'styles.css', value: 'src/styles.css', disabled: true}, {name: 'test.ts', value: 'src/test.ts'}, ], expanded: false, disabled: true, }, {name: 'angular.json', value: 'angular.json'}, {name: 'package.json', value: 'package.json'}, {name: 'README.md', value: 'README.md'}, ]; readonly selected = signal(['angular.json']);}
HTML
<ul ngTree #tree="ngTree" [(values)]="selected" class="basic-tree"> <ng-template [ngTemplateOutlet]="treeNodes" [ngTemplateOutletContext]="{nodes: nodes, parent: tree}" /></ul><ng-template #treeNodes let-nodes="nodes" let-parent="parent"> @for (node of nodes; track node.value) { <li ngTreeItem [parent]="parent" [value]="node.value" [label]="node.name" [disabled]="node.disabled" [(expanded)]="node.expanded" #treeItem="ngTreeItem" > <span aria-hidden="true" class="material-symbols-outlined expand-icon" translate="no">{{node.children ? 'chevron_right' : ''}}</span > <span aria-hidden="true" class="material-symbols-outlined" translate="no">{{node.children ? 'folder' : 'docs'}}</span > {{ node.name }} <span aria-hidden="true" class="material-symbols-outlined selected-icon" translate="no">check</span> </li> @if (node.children) { <ul role="group"> <ng-template ngTreeItemGroup [ownedBy]="treeItem" #group="ngTreeItemGroup"> <ng-template [ngTemplateOutlet]="treeNodes" [ngTemplateOutletContext]="{nodes: node.children, parent: group}" /> </ng-template> </ul> } }</ng-template>
CSS
@import url('https://fonts.googleapis.com/icon?family=Material+Symbols+Outlined');:host { display: flex; justify-content: center; user-select: none; font-family: var(--inter-font);}[ngTree] { min-width: 24rem; background-color: var(--septenary-contrast); border-radius: 0.5rem; padding: 0.5rem;}[ngTreeItem] { cursor: pointer; list-style: none; text-decoration: none; display: flex; align-items: center; gap: 1rem; padding: 0.3rem 1rem;}[ngTreeItem][aria-disabled='true'] { opacity: 0.5; cursor: default;}[ngTreeItem]:focus,[ngTreeItem]:hover { background-color: var(--quinary-contrast);}[ngTreeItem]:focus { outline: 1px solid color-mix(in srgb, var(--hot-pink) 60%, transparent);}[ngTreeItem][aria-selected='true'],[ngTreeItem][aria-selected='true'] .expand-icon { background-image: var(--pink-to-purple-horizontal-gradient); background-clip: text; color: transparent;}.material-symbols-outlined { margin: 0; width: 24px;}.expand-icon { transition: transform 0.2s ease;}[ngTreeItem][aria-expanded='true'] .expand-icon { transform: rotate(90deg);}.selected-icon { visibility: hidden; margin-left: auto;}[ngTreeItem][aria-current] .selected-icon,[ngTreeItem][aria-selected='true'] .selected-icon { visibility: visible;}li[aria-expanded='false'] + ul[role='group'] { display: none;}
TS
import {Component, computed, signal} from '@angular/core';import {NgTemplateOutlet} from '@angular/common';import {Tree, TreeItem, TreeItemGroup} from '@angular/aria/tree';type TreeNode = { name: string; value: string; children?: TreeNode[]; disabled?: boolean; expanded?: boolean;};@Component({ selector: 'app-root', templateUrl: 'app.component.html', styleUrl: 'app.component.css', imports: [Tree, TreeItem, TreeItemGroup, NgTemplateOutlet],})export class App { readonly nodes: readonly TreeNode[] = [ { name: 'C:', value: 'C:', expanded: true, children: [ { name: 'Program Files/', value: 'C:/Program Files', children: [ {name: 'Common Files', value: 'C:/Program Files/Common Files'}, {name: 'Internet Explorer', value: 'C:/Program Files/Internet Explorer'}, ], expanded: true, disabled: true, }, { name: 'Users/', value: 'C:/Users', children: [ {name: 'Default', value: 'C:/Users/Default'}, {name: 'Public', value: 'C:/Users/Public'}, ], expanded: false, }, { name: 'Windows/', value: 'C:/Windows', children: [ {name: 'System32', value: 'C:/Windows/System32'}, {name: 'Web', value: 'C:/Windows/Web'}, ], expanded: false, }, {name: 'pagefile.sys', value: 'C:/pagefile.sys'}, {name: 'swapfile.sys', value: 'C:/swapfile.sys', disabled: true}, ], }, ]; readonly selected = signal([]); readonly selectedCount = computed(() => this.selected().length);}
HTML
<div class="win95-file-explorer"> <div class="title-bar"> <div class="title-bar-text">Exploring - (C:)</div> <div class="title-bar-controls"> <button tabindex="-1" aria-label="Minimize"><span>-</span></button> <button tabindex="-1" aria-label="Maximize"><span>□</span></button> <button tabindex="-1" aria-label="Close"><span>×</span></button> </div> </div> <div class="menu-bar"> <div class="menu-item"><u>F</u>ile</div> <div class="menu-item"><u>E</u>dit</div> <div class="menu-item"><u>V</u>iew</div> <div class="menu-item"><u>T</u>ools</div> <div class="menu-item"><u>H</u>elp</div> </div> <div class="toolbar"> <button tabindex="-1" class="win95-btn"><span class="icon">←</span> Back</button> <button tabindex="-1" class="win95-btn"><span class="icon">→</span> Forward</button> <button tabindex="-1" class="win95-btn"><span class="icon">↑</span> Up</button> </div> <div class="tree-view"> <ul ngTree #tree="ngTree" class="retro-tree" [(values)]="selected"> <ng-template [ngTemplateOutlet]="treeNodes" [ngTemplateOutletContext]="{nodes: nodes, parent: tree}" /> </ul> </div> <div class="status-bar"> <div class="status-panel status-panel-grow">{{selectedCount()}} object(s) selected</div> <div class="status-panel status-panel-right">4.5MB</div> </div></div><ng-template #treeNodes let-nodes="nodes" let-parent="parent"> @for (node of nodes; track node.value) { <li ngTreeItem [parent]="parent" [value]="node.value" [label]="node.name" [(expanded)]="node.expanded" [disabled]="node.disabled" #treeItem="ngTreeItem" > <span aria-hidden="true"> @if (node.children) { {{treeItem.expanded() ? '📂' : '📁'}} } @else { 📄 } </span> {{ node.name }} <span aria-hidden="true" class="material-symbols-outlined selected-icon" translate="no">check</span> </li> @if (node.children) { <ul role="group"> <ng-template ngTreeItemGroup [ownedBy]="treeItem" #group="ngTreeItemGroup"> <ng-template [ngTemplateOutlet]="treeNodes" [ngTemplateOutletContext]="{nodes: node.children, parent: group}" /> </ng-template> </ul> } }</ng-template>
CSS
@import url('https://fonts.googleapis.com/icon?family=Material+Symbols+Outlined');@import url('https://fonts.googleapis.com/css2?family=Jersey+20&display=swap');:host { display: flex; justify-content: center; user-select: none; --win95-gray: #c0c0c0; --win95-dark-gray: #808080; --win95-light: #ffffff; --win95-shadow: #000000; --win95-blue: #000080; --win95-active-blue: linear-gradient(to right, #000080, #1084d0); --win95-font: "Jersey 20", sans-serif; font-family: var(--win95-font); font-size: 1.1rem;}.win95-file-explorer { border-style: solid; border-width: 1px; border-color: var(--win95-light) var(--win95-dark-gray) var(--win95-dark-gray) var(--win95-light); box-shadow: 1px 1px 0 var(--win95-shadow); background-color: var(--win95-gray); min-width: 350px;}.win95-btn { background-color: var(--win95-gray); padding: 3px 8px; cursor: pointer; border-style: solid; border-width: 1px; border-color: var(--win95-light) var(--win95-dark-gray) var(--win95-dark-gray) var(--win95-light); box-shadow: 1px 1px 0 var(--win95-shadow);}.win95-btn:active { box-shadow: 1px 1px 0 var(--win95-shadow) inset; padding: 4px 7px 2px 9px; border-style: solid; border-width: 1px; border-color: var(--win95-dark-gray) var(--win95-light) var(--win95-light) var(--win95-dark-gray);}.title-bar { background: var(--win95-active-blue); color: var(--win95-light); padding: 3px 6px; height: 20px; display: flex; justify-content: space-between; align-items: center;}.title-bar-text { font-weight: bold;}.title-bar-controls button { background: var(--win95-gray); border: 1px solid var(--win95-light); color: var(--win95-shadow); width: 16px; height: 14px; line-height: 8px; font-size: 14px; padding: 0; margin-left: 1px; border-style: solid; border-width: 1px; border-color: var(--win95-light) var(--win95-dark-gray) var(--win95-dark-gray) var(--win95-light); box-shadow: 1px 1px 0 var(--win95-shadow);}.title-bar-controls button span { display: block; margin-top: -3px;}.menu-bar { display: flex; background-color: var(--win95-gray); border-bottom: 1px solid var(--win95-dark-gray); padding: 1px 2px;}.menu-item { padding: 0 6px; cursor: default; margin-right: 4px;}.menu-item:hover { background-color: var(--win95-blue); color: var(--win95-light);}.toolbar { display: flex; align-items: center; padding: 4px; border-top: 1px solid var(--win95-light); border-bottom: 1px solid var(--win95-dark-gray);}.toolbar .win95-btn { display: flex; align-items: center; margin-right: 8px;}.toolbar .icon { font-size: 1.25rem; line-height: 1; margin-right: 4px;}.tree-view { background-color: var(--win95-light); min-height: 300px; padding: 4px; border-width: 1px; border-style: solid; border-color: var(--win95-dark-gray) var(--win95-light) var(--win95-light) var(--win95-dark-gray); color: var(--win95-shadow);}.status-bar { margin-top: 2px;}.status-panel-grow { flex-grow: 1;}.status-panel-right { text-align: right;}.status-bar { height: 18px; background-color: var(--win95-gray); border-top: 1px solid var(--win95-light); border-left: 1px solid var(--win95-light); border-right: 1px solid var(--win95-dark-gray); border-bottom: 1px solid var(--win95-dark-gray); display: flex; font-size: 14px;}.status-panel { padding: 0 4px; height: 100%; display: flex; align-items: center; margin-right: 1px; border-style: solid; border-color: var(--win95-dark-gray) var(--win95-light) var(--win95-light) var(--win95-dark-gray); border-width: 1px; flex-grow: 1;}.status-panel:last-child { flex-grow: 0; width: 120px;}[ngTree] { padding: 0; margin: 0;}[ngTreeItem] { cursor: pointer; list-style: none; text-decoration: none; display: flex; align-items: center; gap: 0.75rem; padding: 0.25rem 0.75rem;}[ngTreeItem][aria-disabled='true'] { opacity: 0.5; cursor: default;}[ngTreeItem]:focus,[ngTreeItem]:hover { background-color: var(--senary-contrast); color: var(--primary-contrast); outline: none;}[ngTreeItem][aria-selected='true'] { background-color: var(--win95-blue); color: var(--win95-light);}.selected-icon { visibility: hidden; margin-left: auto;}[ngTreeItem][aria-current] .selected-icon,[ngTreeItem][aria-selected='true'] .selected-icon { visibility: visible;}li[aria-expanded='false'] + ul[role='group'] { display: none;}
ツリーで[softDisabled]="true"の場合、無効化されたアイテムはフォーカスを受け取ることができますが、アクティブ化または選択できません。[softDisabled]="false"の場合、無効化されたアイテムはキーボードナビゲーション中にスキップされます。
API
Tree
階層的なナビゲーションと選択を管理するコンテナディレクティブです。
Inputs
| プロパティ | 型 | デフォルト | 説明 |
|---|---|---|---|
disabled |
boolean |
false |
ツリー全体を無効にします |
softDisabled |
boolean |
true |
trueの場合、無効化されたアイテムはフォーカス可能ですが、インタラクティブではありません |
multi |
boolean |
false |
複数アイテムの選択が可能かどうか |
selectionMode |
'explicit' | 'follow' |
'explicit' |
選択に明示的なアクションが必要か、フォーカスに追従するかどうか |
nav |
boolean |
false |
ツリーがナビゲーションモードであるかどうか(aria-currentを使用) |
wrap |
boolean |
true |
キーボードナビゲーションが最後のアイテムから最初のアイテムにラップするかどうか |
focusMode |
'roving' | 'activedescendant' |
'roving' |
ツリーで使用されるフォーカス戦略 |
values |
any[] |
[] |
選択されたアイテムの値(双方向バインディングをサポート) |
メソッド
| メソッド | パラメータ | 説明 |
|---|---|---|
expandAll |
none | すべてのツリーノードを展開します |
collapseAll |
none | すべてのツリーノードを折りたたみます |
selectAll |
none | すべてのアイテムを選択します(複数選択モードのみ) |
clearSelection |
none | すべての選択をクリアします |
TreeItem
子ノードを含むことができるツリー内の個々のノードです。
Inputs
| プロパティ | 型 | デフォルト | 説明 |
|---|---|---|---|
value |
any |
— | 必須。 このツリーアイテムの一意な値 |
disabled |
boolean |
false |
このアイテムを無効にします |
expanded |
boolean |
false |
ノードが展開されているかどうか(双方向バインディングをサポート) |
シグナル
| プロパティ | 型 | 説明 |
|---|---|---|
selected |
Signal<boolean> |
アイテムが選択されているかどうか |
active |
Signal<boolean> |
アイテムが現在フォーカスを持っているかどうか |
hasChildren |
Signal<boolean> |
アイテムが子ノードを持っているかどうか |
メソッド
| メソッド | パラメータ | 説明 |
|---|---|---|
expand |
none | このノードを展開します |
collapse |
none | このノードを折りたたみます |
toggle |
none | 展開状態を切り替えます |
TreeGroup
子ツリーアイテムのコンテナです。
このディレクティブには、入力、出力、メソッドはありません。子ngTreeItem要素を整理するためのコンテナとして機能します:
<li ngTreeItem value="parent"> Parent Item <ul ngTreeGroup> <li ngTreeItem value="child1">Child 1</li> <li ngTreeItem value="child2">Child 2</li> </ul></li>