Skip to content

Commit

Permalink
Extended intersection observer to also hide the fixed share buttons w…
Browse files Browse the repository at this point in the history
…hen the user has scrolled past the share buttons.

This is because we don't want to show the fixed share buttons in the footer
  • Loading branch information
JacobArrow committed Dec 12, 2024
1 parent 0a39ad7 commit 230f95d
Showing 1 changed file with 13 additions and 13 deletions.
26 changes: 13 additions & 13 deletions src/components/button-share/button-share.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,31 +24,31 @@ const ButtonShare: React.FC<ButtonShareProps> = ({ className }) => {
navigator.clipboard.writeText(href);
};

// Hide fixed buttons when share button is in view or above viewport
useEffect(() => {
const shareButton = shareButtonRef.current;
const observer = new IntersectionObserver(
(entries) => {
entries.forEach((entry) => {
if (entry.isIntersecting) {
setShowFixedButtons(false);
} else {
setShowFixedButtons(true);
}
const isIntersecting = entries.some((entry) => entry.isIntersecting);
const isAboveViewport = entries.some((entry) => {
// return true if the share button is above the viewport
return entry.boundingClientRect.top < 0;
});
setShowFixedButtons(!isIntersecting && !isAboveViewport);
},
{ threshold: 1 }
);

const shareButton = shareButtonRef.current;

if (shareButton) {
observer.observe(shareButton);

return () => {
// Clean up observer on unmount
observer.disconnect();
};
}

return () => {
if (shareButton) {
observer.unobserve(shareButton);
}
};
return () => {};
}, []);

return (
Expand Down

0 comments on commit 230f95d

Please sign in to comment.