[feat] back button closes modal

This commit is contained in:
2026-03-02 09:30:46 -05:00
parent 858092551a
commit 78af92a16a
4 changed files with 31 additions and 21 deletions

View File

@@ -1,3 +1,25 @@
const filterfacet = (event) => {
console.log(event);
}
import * as bootstrap from 'bootstrap';
window.bootstrap = bootstrap;
// trap browser back and close the modal if open
const cardModal = document.getElementById('cardModal');
// Push a new history state when the modal is shown
cardModal.addEventListener('shown.bs.modal', () => {
history.pushState({ modalOpen: true }, null, '#cardModal');
});
// Listen for the browser's back button (popstate event)
window.addEventListener('popstate', (e) => {
if (cardModal.classList.contains('show')) {
const modalInstance = bootstrap.Modal.getInstance(cardModal);
if (modalInstance) {
modalInstance.hide();
}
}
});
// Trigger a back navigation when the modal is closed via its native controls (X, backdrop click)
cardModal.addEventListener('hide.bs.modal', () => {
if (history.state && history.state.modalOpen) {
history.back();
}
});