Tree
A hierarchy of files, pages or choices, its branches opening on their children — files in folders, pages in a docs sidebar. Start with nested items; add selection, checkboxes, links or children loaded on first open as you need them.
Usage
import { Component, ViewEncapsulation } from '@angular/core';import { ScFrame, ScTree, ScTreeGroup, ScTreeGroupContent, ScTreeItem, ScTreeItemIndicator, ScTreeItemLabel,} from '@semantic-components/ui';import { SiChevronRightIcon, SiFileIcon, SiFolderIcon,} from '@semantic-icons/lucide-icons';
/** Nested parts: a branch holds a group of items; every row its indicator, so the names line up. */@Component({ selector: 'app-tree-usage-demo', imports: [ ScFrame, ScTree, ScTreeGroup, ScTreeGroupContent, ScTreeItem, ScTreeItemIndicator, ScTreeItemLabel, SiChevronRightIcon, SiFileIcon, SiFolderIcon, ], template: ` <div scFrame class="w-64 p-2"> <ul scTree aria-label="Project files"> <li scTreeItem value="src"> <div scTreeItemLabel> <span scTreeItemIndicator><svg siChevronRightIcon></svg></span> <svg siFolderIcon></svg> src </div> <ul scTreeGroup> <ng-template scTreeGroupContent> <li scTreeItem value="src/index.ts"> <div scTreeItemLabel> <span scTreeItemIndicator> <svg siChevronRightIcon></svg> </span> <svg siFileIcon></svg> index.ts </div> </li> <li scTreeItem value="src/utils.ts"> <div scTreeItemLabel> <span scTreeItemIndicator> <svg siChevronRightIcon></svg> </span> <svg siFileIcon></svg> utils.ts </div> </li> </ng-template> </ul> </li> <li scTreeItem value="package.json"> <div scTreeItemLabel> <span scTreeItemIndicator><svg siChevronRightIcon></svg></span> <svg siFileIcon></svg> package.json </div> </li> </ul> </div> `, host: { class: 'flex w-full justify-center' }, encapsulation: ViewEncapsulation.None,})export class TreeUsageDemo {}Anatomy
A tree of items. An item's label is its row; an item holding a group is a branch, the group's content its children — rendered the first time it opens, then kept. Every part is a directive on your element; the items find their tree and their parent by their place. Children from data: a template of yours, stamped for each level with ngTemplateOutletInjector="outlet" (the Items as Data example).
import { ScTree, ScTreeItem, ScTreeItemLabel, ScTreeItemIndicator, ScTreeItemCheckbox, ScTreeGroup, ScTreeGroupContent,} from '@semantic-components/ui';<ul scTree aria-label="…"> <li scTreeItem value="…"> <div scTreeItemLabel> <span scTreeItemIndicator>…</span> <input type="checkbox" scCheckbox scTreeItemCheckbox /> … </div> <ul scTreeGroup> <ng-template scTreeGroupContent> <li scTreeItem value="…">…</li> </ng-template> </ul> </li></ul>Selection and checkboxes
mode: one item (single, the default), several (multiple), or none (none) — the selected values in[(value)].activation="automatic": moving the focus selects.- The open branches in
[(expanded)]. A press on a row opens or closes its branch; with[expandOnPress]="false"only its indicator does. checkable: checkboxes apart from the selection, in[(checked)]. A branch checks all it holds, half-ticked while only some is;checkedholds the outermost fully checked items (checkStrategy="trunk") — a branch stands for all it holds, opened or not —, or each item on its own (independent).current: a navigation tree — the page shown,aria-current="page", its rows links.
Keyboard
- The tree is one tab stop: the selected item, or the first. ↓ and ↑: the next and previous row shown; Home and End: the first and last; a letter: the next row starting with it.
- → opens a branch, or goes to its first child; ← closes it, or goes to its parent — mirrored right to left. Shift + * opens a row's siblings.
- Space or Enter selects; checks, in a checkable tree (Space); follows a link (Enter).
- Several: Shift + ↓ and Shift + ↑ extend the selection, Ctrl + A selects all; with the pointer, a click adds or removes one, Shift + click selects a range.
Accessibility
- A tree (WAI-ARIA, Angular Aria's pattern): name it. Each item says its level, its place among its siblings, whether it is open, selected or current — and, checkable, checked or mixed.
- The item takes the focus, not its row: a link's row is out of the tab order, a checkbox too, and hidden from screen readers (the item says it).
- Disabled items stay focusable, so they can be found and read;
[focusableWhenDisabled]="false"skips them. - Children loading: the item is busy (
[loading],aria-busy); say the result in a live region.
Examples
Items as Data
- app
- components
- components.json
- package.json
- README.md
import { NgTemplateOutlet } from '@angular/common';import { Component, ViewEncapsulation, signal } from '@angular/core';import { ScFrame, ScTree, ScTreeGroup, ScTreeGroupContent, ScTreeItem, ScTreeItemIndicator, ScTreeItemLabel,Multiple Selection
- Inbox
- Projects
- Archive
- Trash
0 selected
import { NgTemplateOutlet } from '@angular/common';import { Component, ViewEncapsulation, signal } from '@angular/core';import { ScFrame, ScTree, ScTreeGroup, ScTreeGroupContent, ScTreeItem, ScTreeItemIndicator, ScTreeItemLabel,Checkboxes
- Frontend
- Backend
Checked: styles
import { NgTemplateOutlet } from '@angular/common';import { Component, ViewEncapsulation, signal } from '@angular/core';import { ScCheckbox, ScFrame, ScTree, ScTreeGroup, ScTreeGroupContent, ScTreeItem, ScTreeItemCheckbox,Links
Showing: installation
import { NgTemplateOutlet } from '@angular/common';import { Component, ViewEncapsulation, signal } from '@angular/core';import { ScFrame, ScTree, ScTreeGroup, ScTreeGroupContent, ScTreeItem, ScTreeItemIndicator, ScTreeItemLabel,Disabled
- Current plans
- Legacy plans
import { Component, ViewEncapsulation, signal } from '@angular/core';import { ScField, ScFrame, ScLabel, ScSwitch, ScTree, ScTreeGroup, ScTreeGroupContent, ScTreeItem,Loading Children
- users
- orders
- archive
import { Component, ViewEncapsulation, signal } from '@angular/core';import { ScFrame, ScTree, ScTreeGroup, ScTreeGroupContent, ScTreeItem, ScTreeItemIndicator, ScTreeItemLabel,} from '@semantic-components/ui';Controller
- Account
- Notifications
- Billing
import { NgTemplateOutlet } from '@angular/common';import { Component, ViewEncapsulation, signal } from '@angular/core';import { ScButton, ScFrame, ScTree, ScTreeGroup, ScTreeGroupContent, ScTreeItem, ScTreeItemIndicator,RTL
- المستندات
- الصور
- ملاحظات.txt
import { Dir } from '@angular/cdk/bidi';import { NgTemplateOutlet } from '@angular/common';import { Component, LOCALE_ID, ViewEncapsulation, signal } from '@angular/core';import { ScFrame, ScTree, ScTreeGroup, ScTreeGroupContent, ScTreeItem, ScTreeItemIndicator,API reference
Each part takes class, merged over its look (cn: a later class wins). A row's size, spacing and corners are the style's (its menu item's).
| Part | Selector | Description |
|---|---|---|
ScTree | ul[scTree] | The tree (name it). [(value)], mode ('single', 'multiple', 'none'), activation ('manual', 'automatic'), [(expanded)], expandOnPress (true), checkable, [(checked)], checkStrategy ('trunk', 'independent'), current, disabled, focusableWhenDisabled (true). exportAs scTree: expandAll(), collapseAll(), reveal(value). |
ScTreeItem | li[scTreeItem] | An item: value (required), disabled, selectable (true; false: it only opens), label (for typeahead; its row's text by default), loading, failed. A branch when it holds a group. exportAs scTreeItem: expanded(), selected(), current(), branch(), level(), checkState(). |
ScTreeItemLabel | div[scTreeItemLabel], a[scTreeItemLabel] | Its row, pressed and styled; on an a, a link that Enter follows. data-selected, data-expanded, data-branch, data-current, data-disabled, data-loading, data-failed. |
ScTreeItemIndicator | span[scTreeItemIndicator] | Your chevron (pointing to the end): turned down when open, mirrored right to left; it opens or closes the branch even when the row doesn't. Unseen on a leaf: put one in every row. |
ScTreeItemCheckbox | input[type="checkbox"][scTreeItemCheckbox] | On an scCheckbox, in a checkable tree: ticked, empty or half-ticked from the tree; out of the tab order (Space on the item checks). |
ScTreeGroup | ul[scTreeGroup] | A branch's children, a group: indented, animated open and closed, hidden closed. |
ScTreeGroupContent | ng-template[scTreeGroupContent] | Inside the group: its items, rendered the first time the branch opens, then kept. |