TreeNode (legacy)

TreeNode (legacy)

TreeNode is a legacy migration surface: the Rust crate's hand-assembled tree row. One call renders a <li class="hk-tree-node"> — an arrow cell, an indented content row, a label — plus, while open, a nested <ul class="hk-tree-node-children"> of child TreeNodes built from its own data type. Its three inner pieces are separate components too: TreeNodeArrow is the open/close chevron, TreeNodeContent the indented clickable strip, TreeNodeLabel the icon-plus-text pair.

packages/vue/src ships no counterpart — the nearest Vue spelling is HkTree, which owns the same nested-row idea with a data array instead of nested component calls. New Rust code should use Tree; this page keeps existing pages building and documents what the four pieces actually do.

  • Not a mirror of Tree. TreeNode carries its own data type (TreeNodeData: key / label / children / disabled), owns its open state, has no selection state, no status paint, no icon type and no keyboard support. Tree is the controlled, Vue-mirroring replacement — see Tree for the deltas.
  • The three inner pieces have no pages of their own; they are documented here with the component that arranges them.
  • A JSON payload rendered as a tree is JsonTree; a very large flat-then-nested list is VirtualTree; reordering rows by pointer is DragDropTree.

Import

rust
1
2
use hikari_components::*;          // TreeNode, TreeNodeData, TreeNodeArrow, TreeNodeContent, TreeNodeLabel
use hikari_components::prelude::*; // TreeNodeProps, TreeNodeArrowProps, TreeNodeContentProps, TreeNodeLabelProps, rsx!, VNode

Each is a plain function taking its props struct: TreeNode(TreeNodeProps { … }), TreeNodeArrow(TreeNodeArrowProps { … }), TreeNodeContent(TreeNodeContentProps { … }), TreeNodeLabel(TreeNodeLabelProps { … }). All four props structs are in the prelude; the data type TreeNodeData is not, so a host that builds one imports it from the crate root. Every field of TreeNodeProps has a default except node_key and label, so inside rsx! those two are the only required props.

The row, and the state it keeps itself

TreeNode renders li with role="treeitem", data-node-key, data-level (always "0" — see the defect note below), and three ARIA strings written as attributes that the renderer emits with underscores, not dashes: aria_expanded (the open flag, or "false" for a leaf), aria_selected and aria_disabled — so no assistive technology sees them as ARIA (see the defect note below). It also adds hk-tree-node-parent when it was handed a node_children, plus hk-tree-node-selected / hk-tree-node-disabled from the flags.

Two source defects, reproduced in the snapshot: the three attributes are written with underscores. The rsx! attribute position does not convert a snake_case key to a dashed one, so the markup carries aria_expanded / aria_selected / aria_disabled, which assistive technology does not read — the component announces no state at all. TreeNodeArrow has the same problem with aria_hidden (rendered as aria_hidden="true"). Separately, data-level is written from a usize through "data-level": level, which the attribute writer emits as the string index "0" for every value; the real depth still reaches the indentation because level is also passed to TreeNodeContent as an integer. Neither is behaviour this page can promise — treat both as bugs to fix in the component, not as a contract.

The open flag is internal: expanded seeds a signal (use_signal(() => props.expanded)) and the arrow and the content row each flip that same signal — so a host can set the initial state but cannot read it back, and there is no event that reports a flip. level is only written to data-level; indentation is the job of the inner TreeNodeContent, which multiplies level by 24px.

The children are built from node_children, a Vec<TreeNodeData>, at level + 1 — a recursive call with the child's key, label, children and disabled. Note what the recursion does not carry: the parent's icon, expanded, selected and onclick are dropped, so a nested row is always closed until someone clicks it and never inherits the parent's press handler.

Rust
  • App
    • API
    • Worker
  • Docs

The snapshot is a still: expanded: true draws the open state and the press handlers are never run.

The three inner pieces

TreeNodeArrow is the chevron cell. Enabled, it renders span.hk-tree-node-arrow (plus hk-tree-node-arrow-expanded while open) holding a 14px Arrow — the crate's own arrow glyph, pointing down when open and right when closed — sets aria_hidden="true" on the span (rendered with the underscore, so it hides nothing), and stops propagation before calling its onclick, so the row handler never sees a chevron press. Disabled, it renders an empty span.hk-tree-node-arrow-placeholder instead and wires no handler at all, which is why a disabled row has no chevron to click.

TreeNodeContent is the row strip: div.hk-tree-node-content, padding-left: {level * 24}px, hi-tree-node-disabled when disabled (the hi- prefix is the source's own spelling, not a typo here), and its onclick gated on the same flag. TreeNodeLabel is a fragment of two spans — span.hk-tree-node-icon (only when an icon element was passed) and span.hk-tree-node-label with the text — so it contributes no wrapper of its own.

Reading the three together explains the prop wiring on TreeNode: the outer node hands TreeNodeContent an onclick that toggles the open signal (only for a parent) and then calls the host's onclick; and it hands TreeNodeArrow its own handler that toggles and stops propagation. A disabled node therefore swallows both — the content row checks !disabled before calling anything, and the arrow renders as the placeholder.

Rust
  • App
    • API
  • Archive

Using the pieces on their own

The three inner components are public and buildable on their own — the crate does not forbid it, and a host that wants a different row grammar (a heavier indent, a label without a chevron) can assemble TreeNodeContent + TreeNodeLabel itself. Two things to know before doing that:

  • TreeNodeContent.onclick and TreeNodeArrow.onclick are not optional: EventHandler<MouseEvent> without an Option, so an rsx! call must pass a handler bare — onclick: noop.clone(), not Some(…), which is the shape TreeNode itself hands them. Each type also has a hand-written Default that installs an empty handler, so ..Default::default() fills them, and a handler value is cloneable, so one closure can serve several calls (the example below does exactly that).
  • TreeNodeContent.children is optional, and TreeNodeLabel.label is required — a label row built without it does not compile.

The result is not a TreeNode any more: the parent class, data-node-key, data-level and the three ARIA attributes come from the outer <li>, so a hand-built row loses all of them and with them the little semantics the component had.

Rust
Node label

API

Rust

Props — TreeNode

PropTypeDefaultDescription
node_keyString""—
labelString""—
node_childrenOption<Vec<TreeNodeData>>None—
iconOption<Element>None—
disabledboolfalse—
expandedboolfalse—
selectedboolfalse—
levelusize0—
classString""—
onclickOption<EventHandler<MouseEvent>>None—

Props — TreeNodeArrow

PropTypeDefaultDescription
expandedboolfalse—
disabledboolfalse—
classStringString::new()—
onclickEventHandler<MouseEvent><code>EventHandler::new(|_| {})</code>—

Props — TreeNodeContent

PropTypeDefaultDescription
levelusize0—
disabledboolfalse—
classStringString::new()—
onclickEventHandler<MouseEvent><code>EventHandler::new(|_| {})</code>—
childrenElementVNode::empty()—

Props — TreeNodeLabel

PropTypeDefaultDescription
labelString""—
iconOption<Element>None—
classString""—

Accessibility

  • The li is role="treeitem" but its container is the host's markup: the component emits no role="tree" of its own, and the children ul is role="group" only while open. A host that nests nodes without a role="tree" ancestor produces an orphan item role — wrap the top level (and put aria-label on that wrapper, since nothing in TreeNode names the tree).
  • The state attributes are not read by assistive technology. aria-expanded / aria-selected / aria-disabled reach the DOM as aria_expanded / aria_selected / aria_disabled (verified in the rendered snapshot), and aria-hidden on the chevron likewise degrades to aria_hidden. So a screen reader sees a treeitem with no expanded, selected or disabled state, and the chevron glyph is exposed as an unlabelled inline element. Fixing the attribute keys is a component change, not a host setting.
  • aria-level is not written at all, and data-level — its stand-in — renders as "0" on every row (see the defect note above), so neither the announced structure nor the data attribute carries depth. Only the nesting and the 24px-per-level padding say where a row sits.
  • data-node-key is the only stable handle a host has on a row, because the component writes no id; aria-activedescendant-style wiring is impossible for the same reason.
  • No keyboard path exists. The li is not focusable and the content row is a plain div with a click handler: no tabindex, no key bindings, no roving cursor. A TreeNode tree can be opened only with a pointer. Tree adds exactly this (role="tree", tabindex="0", aria-activedescendant, arrow keys); migrating is the way to get keyboard access.
  • Disabled is announced nowhere (the aria-disabled attribute does not survive the underscore) and the row stays clickable in the DOM sense — the handler checks the flag rather than the browser: there is no disabled attribute and no removal from the Tab order (there is nothing in it anyway).

Related components

  • Tree — the supported both-language tree; the migration target for this component.
  • JsonTree — a JSON payload as an expandable tree.
  • VirtualTree — the legacy flattening tree for large data sets.
  • DragDropTree — the legacy pointer reorder surface for tree rows.