fix rapid keypress/escape race condition

This commit is contained in:
cade
2026-04-13 10:58:48 -07:00
parent abb889a916
commit 80f6a94319
+23 -2
View File
@@ -490,6 +490,7 @@
#input; #input;
#suggestions; #suggestions;
#lastSuggestions = []; #lastSuggestions = [];
#closeController = null;
constructor() { constructor() {
super(); super();
@@ -587,11 +588,15 @@
}; };
#open() { #open() {
this.#closeController?.abort();
this.#closeController = null;
this.#dialog.show(); this.#dialog.show();
requestAnimationFrame(() => this.#dialog.classList.add('visible')); requestAnimationFrame(() => this.#dialog.classList.add('visible'));
} }
#close() { #close() {
this.#closeController?.abort();
this.#closeController = new AbortController();
this.#dialog.classList.remove('visible'); this.#dialog.classList.remove('visible');
this.#input.value = ''; this.#input.value = '';
this.#input.blur(); this.#input.blur();
@@ -603,8 +608,10 @@
if (!this.#dialog.classList.contains('visible')) { if (!this.#dialog.classList.contains('visible')) {
this.#dialog.close(); this.#dialog.close();
} }
this.#closeController = null;
}, },
{ once: true } { once: true, signal: this.#closeController.signal }
); );
} }
@@ -717,7 +724,21 @@
#onKeydown = (e) => { #onKeydown = (e) => {
if (!this.#dialog.classList.contains('visible')) { if (!this.#dialog.classList.contains('visible')) {
if (e.key === 'Escape') return; if (e.key === 'Escape') {
if (this.#dialog.open) {
this.#closeController?.abort();
this.#closeController = null;
this.#input.value = '';
this.#input.blur();
this.#dialog.close();
this.#suggestions.innerHTML = '';
}
return;
}
this.#closeController?.abort();
this.#closeController = null;
this.#dialog.show(); this.#dialog.show();
this.#input.focus(); this.#input.focus();