[hooks/headingsObserver][s]: behavior adjustments
This commit is contained in:
parent
cc74f3a892
commit
89d1c58f97
|
|
@ -3,18 +3,7 @@ import { useState, useEffect } from "react";
|
||||||
/* Creates an Intersection Observer to keep track of headings intersecting
|
/* Creates an Intersection Observer to keep track of headings intersecting
|
||||||
* a section of the viewport defined by the rootMargin */
|
* a section of the viewport defined by the rootMargin */
|
||||||
const getIntersectionObserver = (callback) => {
|
const getIntersectionObserver = (callback) => {
|
||||||
return new IntersectionObserver(
|
return;
|
||||||
(entries) => {
|
|
||||||
entries.forEach((entry) => {
|
|
||||||
callback(entry);
|
|
||||||
});
|
|
||||||
},
|
|
||||||
{
|
|
||||||
root: null,
|
|
||||||
threshold: 0.55,
|
|
||||||
rootMargin: "-65px 0% -85% 0%", // 65px is a navbar height
|
|
||||||
}
|
|
||||||
);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const useHeadingsObserver = () => {
|
const useHeadingsObserver = () => {
|
||||||
|
|
@ -24,11 +13,27 @@ const useHeadingsObserver = () => {
|
||||||
/* Runs only after the first render, in order to preserve the observer
|
/* Runs only after the first render, in order to preserve the observer
|
||||||
* between component rerenderings (e.g. after activeHeading change). */
|
* between component rerenderings (e.g. after activeHeading change). */
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const observer = getIntersectionObserver((entry) => {
|
const observer = new IntersectionObserver(
|
||||||
if (entry.isIntersecting) {
|
(entries) => {
|
||||||
setActiveHeading(entry.target.id);
|
// entries.forEach((entry) => {
|
||||||
|
// if (entry.isIntersecting) {
|
||||||
|
// setActiveHeading(entry.target.id);
|
||||||
|
// }
|
||||||
|
// });
|
||||||
|
const firstIntersectingHeading = entries.find(
|
||||||
|
(entry) => entry.isIntersecting
|
||||||
|
);
|
||||||
|
if (firstIntersectingHeading) {
|
||||||
|
setActiveHeading(firstIntersectingHeading.target.id);
|
||||||
}
|
}
|
||||||
});
|
},
|
||||||
|
{
|
||||||
|
root: null,
|
||||||
|
threshold: 0.55,
|
||||||
|
rootMargin: "-65px 0% -85% 0%", // 65px is a navbar height
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
setObserver(observer);
|
setObserver(observer);
|
||||||
|
|
||||||
return () => {
|
return () => {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue