npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@ivanheriver/treexplorer

v2.0.1

Published

a simple library to display a tree such as in a file explorer

Downloads

15

Readme

treexplorer

A simple tree view explorer for tree-like structured data. In a few words:

  • each item can be any HTML content you like. Defaults (text label, icon + text label) are provided if needed.
  • it supports keybord navigation: tab, arrow keys and enter key
  • it should properly support screenreaders but it hasn't been checked
  • you can customize the appearance to match the context it is used in
  • you can add listeners for when an item is selected
  • you can update the tree any time your data changes

You can checkout an example at https://ivanheriver.github.io/treexplorer/, which is the example located in the example folder of this repo.

How to install

Install the library using npm:

npm install @ivanheriver/treexplorer

And then you can import the treexplorer() function:

import { treexplorer } from "@ivanheriver/treexplorer";

You can also import default node HTML builders:

  • treexplorerLabelNode: a simple text label
  • treexplorerImageLabelNode: an image / icon followed by a text label

For example:

import {
  treexplorer,
  treexplorerImageLabelNode,
} from "@ivanheriver/treexplorer";

How to use

Checkout out the demo and associated code in the example folder for a full example.

The treexplorer function requires a config objects with the following structure:

type TXConfig<T> = {
  roots: T[] | T;
  getId: (o: T) => string;
  getChildren?: (o: T) => (T[] | null) | Promise<T[] | null>;
  getHTML?: (o: T) => HTMLElement;
  getIsInteractive?: (o: T) => boolean;
  hideRoots?: boolean;
  autoCollapseSiblings?: boolean;
};

where T is the type of a node in your data structure. You need to be able to define at least:

  • roots: either an array of T object or a single T object
  • getId: given a node item of type T returns a unique identifier for this node

It is also often needed to define

  • the getChildren function which given a node item of type T returns either null (if it is a leaf node) or an array of T objects. It can also be an async function.
  • the getHTML function which given a node item of type T, returns and HTMLElement to use as the HTML content of the node. You can use one of the predefined treexplorerLabelNode or treexplorerImageLabelNode builder functions which both come with treexplorer. They require to define function to get the label (and image src) given an item of type T much like getId, getChildren and getHTML functions.

Many examples on how to use the treexplorer are available in the file example/test.js.

Here is an example where each element of my tree data structured is supposed to have three components: id, label and children.

import {
  treexplorer,
  treexplorerImageLabelNode,
} from "@ivanheriver/treexplorer";

const tx = treexplorer({
  roots: treeRoots,
  getId: (o) => o.id,
  getChildren: (o) => o.children,
  getHTML: (o) => {
    const div = document.createElement("div");
    div.innerHTML = `
    <b>${o.label}</b><code>${o.id.substring(0, 8)}</code>
    `;
    div.style.display = "flex";
    div.style.gap = "1rem";
    return div;
  },
});
const container = document.querySelector(".treexplorer-container");
if (container) {
  container.appendChild(tx.HTML);
}

Customizing appearance

Chances are you'd like to modify the appearance of the tree view to match the context it used in. Some CSS class can be used to modify the appearance:

  • .treexplorer-main: only to set some CSS variable
    • --indent: indent size
    • --children-line-width: width of the line connecting children
    • --arrow-div-width: width of the arrow of parent nodes
  • .treexplorer-node: styling of each node, including
    • .treexplorer-node.selected for selected nodes
    • .treexplorer-node:hover for the appearance on hover
    • .treexplorer-node:focus for the appearance when focused
  • .treexplorer-trunk-line: to define the color of the vertical line connecting children

Here is an example:

.treexplorer-main {
  --indent: 1rem;
}

.treexplorer-node {
  border-top: 0.25rem solid transparent;
  border-bottom: 0.25rem solid transparent;
  background-color: rgb(155, 137, 255, 0.1);
}

.treexplorer-node.selected {
  background-color: rgb(155, 137, 255);
}

.treexplorer-node:hover {
  background-color: rgb(155, 137, 255, 0.5);
}

.treexplorer-node:focus-within,
.treexplorer-node:focus {
  outline: none;
  border-top: 0.25rem solid white;
  border-bottom: 0.25rem solid white;
}

.treexplorer-trunk-line {
  color: lightblue;
}