Tree Select
ProHierarchical picker in a body-portalled tree panel: single or multi-select, an ancestor-preserving filter with debounced server search, full keyboard control through aria-activedescendant, ControlValueAccessor support, and the loading / error / empty states inside the panel.
import { SvTreeSelectComponent } from '@styloviz-pro/forms/tree-select';Demo & usage
Basic usage
Usage example — open the gallery for the full interactive demo.
An interactive demo for this component lives in the gallery.
Open live demoMore examples
Bottom sheet on phones
Below 640px the tree opens as a bottom sheet (`createSheetMode`): full width, safe-area padded, riding the virtual keyboard. Off by default.
Multi-select
Restore expansion
Server-side search
Async states
Lazy branches
Mark an unfetched branch `children: []`. `loadChildren` runs once per branch on first expand, gets an `AbortSignal` that fires if the branch is collapsed mid-load, and a branch that failed is retried on the next expand.
Reactive forms
Signal Forms
Bind it with `[formField]` from `@angular/forms/signals` — the control is a ControlValueAccessor, which Signal Forms drives on Angular 21 and 22 alike.
When to use
Picking one node out of a hierarchy the user already thinks of as a tree — folders, org units, categories. A taxonomy too deep for a flat select but too shallow to need a full page. For a strictly level-by-level walk (country → state → city), the Cascader reads better; for browsing rather than picking, use the Tree View.
Async states
- loading
- empty
- error
- retry
Inputs
| Name | Type | Default | Description |
|---|---|---|---|
| options | readonly TreeSelectNode[] | [] | The tree to pick from. |
| loadChildren | ((node: TreeSelectNode, signal?: AbortSignal) => Promise<readonly TreeSelectNode[]>) | null | null | Fetch a branch's children the first time it is expanded. For a tree you cannot fetch whole — a permissions tree, a file browser — instead of fetching everything up front and feeding it in. The signature takes an `AbortSignal` because a branch the user has navigated away from must be cancellable — the same rule as `sv-cascader`, and both use `svKeyedRequestGuard`. Honour it: a tree browsed quickly opens and abandons branches faster than they return. It is called once per branch. A node that has been loaded is not asked again, and a node that FAILED is retried when the user expands it again — a failed branch that could never be retried would be a dead end with no way out but a page reload. Mark an unloaded branch with `children: []`. With a loader bound, an empty `children` array is a branch whose contents have not been fetched: it renders a chevron, `aria-expanded`, and expands (rather than selects) on click. A node with no `children` property at all is a leaf. Once loaded, a branch that came back empty is shown as a leaf. |
| loading | boolean | false | True while `options` are being fetched. |
| stale | boolean | false | The results on screen answer an **older** query — a newer request is still in flight. Distinct from `loading`, which cannot separate the two cases it covers: the first fetch (nothing on screen — show a skeleton) and a refinement of results already visible (keep them, and say they are provisional). Blanking the panel on the second is the most jarring thing a picker can do; showing the old list with no indication is a quiet lie. Bind `svSuggestController(...).stale` from `@styloviz/data`, or `createRequestGuard().stale` from `@styloviz/core`. The results dim and report `aria-busy` while staying fully interactive. |
| error | SvDataError | string | null | null | Load failure. Use `toSvDataError()` from `@styloviz/core` to normalize an `HttpErrorResponse` and get sensible retry behaviour. |
| emptyMessage | string | 'No options' | Copy for the no-data state. |
| value | string | null (two-way) | null | Selected value in single mode. `null` when nothing is chosen. |
| checkedValues | readonly string[] (two-way) | [] | Selected values in `multiple` mode. |
| expandedValues | readonly string[] (two-way) | [] | Values of the expanded branches. Bindable, so expansion survives a reload. |
| searchTerm | string (two-way) | '' | Current filter text. |
| multiple | boolean | false | Allow several nodes to be checked at once. |
| selectable | TreeSelectSelectable | 'leaf' | Which nodes a click can select. |
| filterable | boolean | true | Show the filter box at the top of the panel. |
| localFilter | boolean | true | Filter against the supplied tree in the browser. Turn it off when the host answers `search` with a fresh `options` tree — local filtering would then hide rows the server had just decided were matches. |
| debounceMs | number | 250 | Debounce applied to the `search` output, in ms. |
| clearable | boolean | true | Show the clear (×) button once something is selected. |
| disabled | boolean | false | Disable the whole control. |
| size | TreeSelectSize | 'md' | Trigger size. |
| placeholder | string | 'Select…' | Text shown in the trigger when nothing is selected. |
| filterPlaceholder | string | 'Filter…' | Placeholder for the filter box. |
| ariaLabel | string | '' | Accessible name for the trigger. |
| customClass | string | '' | Extra CSS classes applied to the root wrapper. |
| sheetOnNarrow | boolean | false | Below 640px, open the tree as a bottom sheet — full width, clear of the home indicator and of the virtual keyboard the filter box raises — rather than anchored to a trigger a phone has no room beside. The kit's shared contract (`createSheetMode`, as `sv-dropdown-menu`). |
Outputs
| Event | Payload type | Description |
|---|---|---|
| retry | void | The host should re-run the failed load. |
| nodeSelect | TreeSelectNodeEvent | Emitted when a node is selected or, in `multiple` mode, toggled. |
| search | string | Debounced filter text, for a host that searches server-side. |
| openChange | boolean | Emitted when the panel opens or closes. |
| valueChange | string | null | Emits the new `value` whenever it changes — the output half of `[(value)]`. |
| checkedValuesChange | readonly string[] | Emits the new `checkedValues` whenever it changes — the output half of `[(checkedValues)]`. |
| expandedValuesChange | readonly string[] | Emits the new `expandedValues` whenever it changes — the output half of `[(expandedValues)]`. |
| searchTermChange | string | Emits the new `searchTerm` whenever it changes — the output half of `[(searchTerm)]`. |
Wire it to your API
This component is headless about transport: it renders what you give it and reports what the user asks for. Nothing below requires @styloviz/data — drive it from whatever you already use.
Template
Bound to the names this component actually exposes.
Host class
Drive the three async inputs from your own fetch.
This component does not emit a query, so it renders what you give it. Call `load()` on your own trigger and bind `options`, `loading` and `error`. `debounceMs` (default 250) collapses a burst of typing into one request. Set it to 0 only when the query is driven by clicks. Normalize failures with `toSvDataError()` from `@styloviz/core`. It derives `retryable` from the status, so a 404 does not get a "Try again" button that can only fail again.
Full walkthrough: Connecting to a backend.
Customization & theming
Single or multiple selection. Leaf-only or any-node selection. Filter box, local or server-side, with a configurable debounce. Three trigger sizes, clearable, disabled.
Forms integration
sv-tree-select— ControlValueAccessor.[formControl],formControlName,[(ngModel)]and Signal Forms[formField]all work.
Best practices
Do: Leave `selectable` at `leaf` for a taxonomy, and switch to `any` for a folder picker where every level is a valid answer. Do: Bind `[(expandedValues)]` when the tree reloads — otherwise the user re-opens the same branches after every refresh. Do: Turn `localFilter` off when you answer `(search)` from the server; filtering locally as well would hide rows the server had just decided were matches. Don't: Do not put thousands of nodes behind it unfiltered — this panel renders every visible row, so use server-side search for a large tree. Tip: The panel is portalled to `<body>`, so it escapes `overflow: hidden` and transformed ancestors and is destroyed on close. Do: Expect tri-state ancestry in `multiple` mode: ticking a branch takes its enabled contents with it, and a branch completes once every child the user could tick is ticked. Don't: Don't expect a tick to reach a `disabled` descendant. It cannot, in either direction — a read-only row the user could not have selected must not arrive in your change event because its parent was ticked, and a parent tick must not clear one either. Do: Use `checkedLeafValues()` when your API wants only leaves. `checkedValues` keeps the branch ids because they are the complete answer to "what is selected" — and because dropping them loses the difference between "the whole folder" and "every file in it, which happens to be all of them", two statements that diverge the moment a file is added. Don't: Don't read the mixed state as a value. `aria-checked="mixed"` and the dash are presentation; `checkedValues` holds strings and there is no third thing to store — the same contract Checkbox documents. Do: Use `loadChildren` for a tree you cannot fetch whole: each branch fetches its children the first time it is expanded, instead of the whole tree up front. Do: Honour the `AbortSignal` your loader is handed. Collapsing a branch cancels its request: a user who opens a folder, gets bored and collapses it has said they do not want it any more, and without cancellation the response still lands and grafts children into a branch that is not on screen. Don't: Don't expect a failed branch to be a dead end. It is marked (`branchFailed`) and retried when the user expands it again — a failure with no way back but a page reload is worse than the failure.
Accessibility
The trigger is a `button` with `aria-haspopup="tree"`, `aria-expanded` and `aria-controls`; the panel is a `role="tree"` with `aria-multiselectable` in multi mode. Every row is a `role="treeitem"` carrying `aria-level`, `aria-posinset`, `aria-setsize`, `aria-selected` and — only where it can open — `aria-expanded`. Keyboard: ↑/↓ move, →/← expand, collapse or jump to the parent, Enter/Space select, Escape closes. DOM focus stays in the filter box and the active row is published through `aria-activedescendant`.
Feedback on Tree Select
Spotted a gap in this page, or something that didn't work as described? Comments are GitHub Discussions in the public styloviz/community repo, so posting needs a GitHub account.