Skip to content

Instantly share code, notes, and snippets.

@TrekSoft
Last active February 8, 2022 23:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TrekSoft/fb63a4f119e31ac1d31d84d18c153ad1 to your computer and use it in GitHub Desktop.
Save TrekSoft/fb63a4f119e31ac1d31d84d18c153ad1 to your computer and use it in GitHub Desktop.
shouldFloatFooter function - lastMutation
const [mutationObserver, setMutationObserver] =
useState<MutationObserver>(null);
useEffect(() => {
let delayedMutationEffect;
function handleMutations(mutations: MutationRecord[]) {
setLastMutation(mutations[mutations.length - 1]);
delayedMutationEffect = setTimeout(() => setLastMutation(null), maxAnimationLength);
}
if (window.MutationObserver && !mutationObserver) {
setMutationObserver(new MutationObserver(handleMutations));
}
return () => {
if (mutationObserver) {
mutationObserver.disconnect();
}
if (delayedMutationEffect) {
clearTimeout(delayedMutationEffect);
}
};
}, [setMutationObserver, mutationObserver, maxAnimationLength]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment