From c47bf0d0e0c4ae2c1ba2106f63c359311eab7ed0 Mon Sep 17 00:00:00 2001 From: "Benjamin A. Shelton" Date: Thu, 20 Aug 2026 11:38:40 -0600 Subject: [PATCH] Apparently there's a weird behavior in Android Chrome that this fixes. The bug is that hitting "back" will trigger a cancel event and then the dialog::backdrop will be removed first. The second "back" press will remove the dialog or actually trigger history.back(), navigating back in state. The fix for this is to intercept the "cancel" event on the dialog and then handle history management from there. Apparently this issue will be triggered even if the history API isn't used, and the only way the user can navigate normally with the dialog present is to actually hook into the history API. So, this is kind of a necessary change. Annoying. Thanks, Gemini, for the introspection and links. --- src/utils/modal.js | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/src/utils/modal.js b/src/utils/modal.js index adfc6e7..8a548ab 100644 --- a/src/utils/modal.js +++ b/src/utils/modal.js @@ -80,11 +80,21 @@ class RenderScripture { constructor(dialog) { this.dialog = dialog; - /*history.replaceState({dialog: false}, ''); + //history.replaceState({dialog: false}, ''); window.addEventListener('popstate', (evt) => { - console.log("popState", evt); - this.closeDialog(); - });*/ + if (this.dialog[0]?.open) { + this.closeDialog(); + } + }); + this.dialog[0]?.addEventListener('close', () => { + if (history.state && history.state.dialog) { + history.back(); + } + }); + this.dialog[0]?.addEventListener('cancel', (evt) => { + evt.preventDefault(); + history.back(); + }); if (hasLocalStorage()) { this.ls = true; @@ -109,8 +119,11 @@ class RenderScripture { } }); this.dialog[0]?.querySelector('button[data-close]')?.addEventListener('click', (evt) => { - evt.preventDefault(); - this.closeDialog(); + if (history.state?.dialog) { + history.back(); + } else { + this.closeDialog(); + } }); } @@ -128,12 +141,11 @@ class RenderScripture { .classList.remove('hidden'); this.buildTranslationList(this.sel); this.readScripture(refs); - //console.log("pushState()"); - /*history.pushState({ + history.pushState({ dialog: true, scripture: scripture.value, tx: this.translation - }, '', '#dialog');*/ + }, '', '#dialog'); } buildTranslationList(sel) {