mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Observing section by IntersectionObserver
Signed-off-by: Alex Andreev <alex.andreev.email@gmail.com>
This commit is contained in:
parent
1e73769bc1
commit
a2bb733e48
@ -7,17 +7,22 @@ interface Props extends React.DOMAttributes<any> {
|
||||
|
||||
export function ScrollSpy(props: Props) {
|
||||
const [tree, setTree] = useState<NavigationTree[]>([]);
|
||||
const [activeElementId, setActiveElementId] = useState("");
|
||||
|
||||
const updateNavigation = () => {
|
||||
const getContentParentElement = () => {
|
||||
const firstSection = document.querySelector("section");
|
||||
|
||||
if (!firstSection) {
|
||||
throw new Error("No <section/> tag founded! Content should be placed inside <section></section> elements to activate navigation.");
|
||||
}
|
||||
|
||||
return firstSection.parentElement;
|
||||
};
|
||||
|
||||
const updateNavigation = () => {
|
||||
setTree([
|
||||
...tree,
|
||||
...getNavigation(firstSection.parentElement)
|
||||
...getNavigation(getContentParentElement())
|
||||
]);
|
||||
};
|
||||
|
||||
@ -43,8 +48,28 @@ export function ScrollSpy(props: Props) {
|
||||
return children;
|
||||
};
|
||||
|
||||
const handleIntersect = (entries: IntersectionObserverEntry[]) => {
|
||||
if (entries[0].isIntersecting) {
|
||||
setActiveElementId(entries[0].target.id);
|
||||
}
|
||||
};
|
||||
|
||||
const observeSections = () => {
|
||||
const sections = getContentParentElement().querySelectorAll("section");
|
||||
const options = {
|
||||
rootMargin: "-50% 0px"
|
||||
};
|
||||
|
||||
sections.forEach((section) => {
|
||||
const observer = new IntersectionObserver(handleIntersect, options);
|
||||
|
||||
observer.observe(section);
|
||||
});
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
updateNavigation();
|
||||
observeSections();
|
||||
// element.current.addEventListener("scroll", updateActive);
|
||||
// TODO: Attach on dom change event
|
||||
}, []);
|
||||
|
||||
Loading…
Reference in New Issue
Block a user