1.1.4 β€’ Published 2 years ago

next-active-link v1.1.4

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

βœ… next-active-link standard-readme compliant

Active link component for Next.JS

πŸ”– Description

next/link wrapper which automatically add the provided class to the link element.

πŸ“¦ Installation

# Install the component
$ npm install next-active-link

πŸš€ Usage

The usage is the same as the next/link component. More information in Official docs

The differences are that the next-active-link component has the following props activeClassName and activeMatchOptions .

🟒 ActiveClassName

(Component prop) The class name to be added to the link element when the link is active.

const activeClassName: string = 'active-link'

🟒 ActiveMatchOptions

(Component prop) The options to be used to match the link.

/**
 *  You have the following options:
 */
  const activeMatchOptions_Option_1 = {
    exact: true | false
  }

  const activeMatchOptions_Option_2 = {
    paths: 'exact' | 'partial'        // optional
    queryParams: 'exact' | 'partial'  // optional
    fragment: 'exact'                 // optional
  }

🟒 onActiveChange

(Component method prop) The callback to be called when the link active state changes.

  const onActiveChange: (isActive: boolean) => {
    // ...
  }

πŸ€“ Example

import { ActiveLink } from 'next-active-link';

  function MyPage() {
  /**
   * Adds 'active-link' className to "a" tag
   * if the current page is '/contact'
  */
  return (
    <ActiveLink
      href="/contact"
      activeClassName="active-link"
      onActiveChange={(isActive) => console.log(isActive)}
      {/* Default options */}
      activeMatchOptions={{
        exact: true
      }}
    >
      <a>Contact</a>
    </ActiveLink>
  )


  /** ------------------------------------------------------ */

  function MyPage() {
  /**
   * Adds 'active-link' className to "a" tag
   * if the current page is '/any-page?name=Chempo'
   * (has the exact query params)
  */
  return (
    <ActiveLink
      href="/contact?name=Chempo"
      activeClassName="active-link"
      onActiveChange={(isActive) => console.log(isActive)}
      activeMatchOptions={{
        queryParams: 'exact'
      }}
    >
      <a>Contact</a>
    </ActiveLink>
  )
}

πŸ€“ Happy Code

Created with Typescript! ⚑ and latin music 🎺🎡

1.1.4

2 years ago

1.1.3

2 years ago

1.1.2

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago