Tailwind CSS Tree View Plugin API
Headless, unstyled tree view plugin built with JavaScript and TypeScript, including installation, usage, options, methods, events, and selectors.
Installation
To get started, install Tree View plugin via npm, else you can skip this step if you are already using Preline UI as a package.
-
Install the plugin
Install
@preline/tree-viewvia npmTerminalnpm i @preline/tree-view @preline/accordion -
Add the plugin CSS
Use @source to register the plugin's JavaScript path for Tailwind CSS scanning, then @import the plugin's CSS files into your Tailwind CSS file.
main.css@import "tailwindcss"; /* @preline/accordion */ /* [!code highlight:2] */ @source "../node_modules/@preline/accordion/*.js"; @import "./node_modules/@preline/accordion/variants.css"; /* @preline/tree-view */ /* [!code highlight:2] */ @source "../node_modules/@preline/tree-view/*.js"; @import "./node_modules/@preline/tree-view/variants.css"; /* [!code highlight:1] */ @import "./node_modules/@preline/tree-view/theme.css"; -
Add the plugin JavaScript
Include the JavaScript
<script>that powers the interactive elements near the end of your</body>tag:HTML
Additional Initialization Options
Use the
non-autoentry if you need manual initialization. Tree View depends on Accordion for expand/collapse behavior — callHSAccordion.autoInit()first so all accordion nodes inside the tree are initialized automatically, then initialize the specific Tree View element manually.HTML (non-auto)
Via bundler
When using a bundler (Vite, webpack, etc.), import the plugin directly as an ES module. Initialize
HSAccordionfirst — it powers the expand/collapse nodes inside the tree.JavaScript (auto)import "@preline/accordion"; import "@preline/tree-view";JavaScript (non-auto)import HSAccordion from "@preline/accordion/non-auto"; import HSTreeView from "@preline/tree-view/non-auto"; HSAccordion.autoInit(); HSTreeView.autoInit(); // Or initialize a specific element manually const el = document.querySelector("#tree-view"); if (el) new HSTreeView(el);
Methods
The HSTreeView object is contained within the global window object
Update element.
const treeView = HSTreeView.getInstance('#tree-view', true);
const updateBtn = document.querySelector('#update-btn');
updateBtn.addEventListener('click', () => {
treeView.element.update();
});
Get selected items.
const treeView = HSTreeView.getInstance('#tree-view', true);
const getItemsBtn = document.querySelector('#get-items-btn');
getItemsBtn.addEventListener('click', () => {
console.log(treeView.element.getSelectedItems());
});
Events
When select changes event example.
const el = HSTreeView.getInstance('#tree-view', true);
el.element.on('click', (data) => {...});
Ready to use Components
Looking for prebuilt UI components based on the Tailwind CSS? Preline UI packs hundreds of component examples for all your website needs.
Tree View
Explore ready-to-use Tailwind CSS examples built with Preline UI.
View Tree View examples