Data display

Tree View

A navigation tree of nested links whose branches open and close, with arrow-key navigation.

Class nameTypeDescription
tree-viewplf:tree-viewComponentThe tree's top list
tree-view-itemplf:tree-view-itemPartAn item: a row and an optional group
tree-view-linkplf:tree-view-linkPartA row: a link, or a span for a folder
tree-view-groupplf:tree-view-groupPartA nested list of items
tree-view-iconplf:tree-view-iconPartAn SVG icon before the label; an icon class there doesn't need it

Default

The tree needs aria-label or aria-labelledby, or it does not start.

Client-side routing

Following a link sends no Tree View event, so the script handles the link’s click and marks the link with setCurrent().

JavaScript

Tree View needs JavaScript. The Full script (pelatform-ui.min.js) starts every component on page load, with no code of your own; with ESM imports, call init() yourself, as in Installation. For markup you add later, see Dynamic content.

<link rel="stylesheet" href="pelatform-ui.min.css">
<link rel="stylesheet" href="icons/icons.min.css">
<script src="pelatform-ui.min.js" defer></script>
<link rel="stylesheet" href="pelatform-ui.prefixed.min.css">
<link rel="stylesheet" href="icons/icons.prefixed.min.css">
<script src="pelatform-ui.min.js" defer></script>
Attribute Purpose
data-plf-tree-view Marks the top <ul>, named with aria-label or aria-labelledby. Each <li> starts with an <a> row, or a <span> row for a branch without a page, then at most one nested <ul>
data-plf-expanded On an item, opens its branch at first
aria-current="page" On a row, the current page: the branches above it open and it takes the tab stop
aria-disabled="true" On a row, blocks opening it and following its link; leave out a disabled link’s href

The script sets the tree roles, aria-level, aria-setsize, aria-posinset, aria-expanded, a roving tabindex and hidden on closed groups, and adds a chevron with data-plf-part="icon tree-view-toggle" to each branch row, which opens or closes the branch without following its link; clicking a branch’s link also opens or closes it, and follows the link.

Methods

Method Behavior
PLF.TreeView.getOrCreateInstance(element) Returns the instance, creating it for valid markup
PLF.TreeView.getInstance(element) Returns the instance or null
instance.expand(item), instance.collapse(item), instance.toggle(item) Open, close or toggle a branch; return false when canceled, unchanged, disabled or not a branch
instance.reveal(item) Opens the item’s ancestors and gives it the tab stop without focusing it; returns false when an ancestor cannot open
instance.setCurrent(item) Moves aria-current="page" to the item’s row and reveals it, as after client-side navigation; returns as reveal()
instance.getState() Returns {expanded, active}: the open items and the item with the tab stop
instance.destroy() Removes listeners, the chevrons and the attributes the script set, so every group shows

Methods take an item or its row.

Events

Event When
plf:tree-view:before-toggle Before a branch opens or closes; cancelable
plf:tree-view:toggle After it opens or closes

Each event’s detail:

Property Value
instance The Tree View instance
item The branch’s <li>
expanded The new state: true when the branch opens, false when it closes
reason api, keyboard or pointer
trigger The clicked or focused row, or null from a method
Key Action
Down Arrow, Up Arrow Move to the next or previous visible row
Home, End Move to the first or last visible row
Right Arrow Opens a closed branch, or moves to its first child; reversed right to left
Left Arrow Closes an open branch, or moves to the parent; reversed right to left
Enter Follows a link; on a <span> row, opens or closes the branch
Space On a <span> row, opens or closes the branch
* Opens every branch at the current level
A character Moves to the next visible row that starts with it

React

TreeView from @pelatform/react renders this tree: it keeps its open branches, or the application sets them with expanded, and each part is a component of its own.

Prop Values Default
expanded The open items’ values, which the application changes in onExpandedChange; keeping them refuses the change Its own state
defaultExpanded The items open at first when the application does not set expanded; the branches above the current page open too None
onExpandedChange (expanded), called when a branch opens or closes None

The parts:

Part Element
TreeViewItem An li with tree-view-item; value names it in expanded
TreeViewLink A row with tree-view-link: a link with href, or a span for a folder; a branch’s row draws its chevron, aria-current marks the current page, which opens the branches above it, and aria-disabled="true" blocks the row
TreeViewGroup A ul with tree-view-group, the item’s branch

The other props are the element’s own, className and ref included.

Variable Default
--plf-tree-view-color Text
--plf-tree-view-gap Space 1 (4px) between rows
--plf-tree-view-hover-bg Muted surface
--plf-tree-view-current-bg --plf-color-primary-subtle, on the current page
--plf-tree-view-current-color --plf-color-primary-text, on the current page
--plf-tree-view-guide-color --plf-color-border-subtle, the line beside a nested group

Built with Pelatform UIv0.1.0

Components

Getting started

Foundations

Components

Actions

Data display

Navigation

Feedback

Data input

Layout

Advanced

Integrations

Blocks

Application

Authentication

Icons