useScrollSpy
Automatically update navigation based on scroll position.
Installation
Install the custom hook from your command line.
npm i @raddix/use-scroll-spy
Usage
API
Behind the scenes, it uses the Intersection Observer Web API, so you can rely on the best performance standards while not having to worry about implementing it yourself.
Result
Name | Description | Type |
---|---|---|
activeId | returns the element id of the navigation that is active. |
Parameters
The useScrollSpy
hook accepts an object with the Intersection Observer API options.
Name | Description | Required | Type |
---|---|---|---|
root | The element that is used as the viewport for checking visibility of the target. | No | |
rootMargin | Margin around the root. Can have values similar to the CSS margin property. | No | |
threshold | Either a single number or an array of numbers which indicate at what percentage of the target's visibility the observer's callback should be executed. | No |