-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a2a13d9
commit 8e53cb6
Showing
7 changed files
with
349 additions
and
375 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { useEffect, useState, useRef } from "react"; | ||
|
||
function useHeadsObserver() { | ||
const observer = useRef<IntersectionObserver | null>(null); | ||
const [activeId, setActiveId] = useState(""); | ||
|
||
useEffect(() => { | ||
const handleObserver = (entries: IntersectionObserverEntry[]) => { | ||
entries.forEach((entry) => { | ||
if (entry.isIntersecting) { | ||
setActiveId(entry.target.id); | ||
} | ||
}); | ||
}; | ||
|
||
observer.current = new IntersectionObserver(handleObserver, { | ||
rootMargin: "-20% 0% -35% 0px", | ||
}); | ||
|
||
const elements = document.querySelectorAll("h2, h3, h4"); | ||
elements.forEach((elem) => { | ||
if (observer.current) { | ||
observer.current.observe(elem); | ||
} | ||
}); | ||
|
||
return () => observer.current?.disconnect(); | ||
}, []); | ||
|
||
return { activeId }; | ||
} | ||
|
||
export default useHeadsObserver; |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.