Trees
- HTML/CSS:Dev Ready
- Web Component:True
- Layout:Desktop Only
Sections
Tree Group Header
- Tree Item
- Tree Branch
- Tree Item
- Tree Branch
- Tree Item
- Tree Item with a Really Really Long Name That Should Truncate
About Trees#
A tree is composed of two core elements .slds-tree
and .slds-tree__item
. The tree wrapper, the outer most parent ul
, will receive the class .slds-tree
. This class will be used for scoping a tree, which allows for particular styling based on states in which the tree may enter.
A tree will need helper classes added and removed to help structure the layout. Each child node list item needs an aria-level
attribute with its value being the number of levels deep it is nested to indicate the distinct grouping is nested within.
Whenever the tree has a nested group, the nested ul
element should receive the ARIA role group
. The parent li[role="treeitem"]
requires the aria-expanded
attribute to be applied. Toggling aria-expanded
to true
or false
will show or hide the child group
. The parent li[role="treeitem"]
also requires aria-label
to be applied and set to the tree items text value, this ensures child groups are announced to screen readers as you interact with that branch.
Whenever a role="treeitem"
node is selected, aria-selected="true"
needs to be applied to display the selected styles.
In our example, we are using a chevron icon on tree branches to help indicate to the user what action clicking the tree branch will perform, whether opening or closing it. The effect of rotating the icon 90° to indicate open/closed status is achieved by applying the ARIA attribute aria-expanded
to the treeitem
. aria-hidden="true"
and tabindex="-1"
must be placed on the toggle button.
Tree items can only contain text values, no actionable elements, apart from our toggle button, can be placed inside a Tree item.
Trees can only contain a single focusable tree item and tabindex="0"
must be placed on the li[role="treeitem]
that takes current focus. Every other actionable and non-actionable element must be made unfocused by adding tabindex="-1"
or removing tabindex
, respectively.
When implementing collapsed rows, we suggest showing the content DOM nodes within each collapsed row only once the row is expanded for performance reasons. You can additionally toggle the hidden row with slds-show
and slds-hide
if you intend to keep all of the content in the DOM.
You can add metatext (see: metatext state) to any tree item, which adds a smaller, second line of text below tree node labels to provide supplemental information (to provide users with added context, aid with identification/disambiguation). To add metatext, include an additional span
within the treeitem with the class slds-tree__item-meta
. We've added an additional parent span around the label/title and metatext to ensure the spacing works properly when metatext is included. If adding metatext to a tree item with child nodes (i.e. a branch), be sure to update the aria-label
to include the metatext. For example: aria-label="Tree Branch Label: Tree Branch Metatext"
Accessibility#
Markup#
role="tree"
is placed on theul
role="tree"
element also hasaria-labelledby
applied which points to the trees heading elementrole="treeitem"
is placed on the treeli
elementsaria-level
is applied totreeitem
elements to indicate their nesting deptharia-expanded
is applied totreeitem
elements that have child tree nodes. It is set totrue
orfalse
aria-label
is applied totreeitem
elements that have child tree nodes. Be sure to add any metatext to the label, if applicablearia-selected="true"
is applied totreeitem
elements that are selectedtabindex="0"
is applied to thetreeitem
that is in focusrole="group"
is applied to child tree node containers,ul
Interactions#
- Only a single action per tree item
- Only 1 focused item per Tree
- Actionable elements in a tree item are mouse only and should not be focusable, they should be presentational and should be hidden from screen readers and keyboard users
- Focus is placed on the entire
li[role="treeitem"]
. If that item has child items, focus must include those as well.
Keyboard Navigation#
- Clicking on a tree item creates a selection
Up
andDown
arrow keys move:focus
andaria-selected
. Previous selections are clearedRight
arrow key to expand collapsed node.Left
arrow key to collapse expanded node.Left
arrow key on an end child node, collapses the group and moves:focus
andaria-selected
to the parenttreeitem
Enter
performs the default action on an end tree item (if there is one).Ctrl
+Up
andCtrl
+Down
moves focus. Current selection is maintainedCtrl
+Space
will add or remove the currently focused tree item to the selection
Filtering#
When filtering a tree you should couple the tree with a search input. The search input will control the contents of the tree and as such should the attribute aria-controls
added to it. The value of the aria-controls
attribute should be the ID of the tree it controls. The search input should also be of type search
.
Upon typing in the input the tree should start filtering immediately, expanding all nodes that have matching tree items to display, and highlighting the search term in each of the matching items. The highlight is provided by wrapping the term in <mark />
elements.
Base#
Tree Group Header
- Tree Item
- Tree Branch
- Tree Item
- Tree Branch
- Tree Item
- Tree Item with a Really Really Long Name That Should Truncate
<div class="slds-tree_container">
<h4 class="slds-tree__group-header" id="treeheading">Tree Group Header</h4>
<ul aria-labelledby="treeheading" class="slds-tree" role="tree">
<li aria-level="1" role="treeitem" tabindex="0">
States#
Expanded Item#
Tree Group Header
- Tree Item
- Tree Branch
- Tree Item
- Tree Branch
- Tree Item
- Tree Item with a Really Really Long Name That Should Truncate
<div class="slds-tree_container">
<h4 class="slds-tree__group-header" id="treeheading">Tree Group Header</h4>
<ul aria-labelledby="treeheading" class="slds-tree" role="tree">
<li aria-level="1" role="treeitem" tabindex="0">
Item Selected#
Tree Group Header
- Tree Item
- Tree Branch
- Tree Item
- Tree Branch
- Tree Item
- Tree Item with a Really Really Long Name That Should Truncate
<div class="slds-tree_container">
<h4 class="slds-tree__group-header" id="treeheading">Tree Group Header</h4>
<ul aria-labelledby="treeheading" class="slds-tree" role="tree">
<li aria-level="1" role="treeitem">
Deeply Nested Branches#
Tree Group Header
- Tree Item
- Tree Branch
- Tree Item
- Tree Branch
- Tree Item
- Tree Item
- Tree Branch
- Tree Item
- Tree Branch
- Tree Item
- Another Tree Item with Really Really Long Text That Should Truncate
- Tree Item
- Tree Branch
- Tree Item
- Tree Item
- Tree Item
- Tree Item
- Tree Item
- Tree Branch
- Tree Item
- Tree Item with a Really Really Long Name That Should Truncate
<div class="slds-tree_container">
<h4 class="slds-tree__group-header" id="treeheading">Tree Group Header</h4>
<ul aria-labelledby="treeheading" class="slds-tree" role="tree">
<li aria-level="1" role="treeitem" tabindex="0">
Item with Metatext#
Tree Group Header
- Tree Item
- Tree Branch
- Tree Item:Tree Item Metatext with a Really Really Long Name That Should Truncate
- Tree Branch:Tree Branch Metatext
- Tree Item
- Tree Item with a Really Really Long Name That Should Truncate
<div class="slds-tree_container">
<h4 class="slds-tree__group-header" id="treeheading">Tree Group Header</h4>
<ul aria-labelledby="treeheading" class="slds-tree" role="tree">
<li aria-level="1" role="treeitem" tabindex="0">
Item Hovered#
Tree Group Header
- Tree Item
- Tree Branch
- Tree Item
- Tree Branch
- Tree Item
- Tree Item with a Really Really Long Name That Should Truncate
<div class="slds-tree_container">
<h4 class="slds-tree__group-header" id="treeheading">Tree Group Header</h4>
<ul aria-labelledby="treeheading" class="slds-tree" role="tree">
<li aria-level="1" role="treeitem" tabindex="0">
Items Disabled#
Tree Group Header
- Tree Item
- Tree Branch
- Tree Item
- Tree Branch
- Tree Item
- Tree Item with a Really Really Long Name That Should Truncate
<div class="slds-tree_container">
<h4 class="slds-tree__group-header" id="treeheading">Tree Group Header</h4>
<ul aria-labelledby="treeheading" class="slds-tree" role="tree">
<li aria-level="1" role="treeitem" tabindex="0">
Examples#
Filterable Tree#
Components
- lightning
- lightning-button
- lightning-checkbox-button
- lightning-radio-button
- lightning-toggle:variant a checkbox button
- ui
- ui:button
- ui:checkboxButton
- ui:radioButton
- lightningcommunities:Community components
- Tree Item
<div>
<div class="slds-m-bottom_small">
<div class="slds-form-element">
<label class="slds-form-element__label slds-assistive-text" for="filter-id">Filter navigation items</label>
Filtered Tree#
Components
- lightning
- lightning-button
- lightning-checkbox-button
- lightning-radio-button
- lightning-toggle:variant a checkbox button
- ui
- ui:button
- ui:checkboxButton
- ui:radioButton
<div>
<div class="slds-m-bottom_small">
<div class="slds-form-element">
<label class="slds-form-element__label slds-assistive-text" for="filter-id">Filter navigation items</label>
Overview of CSS Classes#
- Selector
- The CSS class being referred to.
- Summary
- A description of what the class does.
- Support
- Whether the class name is dev-ready (meaning it's fully vetted and tested and safe to use) or prototype (which means it's not fully vetted yet).
- Restrict
- The selector that the class name is allowed to be used on.
- Variant
- The base level pattern for a component. A variant can be extended to create another variant of that component, for example, a stateful button is a derivative of the base button.
- Modifier
- A single class that can be added to an HTML element of a component to modify its output. Typically these will be colors, sizing and positioning.
Selector | .slds-tree_container |
---|---|
Summary | A tree is visualization of a structure hierarchy. A branch can be expanded or collapsed. |
Support | dev-ready |
Restrict | div |
Variant | True |
Selector | .slds-tree |
---|---|
Summary | Initializes a slds tree |
Restrict | .slds-tree_container ul, table |
Selector | .slds-tree__item |
---|---|
Summary | Initializes a slds tree item |
Restrict | .slds-tree div |
Selector | .slds-is-selected Deprecated |
---|---|
Summary | Selected state for a tree item |
Restrict | .slds-tree__item |
Selector | .slds-is-focused Deprecated |
---|---|
Summary | Focus state for a tree item |
Restrict | .slds-tree__item |
Selector | .slds-is-hovered |
---|---|
Summary | Hover state for a tree item |
Restrict | .slds-tree__item |
Modifier | True |
Selector | .slds-is-disabled |
---|---|
Summary | When a branch doesn't have children, apply slds-is-disabled to the button icon |
Restrict | .slds-tree__item button |
Modifier | True |
Selector | .slds-tree__item-label |
---|---|
Summary | The label text of a tree item or tree branch |
Restrict | .slds-tree__item span |
Selector | .slds-tree__item-meta |
---|---|
Summary | The meta text or secondary text of a tree item |
Restrict | .slds-tree__item span |
Selector | [role="treeitem"] |
---|---|
Summary | Styles the focus and selected state for any tree item that has role="treeitem" |
Restrict | .slds-tree li |
Selector | .slds-tree__group-header |
---|---|
Summary | Initializes a slds tree group header |
Restrict | .slds-tree_container h4 |
Trees Release Notes
2.10.0
Added
- Added right-to-left specific selector to
slds-tree__item
to place thebox-shadow
in the correct location in right-to-left languages. - Added right-to-left specific selector to
slds-tree__item slds-button__icon
to allow the icon to rotate in the proper direction in right-to-left languages.
2.8.0
Added
- Two new examples of trees; filterable and filtered trees, with a search input. These show how you can use an input to filter the contents of a large tree and highlight the search term in matching items
Fixed
- Adjusted the alignment of the tree item text and chevron to be less reliant on button line height
2.7.0
Changed
- Removed
aria-selected
from the examples since this is only necessary if the tree supports multi-select - Removed
role="application"
from the top level tree
Fixed
- Replaced the
slds-size_1-of-1
surrounding the tree label withslds-has-flexi-truncate
to prevent the label from overflowing the tree's boundary