Interactive Tree View In Vanilla JavaScript – vanillatree

Category: Javascript , Recommended | October 21, 2026
Authorfinom
Last UpdateOctober 21, 2026
LicenseMIT
Views7,563 views
Interactive Tree View In Vanilla JavaScript – vanillatree

vanillatree is a simple, collapsible interactive tree view component with context menu support, implemented in vanilla JavaScript.

How to use it:

1. Install the vanillatree package and import it into your JS project.

# NPM
$ npm install vanillatree
import VanillaTree from 'vanillatree';
import 'vanillatree/vanillatree.css';

2. Or directly load the vanillatree’s JavaScript and CSS files from a CDN.

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/vanillatree@1/dist/vanillatree.css">
<script src="https://cdn.jsdelivr.net/npm/vanillatree@1/dist/vanillatree.min.js"></script>

3. Create a container where you want to present the tree view.

<div id="main"></div>

4. Initialize the tree view and set the right-click context menu as follows:

const tree = new VanillaTree('#main', {
  placeholder: 'Placeholder text',
  contextmenu: [
    {
      label: 'Menu 1',
      action: function(id) {
        alert('Menu 1 ' + id);
      }
    }, {
      label: 'Menu 2',
      action: function(id) {
        alert('Menu 2 ' + id);
      }
    },
    // ... more menus here...
  ],
  theme: 'auto', // or 'light', 'dark',
});

5. Add custom nodes to the tree view.

tree.add({
  label: 'Label A',
  id: 'a',
  opened: true
});
tree.add({
  label: 'Label B',
  id: 'b'
});
tree.add({
  label: 'Label A.A',
  parent: 'a',
  id: 'a.a',
  opened: true,
  selected: true,
});
tree.add({
  label: 'Label A.A.A',
  parent: 'a.a',
  selectable: false,
});
tree.add({
  label: 'Label A.A.B',
  parent: 'a.a'
});
tree.add({
  label: 'Label B.A',
  parent: 'b',
  html: true,
});

6. API methods.

// add new nodes
tree.add(options);
// move a node to parent
tree.move(id, parentId);
// remove a node
tree.remove(id);
// open a node
tree.open(id);
// close a node
tree.close(id);
// toggle a node
tree.toggle(id);
// select a node
tree.select(id);
// destroy the instance
tree.destroy();
// get/set data
tree.getSelected()
tree.getParent(id)
tree.getPath(id)
tree.hasChildren(id)
tree.getLeaf(id, notThrow)
tree.getChildList(id)

7. Available events.

main.addEventListener('vtree-add', function(evt) {
  // when added
});
main.addEventListener('vtree-remove', function(evt) {
  // when closed
});
main.addEventListener('vtree-move', function(evt) {
  // when moved
});
main.addEventListener('vtree-open', function(evt) {
  // when opened
});
main.addEventListener('vtree-close', function(evt) {
  // when closed
});
main.addEventListener('vtree-select', function(evt) {
  // when selected
});

Changelog:

10/21/2026

  • Rewritten in TypeScript.

You Might Be Interested In:


One thought on “Interactive Tree View In Vanilla JavaScript – vanillatree”

  1. Frank Ni

    Hi,

    Thank you for the excellent codes!

    Right click on the vanillatree, the contextmenu always pops up at xpos = 0. Should be right under where the mouse is clicked.

    Again, great codes!

    Thank you!

    Reply ↓

Leave a Reply