Skip to content

Commit

Permalink
check for multiple ctas buttons
Browse files Browse the repository at this point in the history
  • Loading branch information
microstudi committed Jun 10, 2024
1 parent 344d2d3 commit 13a8919
Showing 1 changed file with 23 additions and 8 deletions.
31 changes: 23 additions & 8 deletions decidim-core/app/packs/src/decidim/sticky_header.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,30 @@
let prevScroll = window.scrollY;
const stickyHeader = document.querySelector("[data-sticky-header]");
const footer = document.querySelector("footer");
const ctasButtons = document.querySelector("[data-ctas-buttons]");
const ctasButtons = document.querySelectorAll("[data-ctas-buttons]");

const isElementInViewport = (element) => {
const rect = element.getBoundingClientRect();
return rect.top >= 0 && rect.bottom <= (window.innerHeight || document.documentElement.clientHeight);
};

const adjustCtasButtons = () => {
if (!ctasButtons || !ctasButtons.length) {
return;
}

ctasButtons.forEach((ctasButton) => {
if (!ctasButton.offsetParent) {
return;
}
if (isElementInViewport(ctasButton)) {
footer.style.marginBottom = `${ctasButton.offsetHeight}px`;
} else {
footer.style.marginBottom = 0;
}
});
};

if (stickyHeader) {
document.addEventListener("scroll", () => {
// if a subelement is not visible it has no offsetParent
Expand All @@ -28,13 +45,11 @@ if (stickyHeader) {
prevScroll = currentScroll;
}

if (ctasButtons) {
if (isElementInViewport(ctasButtons)) {
footer.style.marginBottom = `${ctasButtons.offsetHeight}px`;
} else {
footer.style.marginBottom = 0;
}
}
adjustCtasButtons();
}
});

document.addEventListener("on:toggle", () => {
adjustCtasButtons();
});
};

0 comments on commit 13a8919

Please sign in to comment.