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

dynamic-react-navlinks

v2.0.14

Published

Generates dynamic navbar based on BrowserRouter Routes

Downloads

10

Readme

React Dynamic NavLinks

This package allows you to render dynamic NavLinks and a basic navbar from the Routes within a react-router-dom BrowserRouter component.

It will render the proper navigation link based on the path in the given route's props. Link text is generated from the path as well, but can be customized.

Usage

In your entrypoint:

Import the DynamicNavbar Component

import DynamicNavbar from "dynamic-react-navlinks"

Wrap your Routes within the DynamicNavbar HOC (Higher Order Component)

...

const rootElement = document.getElementById("root");
ReactDOM.render(
  <BrowserRouter>
        <DynamicNavbar>
          <Route exact path="/" component={App} />
          <Route path="/about" component={About} />
          <Route path="/contact" component={Contact} />
        </DynamicNavbar>
  </BrowserRouter>,
  rootElement
);

DynamicNavbar returns a navbar in its own div tag. The div has a className of "react-dynamic-navbar", and returns with NavLinks to each route. The routes returned will be as children to the React Router Dom Switch component. This package will exclude any routes with url params

This Route will be available for navigation, however the DynamicNavbar component will not create a link for it.

Note: It will not render links for nested Routes

Styles

The DynamicNavbar and NavLinks render with a default style object. These styles can be kept, or they can be overwritten using props (listed below in the "Props" section). The default styles are:

Navbar:

defaultWrapperStyles = {
    display: "flex",
    justifyContent: "space-around",
    width: "100vw",
    alignItems: "center",
    height: "70px",
    marginBottom: "35px"
  };

NavLinks:

defaultLinkStyles = {
    textDecoration: "none",
    width: "100px",
    fontSize: "2rem",
    fontWeight: "500"
  };

Note on Styles: Styles are all inline.

Props

  1. DynamicNavbar
  • These Props directly relate to the Navbar layout.

  • navbarStyles

  • Use the navbarStyles prop to overwrite the defaults. Pass in an object with standard JSX syntax styles.

    <DynamicNavbar navbarStyles={{ backgroundColor: 'black' }} />

  1. Route Component
  • Route Component Props have effect on the NavLinks that are returned in your dynamic navbar.

  • linkText Prop (optional):

  • Pass linkText prop to use a custom link name instead of the auto generated text (this defaults to a formatted version of the path).

  • The above code would create a default NavLink of 'Blog-detail'. Passing in the "linkText" prop allows you to define what text to use for that particular link.

  • If no linkText prop is passed for a Route with "/" as its path, the link will default to "Home" Ex:

  • linkStyles Prop (optional):

  • Pass the linkStyles prop to the Route component with an object of JSX styles.

    <Route exact path="/" linkStyles={{ fontSize: "3rem", color: "white"}} component={Home} />

  • This will overwrite the default linkStyles given by the HOC.

Props built in to the Route component are still fully available.

activeClass Prop (optional):

  • The activeClass prop works with react-router-dom's NavLink activeClassName prop.
  • By default, NavLinks come with the "active" className when active.
  • Passing the activeClass prop allows you to define a custom name.

Note: NavLinks here are returned with a className of "dynamic-nav-link". The activeClass appends to that class list: "dynamic-nav-link active".

To make new links, simply add a new Route component to your Router.

Standard BrowserRouter functionality is persisted. Route guards, render props, etc work the same as normal.

Thanks for using React Dynamic Navlinks!