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

use-page-headings-tree

v1.0.8

Published

A React hook to get tree data from a list of heading nodes

Downloads

32

Readme

use-page-headings-tree

A React hook to get tree data from a list of heading nodes

NPM JavaScript Style Guide

Install


npm install --save use-page-headings-tree

Example Usage

Check out the example that uses the following code or check out the example source!

Get a list of heading nodes

useEffect(() => {
	const headingNodes = headingsContainerRef.current.querySelectorAll(
		"h2,h3,h4,h5,h6"
	);
	setPageHeadingNodes(headingNodes);
}, []);

Pass the node list, a callback to set the tree data and weather you would like the expanded attribute to be true or false by default

usePageHeadingsTree(pageHeadingNodes, setPageHeadingTree, false);

Use the tree data (recursion is the simplest method)

const renderNodeList = (node) => (
		<li key={node.id}>
			{node.text}
			{node.childNodes.length > 0 ? (
				<ul>{node.childNodes.map(renderNodeList)}</ul>
			) : null}
		</li>
	);
);

usePageHeadingsTree hook


  usePageHeadingsTree = (
    pageHeadingNodes: NodeList,
    callback: (treeNodes: TreeNode[]) => void,
    defaultToExpand: bool,
    ) => { ... }

Param List

  1. pageHeadingNodes: NodeList -> NodeList as returned by Element.querySelectorAll
  2. callback: (treeNodes: TreeNode[]) => void -> callback to set an array of TreeNodes (see below)
  3. defaultToExpand: bool -> default value for TreeNode.expanded on all tree nodes.

Sample TreeNode

{
  // the id from the header element (if any)
  // useful for generating a linked table of contents
  id: "my-fancy-header-id",

  // the text inside the header element
  text: "some header text",

  // expanded attribute
  // useful for creating expand/collapse table of contents
  // default for all nodes set as third argument of usePageHeadingsTree
  expanded: true,

  // the total number of nested children contained in any given node
  childrenCount: 3,

  // root id for node
  rootId: "generated unique id",

  // a copy of the heading element from the original NodeList
  element: heading,

  // a list of child nodes
  childNodes: [],

  // the index of the node from the original node list
  index: 6,
}

License

MIT © kbrock84


This hook is created using create-react-hook.