docbrown/app/javascript/utilities/billboardInteractivity.jsx
Ben Halpern 205aee8c78
Add billboard for bottom of page with allowance to delay until after reading is finished. (#20616)
* Add delayed behavior

* Add special behavior to billboards

* Fix order for test

* Test clicking outside element when close event is present

* Update app/javascript/packs/billboardAfterRenderActions.js
2024-02-13 11:56:32 -05:00

53 lines
1.8 KiB
JavaScript

import { initializeDropdown } from './dropdownUtils';
export function setupBillboardInteractivity() {
const sponsorshipDropdownButtons = document.querySelectorAll(
'button[id^=sponsorship-dropdown-trigger-]',
);
if (sponsorshipDropdownButtons.length) {
sponsorshipDropdownButtons.forEach((sponsorshipDropdownButton) => {
amendBillboardStyle(sponsorshipDropdownButton);
const dropdownContentId =
sponsorshipDropdownButton.getAttribute('aria-controls');
if (
sponsorshipDropdownButton &&
sponsorshipDropdownButton.dataset.initialized !== 'true'
) {
initializeDropdown({
triggerElementId: sponsorshipDropdownButton.id,
dropdownContentId,
});
sponsorshipDropdownButton.dataset.initialized = 'true';
}
});
}
const sponsorshipCloseButtons = document.querySelectorAll(
'button[id^=sponsorship-close-trigger-]',
);
if (sponsorshipCloseButtons.length) {
sponsorshipCloseButtons.forEach((sponsorshipCloseButton) => {
sponsorshipCloseButton.addEventListener('click', () => {
sponsorshipCloseButton.closest('.js-billboard').style.display = 'none';
});
document.addEventListener('click', (event) => {
if (!event.target.closest('.js-billboard')) {
sponsorshipCloseButton.closest('.js-billboard').style.display = 'none';
}
});
});
}
}
/**
* If the billboard container height is less than 220px, then we revert the overflow-y property
given by the billboard class so that the dropdown does not scroll within the container
*/
function amendBillboardStyle(sponsorshipDropdownButton) {
if (sponsorshipDropdownButton.closest('.js-billboard').clientHeight < 220) {
sponsorshipDropdownButton.closest('.js-billboard').style.overflowY =
'revert';
}
}