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

next-navlink

v1.3.0

Published

A customizable NavLink component for Next.js

Downloads

15

Readme

Next-NavLink

Next-NavLink is a customizable navigation link component for Next.js applications. I know what you're thinking: "Another NavLink component?" But hear me out... this one’s different. It lets you easily create navigation links with active states, conditional classes, and yes even support for different matching modes to determine the active state, among other features.

Table of Contents

Installation

npm install next-navlink

Or, if you’re more of a yarn person:

yarn add next-navlink

Or, if you’re feeling adventurous:

bun install next-navlink

Usage

Here's a basic example of how to use the NavLink component in your Next.js project:

import React from "react";
import NavLink from "next-navlink";

const Navbar = () => {
  return (
    <nav>
      <NavLink to="/home" activeClassName="active" className="nav-link">
        Home
      </NavLink>
      <NavLink to="/about" activeClassName="active" className="nav-link">
        About
      </NavLink>
      <NavLink
        to="/contact"
        activeClassName="active"
        className="nav-link"
        redirection={false}
      >
        Contact
      </NavLink>
    </nav>
  );
};

export default Navbar;

Props

The NavLink component accepts the following props:

| Prop | Type | Description | Default | | ------------------- | ------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------ | | children | React.ReactNode or (isActive: boolean) => React.ReactNode | The content to be rendered inside the link. It can be a React node or a function that returns a React node, allowing customization based on the active state. | - | | activeClassName | string | The class name to apply when the link is active. | active | | inActiveClassName | string | The class name to apply when the link is not active. | '' | | className | string | Additional class names to apply to the link. | '' | | to | string | The target path to navigate to when the link is clicked. | - | | redirection | boolean | If true, the link redirects to the specified path. If false, it renders a <span> instead, disabling the navigation. | true | | id | string | The id attribute to apply to the link element. | - | | onClick | () => void | A function to call when the link is clicked. | - | | matchMode | 'exact' \| 'includes' \| 'startsWith' | Determines the matching behavior for the active state. | 'includes' | | replace | boolean | If true, replaces the current history state instead of adding a new URL to the stack. | false | | scroll | boolean | If true, scrolls to the top of the page after navigation. | true | | prefetch | boolean | If true, prefetches the page in the background when the link is in the viewport. | true | | isExternal | boolean | If true, the link will open in a new tab and will be treated as an external link. | false | | aria | { [key: string]: string } | An object of ARIA attributes to apply to the link element. | - | | testId | string | A test ID to apply to the link element for testing purposes. | - | | disabled | boolean | If true, disables the link, preventing navigation and click events. | false | | activeStyle | React.CSSProperties | An object containing inline styles to apply when the link is active. | - | | inactiveStyle | React.CSSProperties | An object containing inline styles to apply when the link is inactive. | - | | customActiveUrl | string | The link will be considered active when the current pathname matches this URL. | - |

Examples

Using replace, scroll, and prefetch

Here’s how you can make the most of these props with NavLink:

<NavLink
  to="/about"
  activeClassName="active"
  className="nav-link"
  replace={true}
  scroll={false}
  prefetch={true}
>
  About Us
</NavLink>

Conditional Rendering

Want to customize the link content based on whether it’s active? Here’s how:

<NavLink to="/profile" className="nav-link">
  {(isActive) => (
    <span>{isActive ? "Your Profile (Active)" : "Your Profile"}</span>
  )}
</NavLink>

You even might want a link to be considered active based on a different URL than the one it actually points to.

Copy code
<NavLink
  to="/profile/edit"
  customActiveUrl="/profile"
  activeClassName="active"
  className="nav-link"
>
  Edit Profile
</NavLink>

Styling Links

You can style active and inactive links easily:

<NavLink
  to="/dashboard"
  activeClassName="text-bold text-primary"
  inActiveClassName="text-muted"
  className="nav-link"
>
  Dashboard
</NavLink>

Disabling Redirection

Need a link that doesn’t actually navigate for some reasons? No problem:

<NavLink to="/settings" redirection={false} className="nav-link">
  Settings
</NavLink>

Using matchMode

Exact Match

Want the link to be active only when the URL exactly matches? Use this:

<NavLink
  to="/log"
  matchMode="exact"
  activeClassName="active"
  className="nav-link"
>
  Log
</NavLink>

Starts With Match

Make the link active for any path that starts with the specified path:

<NavLink
  to="/blog"
  matchMode="startsWith"
  activeClassName="active"
  className="nav-link"
>
  Blog
</NavLink>

Includes Match (Default)

By default, NavLink matches if the path is included as a substring:

<NavLink to="/profile" activeClassName="active" className="nav-link">
  Profile
</NavLink>

Contributing

Contributions are welcome! If you have suggestions, bug reports, or feature requests, feel free to open an issue or submit a pull request.

License

License: MIT