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

table-of-contents-tk

v1.0.3

Published

The TableOfContents component provides a dynamic, interactive table of contents for your content.

Downloads

5

Readme

table-of-contents

Descriptions

The TableOfContents component provides a dynamic, interactive table of contents for your content. It highlights the currently active section based on the user's scroll position and allows for smooth navigation through headings in your document.

Installation

First, install the package from npm:

npm install table-of-contents-tk

Or use yarn

yarn add install table-of-contents-tk

Usage

import React, { useRef } from 'react';
import TableOfContents from 'table-of-contents-tk';

const MyComponent = () => {
    const contentRef = useRef<HTMLDivElement>(null);

    return (
        <div>
            <TableOfContents
                contentRef={contentRef}
                offsetTop={10}
                activeColor="#03a9f4"
                defaultColor="black"
            />
            <div ref={contentRef}>
                <h1 id="section1">Section 1</h1>
                <p>Content of section 1...</p>
                <h2 id="section1-1">Section 1.1</h2>
                <p>Content of section 1.1...</p>
                <h2 id="section1-2">Section 1.2</h2>
                <p>Content of section 1.2...</p>
                <h1 id="section2">Section 2</h1>
                <p>Content of section 2...</p>
                <h2 id="section2-1">Section 2.1</h2>
                <p>Content of section 2.1...</p>
                <!-- More content -->
            </div>
        </div>
    );
};

export default MyComponent;

Props

| Prop | Description | Type | Default | |---------------|-------------------------------------------------------------------------------------------------|-----------------------------------------------|-----------| | contentRef | A reference to the content container where the headings are located. | React.RefObject<HTMLDivElement> | - | | offsetTop | An optional offset from the top of the viewport for calculating the active heading. | number | 0 | | activeColor | An optional color for the active heading link. | string | #03a9f4 | | defaultColor| An optional color for the non-active heading links. | string | black |

Style

To style the component, you can use the provided class names and apply custom CSS as needed Here is example:

.table-of-content-tk {
  /* your styles */
}

.table-of-content-tk nav ul {
  list-style: none;
  padding: 0;
}

.table-of-content-tk nav ul li {
  margin: 5px 0;
}

.table-of-content-tk nav ul li a {
  text-decoration: none;
}

Table of Contents Utility Functions

In addition to the TableOfContents component, this package provides utility functions for manually managing the table of contents (TOC) in your React applications. These functions can be used to customize the behavior of your TOC or to integrate it with other components or libraries.

Utility Functions

| Function | Parameters | Description | | -------------------- | --------------------------------------------------------------- | ------------------------------------------------------------------------------------- | | generateId | text: string, level: number, index: number | Generates a unique ID for a heading element based on its text, level, and index. | | renderHeadingsData | contentRef: React.RefObject<HTMLDivElement> | Extracts heading elements from the specified content container. | | firstHandle | headings: Heading[], offsetTop: number \| null \| undefined | Handles the initial page load by scrolling to the heading specified in the URL hash. | | handleScroll | headings: Heading[], offsetTop: number \| null \| undefined | Updates the active heading based on the user's scroll position. | | handleClick | id: string | Handles the click event for a heading link, smoothly scrolling to the target section. |

Parameters

| Parameter | Description | Type | | ------------ | ------------------------------------------------ | --------------------------------- | | text | The text content of the heading. | string | | level | The level of the heading. | number | | index | The index of the heading. | number | | contentRef | A reference to the content container. | React.RefObject<HTMLDivElement> | | headings | An array of heading objects. | Heading[] | | offsetTop | An optional offset from the top of the viewport. | number \| null \| undefined | | id | The ID of the heading to scroll to. | string |

Usage example

import React, { useRef, useEffect, useState } from 'react';
import { renderHeadingsData, firstHandle, handleScroll, handleClick } from 'table-of-contents-tk/lib/master';

const CustomTableOfContents = () => {
  const contentRef = useRef<HTMLDivElement>(null);
  const [headings, setHeadings] = useState<Heading[]>([]);
  const [activeId, setActiveId] = useState<string | null>(null);

  useEffect(() => {
    if (contentRef.current) {
      const newHeadings = renderHeadingsData(contentRef);
      setHeadings(newHeadings);
      const initialActiveId = firstHandle(newHeadings, 60);
      if (initialActiveId) {
        setActiveId(initialActiveId);
      }
    }
  }, [contentRef]);

  useEffect(() => {
    const onScroll = () => {
      const newActiveId = handleScroll(headings, 60);
      setActiveId(newActiveId);
    };

    window.addEventListener('scroll', onScroll);
    return () => {
      window.removeEventListener('scroll', onScroll);
    };
  }, [headings]);

  const onHeadingClick = (id: string) => {
    handleClick(id);
    setActiveId(id);
  };

  return (
    <div>
      <nav>
        <ul>
          {headings.map((heading) => (
            <li key={heading.id} style={{ paddingLeft: (heading.level - 1) * 10 }}>
              <a
                href={`#${heading.id}`}
                onClick={(e) => {
                  e.preventDefault();
                  onHeadingClick(heading.id);
                }}
                style={{ color: activeId === heading.id ? 'blue' : 'black' }}
              >
                {heading.text}
              </a>
            </li>
          ))}
        </ul>
      </nav>
      <div ref={contentRef}>
        <h1 id="section1">Section 1</h1>
        <p>Content of section 1...</p>
        <h2 id="section1-1">Section 1.1</h2>
        <p>Content of section 1.1...</p>
        <h2 id="section1-2">Section 1.2</h2>
        <p>Content of section 1.2...</p>
        <h1 id="section2">Section 2</h1>
        <p>Content of section 2...</p>
        <h2 id="section2-1">Section 2.1</h2>
        <p>Content of section 2.1...</p>
        <!-- More content -->
      </div>
    </div>
  );
};

export default CustomTableOfContents;