-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #180 from Vizzuality/SKY30-241-highlight-side-navi…
…gation-active-section-item-on-the-home-and-about-pages [SKY30-241] Highlight side navigation active section item on the home and about pages
- Loading branch information
Showing
4 changed files
with
106 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { RefObject, useLayoutEffect, useState } from 'react'; | ||
|
||
type ScrollSpyItem = { | ||
id: string; | ||
ref: RefObject<HTMLDivElement>; | ||
}; | ||
|
||
type ScrollSpyOptions = { | ||
overBounds: boolean; | ||
}; | ||
|
||
const DEFAULT_OPTIONS = { | ||
overBounds: true, | ||
thresholdPercentage: 20, | ||
}; | ||
|
||
const useScrollSpy = (items: ScrollSpyItem[], options?: ScrollSpyOptions) => { | ||
const [activeId, setActiveId] = useState<string | null>(null); | ||
|
||
useLayoutEffect(() => { | ||
const scrollListener = () => { | ||
const windowHeight = window.innerHeight; | ||
const viewingPosition = (DEFAULT_OPTIONS.thresholdPercentage * windowHeight) / 100; | ||
|
||
const itemsPositions = items.map(({ id, ref }) => { | ||
const element = ref?.current; | ||
if (!element) return null; | ||
|
||
const boundingRect = element.getBoundingClientRect(); | ||
|
||
return { | ||
id, | ||
top: boundingRect.top, | ||
bottom: boundingRect.bottom, | ||
}; | ||
}); | ||
|
||
const activeItem = itemsPositions.find(({ top: itemTop, bottom: itemBottom }) => { | ||
return itemTop < viewingPosition && itemBottom > viewingPosition; | ||
}); | ||
|
||
if (!activeItem?.id && (options?.overBounds || DEFAULT_OPTIONS.overBounds) === true) { | ||
const firstItem = itemsPositions[0]; | ||
const lastItem = itemsPositions[itemsPositions.length - 1]; | ||
|
||
if (viewingPosition < firstItem.top) { | ||
setActiveId(firstItem.id); | ||
} | ||
|
||
if (viewingPosition > lastItem.bottom) { | ||
setActiveId(lastItem.id); | ||
} | ||
} else { | ||
setActiveId(activeItem?.id); | ||
} | ||
}; | ||
|
||
scrollListener(); | ||
|
||
window.addEventListener('resize', scrollListener); | ||
window.addEventListener('scroll', scrollListener); | ||
|
||
return () => { | ||
window.removeEventListener('resize', scrollListener); | ||
window.removeEventListener('scroll', scrollListener); | ||
}; | ||
}, [items, options]); | ||
|
||
return activeId; | ||
}; | ||
|
||
export default useScrollSpy; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters