VirtualTree (legacy)

VirtualTree (legacy)

VirtualTree is a legacy migration surface: a Rust-only scroll viewport for a large, already-expanded tree. It flattens the whole data tree into one list of (node, depth) pairs, sizes a spacer to rows × item_height, and renders only the rows inside the scroll window plus overscan extra rows above and below — each pinned at top: index × item_height with padding-left: depth × 24px. packages/vue/src ships nothing comparable; the nearest Vue list virtualiser is a different component family.

  • It does not fold. Every node is always expanded — flatten recurses into every child unconditionally, and the component owns no open set, no chevron and no on_toggle. It is a windowing device, not a tree control.
  • For a real tree with folding, selection and a keyboard cursor use Tree; for a JSON payload use JsonTree; for the legacy per-node markup use TreeNode.

Import

rust
1
2
use hikari_components::*;          // VirtualTree, VirtualTreeNodeData, VirtualScrollComponent
use hikari_components::prelude::*; // VirtualTreeProps, rsx!, VNode

VirtualTree is a plain function, VirtualTree(VirtualTreeProps { … }). VirtualTreeNodeData (the data type) is not in the prelude — take it from the crate root. Inside rsx! every prop may be omitted except data: height defaults to "400px", item_height to 32, overscan to 5, on_scroll to None.

The window, and how it moves

height is a CSS string, not a number: the component keeps it verbatim for the viewport and also parses it with parse::<f64>() to compute how many rows fit (container_height / item_height, rounded up). Only a unitless string parses, so "400px" — the default — falls back to 400.0 and a host that writes "60vh" or "24rem" silently gets the same 400px window height, and a "240px" window still believes it is 400px tall. The spacer is sized from the flattened row count, so the scrollbar reflects the whole tree even though only a window of rows exists in the DOM.

Scrolling is the interesting part: the position is a use_signal(0.0) and the onscroll handler that writes it is compiled only for wasm32 (#[cfg(target_arch = "wasm32")]), because it reads the scroll offset through the platform helper get_scroll_top_by_selector(".hk-virtual-tree-viewport"). On any other target the handler body is empty — the signal never moves, the window never advances, and the snapshot always shows the first overscan-padded window. on_scroll carries a f64 and fires only on the wasm path.

Rust
App
API
Worker
Docs

The snapshot is a still: with no wasm scroll event the window sits at offset 0, and the two rows below the visible slot are simply not built.

Rows, depth and the stylesheet that does not match

A row is a div carrying the same class as the root — hk-virtual-tree (the VirtualScrollClass::VirtualTree token) — plus an inline absolute position and a data-key attribute holding the node id; inside it a span.hk-tree-node-content holds the title. Root and rows therefore share one class name and are distinguishable only by the root's inline overflow: hidden; height: … and by data-key being absent on the root. Depth reaches only the inline padding-left, so nesting is visual. disabled adds hk-tree-node-disabled — a class name the stylesheet does not know (see below) — and does not stop the row from rendering: there is no click handler on a row at all, so a disabled node is indistinguishable from an enabled one except for that inert class.

The row lookup is worth knowing before putting this in front of a big tree: for each visible row the component searches the whole flattened list for the node's id (position(|(n, _)| n.id == item.id)) to recover its index — an O(n) scan per visible row on every render, so the cost grows with the tree, which is exactly what the component is supposed to avoid.

Rust
Root
Child
Disabled child
Root
Child
Disabled child

Sheet mismatch (source defect, visible in the snapshot): the stylesheet this component ships (styles/components/virtual-scroll.scss, imported by styles/index.scss) styles .hk-virtual-scroll, .hk-virtual-scroll-item and .hk-virtual-scroll-content — the names a different component used — while this component renders hk-virtual-tree / hk-virtual-tree-viewport. None of the sheet's rules match, and the root's hk-virtual-tree class name also lands on every row; spacers, absolute positioning and all row geometry come only from the inline styles. Treat the rendering as final-state markup rather than as the sheet above.

Behaviour the caller should plan for

  • Every node is visible. There is no way to filter, fold or hide a branch; to show a partial tree, hand in a pruned data.
  • The window is sized before the data is known. container_height is computed once from height, outside the memos, so changing height at runtime does not re-derive the visible count.
  • The scroll signal is target-gated. A non-wasm host (the server-rendered docs snapshot included) renders window zero forever — that is why this page has no interaction example: there is nothing to interact with off-browser.
  • Offsets are per-node, not per-window. The visible rows are rendered from their absolute index in the flattened list, so the slice is a window of all rows starting near the scroll offset — the overscan rows above the start are included on every render, which is the point, but it also means a tree shorter than the window renders in full (which is what every snapshot here shows).
  • No loading or empty state. An empty data renders the shell with a zero-height spacer; the sheet's .hk-virtual-scroll-empty / -loading rules are for the other class names and never appear.
Rust
Row 0
Row 1
Row 2
Row 3
Row 4
Row 5
Row 6
Row 7
Row 8
Row 9
Row 10
Row 11
Row 0
Row 1
Row 2
Row 3
Row 4
Row 5
Row 6
Row 7
Row 8
Row 9
Row 10
Row 11

API

Rust

Props — VirtualTree

PropTypeDefaultDescription
dataVec<VirtualTreeNodeData>[]—
heightString"400px".to_string()—
item_heightu3232—
overscanu325—
classString""—
on_scrollOption<EventHandler<f64>>None—

Accessibility

  • The root is not a named region. The root is a plain div.hk-virtual-tree with no role="tree", no aria-label, no aria-multiselectable; the rows likewise carry no role="treeitem" and no aria-level. What a screen reader gets is a run of divs containing text, with the nesting expressed only by padding — and the rows share the root's class name, so even a host-side selector cannot tell the two apart without data-key.
  • The viewport is not a scroll region it can enter. The scroller is div.hk-virtual-tree-viewport with overflow-y: auto and no tabindex, so it is not a focus stop; a keyboard user cannot scroll it with the arrow keys unless the browser decides to, and rows that the window has not rendered are not in the accessibility tree at all — the announced content changes as the pointer user scrolls.
  • No row is focusable and nothing is selected or expanded. There is no aria-selected, aria-expanded or aria-disabled anywhere, and disabled is carried by an inert class name, so a disabled node is announced exactly like an enabled one.
  • The spacer (position:absolute; height: rows × item_height; pointer-events: none) is an empty div; it is harmless to a reader but it is the only element that tells the page how long the list is, and it is not exposed.
  • Because only the visible window exists in the DOM, find-in-page and screen-reader "read all" see a fraction of the list. For an accessible large list the host needs its own controls (a filter, a pager) rather than a pure virtualiser.

Related components

  • Tree — the supported tree with folding, selection and a keyboard cursor.
  • TreeNode — the legacy node markup this component flattens.
  • JsonTree — the JSON viewer, with a real role="tree" root.
  • DragDropTree — the legacy reorder surface over the same tree data idea.