react-active-menu
TypeScript icon, indicating that this package has built-in type declarations

0.0.14 • Public • Published

react-active-menu

A React hook to build an active scrolling menu navigation.

Installation

npm i react-active-menu

Getting Started

Initialize hook:

const {
  activeId,
  registerContainer,
  registerSection,
  registerTrigger,
} = useActiveMenu();

Register triggers with unique IDs:

<button ref={registerTrigger('section-1')}>
  Section 1
</button>

Register sections with unique IDs:

<section ref={registerSection('section-1')}>
  <h2>Section 2</h2>
  <p>Lorem ipsum dolor sit amet.</p>
</section>

Register scrollable container:

<div ref={registerContainer}>
  <section ref={registerSection('section-1')}>
    <h2>Section 2</h2>
    <p>Lorem ipsum dolor sit amet.</p>
  </section>
  <section ref={registerSection('section-1')}>
    <h2>Section 2</h2>
    <p>Lorem ipsum dolor sit amet.</p>
  </section>
</div>

API

const {
  activeId,
  registerContainer,
  registerSection,
  registerTrigger,
} = useActiveMenu({
  activeClassName,
  offset,
  smooth,
});

Options

Key Type Default Description
activeClassName string 'active' Specifies the className that gets added to the active trigger.
offset number 0 Specifies the threshold distance (px) from the container top where the active section detection gets triggered.
smooth boolean false Specifies whether scrolling to each section should animate smoothly or not when a trigger is clicked.

Demos

Without Container

Edit react-active-menu / Full Screen

With Container

Edit react-active-menu / Containerized

https://user-images.githubusercontent.com/1900645/176918185-0fbf7484-f703-416f-8a32-fb624b3ea428.mp4

Package Sidebar

Install

npm i react-active-menu

Weekly Downloads

84

Version

0.0.14

License

MIT

Unpacked Size

18.6 kB

Total Files

33

Last publish

Collaborators

  • coreyleelarson