Skip to content

Commit

Permalink
fix: the addEventListener was not adding correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
jonasCr committed Sep 29, 2022
1 parent 51650ed commit a11cd08
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export interface useScrollSpyParams {
export default ({
activeSectionDefault = 0,
offsetPx = 0,
scrollingElement = window,
scrollingElement = null,
sectionElementRefs = [],
throttleMs = 100,
}: useScrollSpyParams ) => {
Expand All @@ -37,13 +37,17 @@ export default ({
});

useEffect(() => {
scrollingElement.addEventListener('scroll', handle);
let scrollable = scrollingElement == null
? window
: scrollingElement.current

scrollable.addEventListener('scroll', handle);

// Run initially
handle();

return () => {
scrollingElement.removeEventListener('scroll', handle);
scrollable.removeEventListener('scroll', handle);
};
}, [sectionElementRefs, offsetPx]);
return activeSection;
Expand Down

0 comments on commit a11cd08

Please sign in to comment.