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-use-navscroll

v0.2.0

Published

A React headless hook for navscroll detection

Downloads

1,371

Readme

react-use-navscroll

The React hook for nav scrolling

Demo here

Install

npm install --save react-use-navscroll

Usage

import React from 'react'
import {useNavScroll} from 'react-use-navscroll'


function Example(){
  const {register, isActive} = useNavScroll();
  return <div>
    <h1 {...register('1')}>My title {isActive('1') ? '(active)' : ''}</h1>
    <h2 {...register('2')}>My subtitle {isActive('2') ? '(active)' : ''}</h2>
  </div>
}

Browser support

The hook uses the IntersectionObserver API which is available only for modern browsers. For older browsers with no support (i.e. Internet Explorer 11) it is recommended a polyfill.

Contributing

To get up and running with the development of the project you can follow these steps.

To see the component in action, useful to test it manually after some changes, open a terminal window and type:

cd example
npm start

This will start the create-react-app project and open a new browser tab with the example application in it.

At this point it is possible to perform changes to the component/library, open a new terminal in the project folder and use this command when done with changes:

npm run build

To run the test do:

npm test

Change Readme file

The readme file is autogenerated from a template located in script/readme.template: the template contains everything but the API which is generated from the Typescript source. No need to touch the API part (which will be overwritten anyway).

API

Hook

useNavScroll

useNavScroll(args : useNavScrollArgs) => useNavScrollResult

This is the main hook: use it in a react function component to track
the state of the passed ids. The function accepts an initial configuration
of type useNavScrollArgs to customize the behaviour.


Types

ChangesType

The returned object from the onChange passed function, with the current state of
added and removed element ids.

{

added: string | null,

removed: string | null

}

RegisterOptions

The options object passed to the register function.

{

parent: string,

ref: RefObject<Element>

}

The details of each property is described here:

  • parent: string

Pass the string id of the parent element

  • ref: RefObject<Element>

If the tracked element has already a reference, you can pass it and will be reused


useNavScrollArgs

The react-use-navscroll configuration object

{

isHorizontal: boolean,

offset: number,

root: Element,

onChange: (changes : ChangesType) => void

}

The details of each property is described here:

  • isHorizontal: boolean

Declare if the detection should work vertically or horizontally. By default false (vertical)

  • offset: number

Moves the detection line by the amount of this offset, expressed in percentage. By default the value is 50 (center).

  • root: Element

Pass an element as root to track a specific container, smaller than a viewport. Default is window (the whole viewport).

  • onChange: (changes : ChangesType) => void

Function called every time an element becomes active.
The changes object returned contains the "added" and "removed" id.


useNavScrollResult

The object returned by the hook.

{

activeIds: string[],

getActiveRef: () => null | RefObject<Element>,

isActive: (id : string) => boolean,

register: (id : string, options ?: RegisterOptions) => RegisteredAttributes<T extends Element>,

unregister: (idToUnregister : string) => void

}

The details of each property is described here:

  • activeIds: string[]

A list of active ids (the full hierarchy).

  • getActiveRef: () => null | RefObject<Element>

A function to retrieve the reference of the current active element (only the last element, not the elements hierarchy).

  • isActive: (id : string) => boolean

A convenience function to quickly check the active state for the given id

  • register: (id : string, options ?: RegisterOptions) => RegisteredAttributes<T extends Element>

The function used to register the component into the tracking system.
It returns the id already passed and the reference object.
Note that only the reference value will be null in a SSR context.

  • unregister: (idToUnregister : string) => void

Removes the given id from the tracking system.


Acknowledgements

I would like to acknowledge few articles and libraries used as inspiration for this one:

  • This article for the detection technique used in this library
  • The React Hook Form for the inspiration about the register pattern used in this library.
  • The RomaJS slack channel for preliminary the useful discussion which led to the build of this library

License

MIT © dej611