From 9cf8cca1cbcb9522b229732c5540a3cc9bdb5aff Mon Sep 17 00:00:00 2001 From: Herculino Trotta Date: Fri, 13 Nov 2026 15:04:41 -0300 Subject: [PATCH 1/2] 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. --- frontend/src/js/datepicker.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/frontend/src/js/datepicker.js b/frontend/src/js/datepicker.js index 6f5f4fbc..0da3ad6b 100644 --- a/frontend/src/js/datepicker.js +++ b/frontend/src/js/datepicker.js @@ -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, From e2981bf1f6276692ba3c2d52006797e6989efbcd Mon Sep 17 00:00:00 2001 From: Herculino Trotta Date: Fri, 13 Nov 2026 15:18:06 -0300 Subject: [PATCH 2/2] fix(datepicker): keep pickers working after history back and forward A history restore swaps in htmx's snapshot of the body, which carries a lifeless copy of AirDatepicker's global container. The previous fix looked the container up by id, found that copy, and left the library's real container detached, so pickers opened into nothing. Track the container through datepicker.$container instead, remove any snapshot copies after a swap, and reattach the real one when detached. --- frontend/src/js/datepicker.js | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/frontend/src/js/datepicker.js b/frontend/src/js/datepicker.js index 0da3ad6b..ff530a06 100644 --- a/frontend/src/js/datepicker.js +++ b/frontend/src/js/datepicker.js @@ -113,32 +113,30 @@ 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; +// The container AirDatepicker opens every picker into (its own reference, not a lookup by +// id: a history restore brings back lifeless copies of it from htmx's snapshot). let globalContainer = null; function createAirDatepicker(element, opts) { const datepicker = new AirDatepicker(element, opts); - mobileOverlay ??= document.querySelector('.air-datepicker-overlay'); - const container = document.getElementById(AirDatepicker.defaultGlobalContainerId); - if (mobileOverlay && !mobileOverlay.isConnected && container) { + globalContainer = datepicker.$container; + mobileOverlay ??= globalContainer.querySelector('.air-datepicker-overlay'); + if (mobileOverlay && !mobileOverlay.isConnected) { mobileOverlay.classList.remove('-active-'); // its picker was swapped out while open - container.appendChild(mobileOverlay); + globalContainer.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. +// Pickers kept by hx-preserve aren't recreated, so nothing makes AirDatepicker rebuild +// the container the swap removed: put it back, and drop any snapshot copies of it. +// Runs after htmx:load, so pickers created by this swap have already set globalContainer. 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); - } + if (!globalContainer) return; + document.querySelectorAll(`#${AirDatepicker.defaultGlobalContainerId}`).forEach(c => { + if (c !== globalContainer) c.remove(); + }); + if (!globalContainer.isConnected) document.body.appendChild(globalContainer); }); window.DatePicker = async function createDynamicDatePicker(element) {