fix(datepicker): keep preserved pickers working after boosted navigation

The filter on the Monthly and All pages is wrapped in hx-preserve, so a
boosted navigation keeps its pickers instead of recreating them. The
AirDatepicker global container they render into lives on the body and is
swapped out with it, leaving those pickers drawing into a detached node.

After each swap, move the detached container's pickers into the new
container, or reattach it when no new picker created one.
This commit is contained in:
Herculino Trotta committed 2026-11-13 15:04:41 -03:00
1 parent 1c891a0a60
commit 9cf8cca1cb
1 file changed
+16
+16
View File
@@ -113,6 +113,7 @@ function isMobile() {
// swaps the body: AirDatepicker then rebuilds the container, but keeps the old
// overlay, which is left detached and can't receive the clicks that close a picker.
let mobileOverlay = null;
let globalContainer = null;
function createAirDatepicker(element, opts) {
const datepicker = new AirDatepicker(element, opts);
@@ -122,9 +123,24 @@ function createAirDatepicker(element, opts) {
mobileOverlay.classList.remove('-active-'); // its picker was swapped out while open
container.appendChild(mobileOverlay);
}
globalContainer = document.getElementById(AirDatepicker.defaultGlobalContainerId) ?? globalContainer;
return datepicker;
}
// Pickers kept across a boosted navigation by hx-preserve aren't recreated, but the
// global container they render into was swapped out with the body. htmx:load (which
// creates the new pickers) runs before htmx:afterSwap, so by now any new container exists.
document.addEventListener('htmx:afterSwap', () => {
if (!globalContainer || globalContainer.isConnected) return;
const current = document.getElementById(AirDatepicker.defaultGlobalContainerId);
if (current) {
current.append(...globalContainer.children);
globalContainer = current;
} else {
document.body.appendChild(globalContainer);
}
});
window.DatePicker = async function createDynamicDatePicker(element) {
let todayButton = {
content: element.dataset.nowButtonTxt,