Easily create sortable, draggable and collapsible trees — vanilla TypeScript, lightweight and no dependencies.
You can find a detailed description of all available configuration options in the official Readme on GitHub. Alternatively continue reading here and check out some example code below.
Preparing Node Data
A sortable tree is generated based on an
recursive array of tree nodes. Every node
contains two properties — a
and a nested array of
nodes that inherit the
same structure and therefore build a recursive tree.
data object can contain any number of
custom properties. The names of those properties don't
matter at all. The shape is freely defined by the
application and the developer.
The data object can be accessed by the render function in order to define the content and behavior of each node.
First, it is required to included the
sortable-tree.css into your bundle in order to
handle the basic tree functionality — this will not
apply any other styling but folding
Now the actual tree can be created. The most basic
configuration only requires a
array and a target
The actual clickable and draggable area — the label — is rendered by a fully customizable function that can be passed to the tree constructor.
Custom icons to indicate opened and collapsed nodes can be defined using the icons property. Any string, including HTML or SVG, can be used here as values.
It is possible to catch the drop event and wait for confirmation before actually moving things around.
Handling Change Events
You can define your own drop event handler in the tree constructor options.
Handling Click Events
Aside from drop events, it is also possible to define a custom handle for click events.
Finding and Revealing Nodes
You can search for nodes and interact with them. Click here in order to reveal the hidden node with the title "Deep Nested Page".
In case you just want to create a tree instance in your app that doesn't provide any sorting functionalty, you can easily disbale the drag-and-drop behavior.
By default, users are not allowed to move nodes next to the
root node on the top level. This is useful in order to
address use cases such as an actual file system tree that
can only have on root node. However, it is possible to
disable this restriction by setting the
lockRootLevel option to
By default, the
is required to be imported, only covers the most
basic functionality such as collapsing and indentation. You
can add your own custom CSS
classes to the rendered elements as well as fully customize
the render function. Without
any theming, the generated tree looks as follows. In
addition to that, the basic styles are controlled by
that allow for easy integration into other design systems.
The theme that is used here on this example page can be
found as a
.less file on
and serves as a good boiler plate for custom themes.