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

gatsby-dynamical-navigation

v0.4.1

Published

Dynamical navigation plugin for gatsbyjs

Downloads

43

Readme

gatsby-dynamical-navigation

Dynamical navigation plugin for gatsbyjs.

Install

npm install --save gatsby-dynamical-navigation

How to use

gatsby-config.js

...
plugins: [
  ...`gatsby-dynamical-navigation`
],
...

How it works

The algorithm creates GraphQL nodes using files with .md/.mdx extensions, located in src/pages.

Support for other formats is under development

For example:

You have such a file structure in src/pages:

├── index.md
├── lectures
|   ├── index.md
|   ├── file1.md
|   ├── file2.md
|   ├── file3.md

The presence of the file index.md in the root of each folder is necessary for correctly compiling the full navigation of the application

Using frontmatter:

/index.md

---
title: Home
---

...Content...

/lectures/index.md

---
title: Lectures
---

...Content...

/lectures/file1.md

---
title: lecture 1
navTitle: HTML
order: 1
---

...Content...

/lectures/file2.md

---
title: lecture 2
navTitle: CSS
order: 2
---

...Content...

/lectures/file1.md

---
title: lecture 3
navTitle: JS
order: 3
---

...Content...

The title and navTitle fields are used as the text of links for navigation. Moreover, navTitle overrides title.

At least one of navTitle and title must be defined. Otherwise, the GraphQL node for this page will not be created.

order is optional. It is necessary to organize the links when displaying.

Using of custom paths starting from version 0.4.0

It's allowed use a navigation scheme that is not based on the file system in src/pages, but on your own logic (when field slug creates from frontmatter's attribute path):

---
title: some-title
path: /section/sub-section/page/
---

...content

In this case, path from frontmatter will be used to create the navigation element.

Navigation component API

Navigation is React component for rendering of navigation.

How to use

import { Navigation } from 'gatsby-dynamical-navigation';

...
<Navigation 
  root={some path} //vertex path of displayed navigation
  target= {some path} //bottom path of displayed navigation (usually location)
  loader={React component} // optional. Component that will be displayed until the navigation is loaded
/>
...

You can find an example here:

Site page with left side navigation

(Where root is /dictionaries/ and target is /dictionaries/html/1_html_introduction/)

This site on GitHub

loadNavigation API

If you prefer to create your own navigation instance, you can use this API.

How to use

import { loadNavigation } from 'gatsby-dynamical-navigation';

...
loadNavigation(navigation => {
  //some kind of code with navigation
})
...

How it used in the plugin

The loadNavigation allows loading navigation asynchronously.

navigation is cached. No repeat requests.

navigation is an array of the type:

[
  {
    path: string, //path to page
    title: string, //text for link (name)
    childrenSiteNavigation: { //array of child links
      title: string, //path to page
      path: string, //text for link (name)
      order: number | null, //useful for arranging links when rendering
      fields: {
        isRoot: true, //means it has child links
      } | null //or not
    }[]
  }
]

Only navigation items with children loaded.

Items without children loaded as children of others.

For this example navigation would be:

[
  {
    title: Home,
    path: '/',
    childrenSiteNavigation: [
      {
        title: Lectures,
        path: '/lectures/'
        order: null,
        fields: {
          isRoot: true,
        },
      },
    ],
  },
  {
    title: Lectures,
    path: '/lectures/'
    childrenSiteNavigation: [
      {
        title: HTML,
        path: '/lectures/file1/'
        order: 1,
        fields: null,
      },
      {
        title: CSS,
        path: '/lectures/file2/'
        order: 2,
        fields: null,
      },
      {
        title: JS,
        path: '/lectures/file3/'
        order: 3,
        fields: null,
      },
    ],
  },
]

GraphQL queries

You can use the GraphQL nodes as you like.

For example, to load all navigation items:

in page template

...

export const pageQuery = graphql`
    query queryName {
      allSiteNavigation {
        edges {
          node {
            title
            path
            order
            fields {
              isRoot
            }
          }
        }
      }
    }
  `