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

react-navlink

v1.2.4

Published

A customizable NavLink component for React.js

Downloads

141

Readme

NavLink Component

NavLink is a customizable and accessible React component for navigation. It provides active link detection and flexible redirection logic with support for internal and external navigation, custom styling, and dynamic content rendering.

Installation

To install the NavLink component in your project, you can run the following command:

npm install react-navlink

or if you're using bun:

bun add react-navlink

Usage

Here's an example of how to use the NavLink component in your React project:

import React from 'react';
import NavLink from 'react-navlink';

const ExampleComponent = () => (
  <nav>
    <NavLink to="/home" activeClassName="active" inactiveClassName="inactive">
      Home
    </NavLink>
    <NavLink to="/about" activeClassName="active" inactiveClassName="inactive" matchMode="exact">
      About Us
    </NavLink>
    <NavLink to="/contact" isExternal={true}>
      Contact
    </NavLink>
  </nav>
);

export default ExampleComponent;

Props

| Prop | Type | Default | Description | | ----------------- | ---------------------------- | ------------- | ---------------------------------------------------------------------------------------------------- | | to | string | | URL or path to navigate to. Supports internal and external navigation. | | redirection | boolean | true | Determines if redirection should occur when the link is clicked. | | id | string | undefined | Unique identifier for the link element. | | children | ReactNode \| (isActive: boolean) => ReactNode | | Content or function to render within the link. Supports dynamic content rendering. | | inActiveClassName| string | '' | CSS class applied when the link is inactive. | | className | string | '' | Base className applied to the link. | | activeClassName | string | 'active' | CSS class applied when the link is active. | | onClick | function | undefined | Optional click event handler. | | matchMode | 'exact' \| 'startsWith' \| 'includes' | 'includes' | Determines how the active state is detected based on the current URL. | | replace | boolean | false | Replaces the current entry in the history stack if set to true. | | isExternal | boolean | false | If true, the component will behave as an external link. | | aria | object | {} | Additional ARIA attributes for accessibility. | | testId | string | undefined | Test identifier for easier testing in tools like Jest and Cypress. | | disabled | boolean | false | Disables the link, preventing navigation and styling it as inactive. | | activeStyle | React.CSSProperties | undefined | Inline styles applied when the link is active. | | inactiveStyle | React.CSSProperties | undefined | Inline styles applied when the link is inactive. | | customActiveUrl | string | undefined | Allows for custom URLs to be matched as active, instead of using the to prop. |

Prop Usage Examples

  1. Basic Link
<NavLink to="/home">Home</NavLink>
  1. Exact Match Mode
<NavLink to="/about" matchMode="exact">About Us</NavLink>
  1. Dynamic Children
<NavLink to="/dashboard">
  {(isActive) => <span>{isActive ? 'Active Dashboard' : 'Dashboard'}</span>}
</NavLink>
  1. External Link
<NavLink to="https://example.com" isExternal>Visit Example</NavLink>

Customization

  • CSS Classes: You can pass custom class names to style the active and inactive states using activeClassName and inActiveClassName.
  • Inline Styles: If needed, inline styles can be applied using the activeStyle and inactiveStyle props.
  • Custom Active URL: Use customActiveUrl to match a URL path different from the to prop.

Accessibility

This component is built with accessibility in mind:

  • Supports ARIA attributes through the aria prop.
  • aria-disabled is automatically applied when the link is disabled.

License

This project is licensed under the MIT License - see the LICENSE file for details.