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.
This commit is contained in:
Benjamin A. Shelton 2026-08-20 11:38:40 -06:00
commit c47bf0d0e0
Signed by: zancarius
GPG key ID: 8BD42DBD6C337E01

View file

@ -80,11 +80,21 @@ class RenderScripture {
constructor(dialog) { constructor(dialog) {
this.dialog = dialog; this.dialog = dialog;
/*history.replaceState({dialog: false}, ''); //history.replaceState({dialog: false}, '');
window.addEventListener('popstate', (evt) => { window.addEventListener('popstate', (evt) => {
console.log("popState", evt); if (this.dialog[0]?.open) {
this.closeDialog(); 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()) { if (hasLocalStorage()) {
this.ls = true; this.ls = true;
@ -109,8 +119,11 @@ class RenderScripture {
} }
}); });
this.dialog[0]?.querySelector('button[data-close]')?.addEventListener('click', (evt) => { this.dialog[0]?.querySelector('button[data-close]')?.addEventListener('click', (evt) => {
evt.preventDefault(); if (history.state?.dialog) {
this.closeDialog(); history.back();
} else {
this.closeDialog();
}
}); });
} }
@ -128,12 +141,11 @@ class RenderScripture {
.classList.remove('hidden'); .classList.remove('hidden');
this.buildTranslationList(this.sel); this.buildTranslationList(this.sel);
this.readScripture(refs); this.readScripture(refs);
//console.log("pushState()"); history.pushState({
/*history.pushState({
dialog: true, dialog: true,
scripture: scripture.value, scripture: scripture.value,
tx: this.translation tx: this.translation
}, '', '#dialog');*/ }, '', '#dialog');
} }
buildTranslationList(sel) { buildTranslationList(sel) {