react-navlink
v1.2.2
Published
A customizable NavLink component for React.js
Downloads
14
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
- Basic Link
<NavLink to="/home">Home</NavLink>
- Exact Match Mode
<NavLink to="/about" matchMode="exact">About Us</NavLink>
- Dynamic Children
<NavLink to="/dashboard">
{(isActive) => <span>{isActive ? 'Active Dashboard' : 'Dashboard'}</span>}
</NavLink>
- 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
andinActiveClassName
. - Inline Styles: If needed, inline styles can be applied using the
activeStyle
andinactiveStyle
props. - Custom Active URL: Use
customActiveUrl
to match a URL path different from theto
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.