Merge pull request #21690 from Snuffleupagus/Menu-#goToInitial

Add a go to first/last menu-item helper method in the `Menu` class
This commit is contained in:
Jonas Jenwald 2026-08-04 10:20:46 +02:00 committed by GitHub
commit 72a76e585b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -146,19 +146,11 @@ class Menu {
stopEvent(e); stopEvent(e);
break; break;
case "Home": case "Home":
this.#menuItems this.#goToFirstLast(false);
.find(
item => !item.disabled && !item.classList.contains("hidden")
)
?.focus();
stopEvent(e); stopEvent(e);
break; break;
case "End": case "End":
this.#menuItems this.#goToFirstLast(true);
.findLast(
item => !item.disabled && !item.classList.contains("hidden")
)
?.focus();
stopEvent(e); stopEvent(e);
break; break;
default: default:
@ -194,11 +186,7 @@ class Menu {
if (!this.#openMenuAC) { if (!this.#openMenuAC) {
this.#openMenu(); this.#openMenu();
} }
this.#menuItems this.#goToFirstLast(false);
.find(
item => !item.disabled && !item.classList.contains("hidden")
)
?.focus();
break; break;
case "ArrowUp": case "ArrowUp":
case "End": case "End":
@ -206,11 +194,7 @@ class Menu {
if (!this.#openMenuAC) { if (!this.#openMenuAC) {
this.#openMenu(); this.#openMenu();
} }
this.#menuItems this.#goToFirstLast(true);
.findLast(
item => !item.disabled && !item.classList.contains("hidden")
)
?.focus();
break; break;
case "Escape": case "Escape":
this.#closeMenu(); this.#closeMenu();
@ -252,6 +236,20 @@ class Menu {
} }
} }
/**
* Go to the first/last menu item.
* @param {boolean} [last]
*/
#goToFirstLast(last = false) {
const i = this.#menuItems[last ? "findLastIndex" : "findIndex"](
item => !item.disabled && !item.classList.contains("hidden")
);
if (i >= 0) {
this.#menuItems[i].focus();
this.#lastIndex = i;
}
}
destroy() { destroy() {
this.#closeMenu(); this.#closeMenu();
this.#menuAC?.abort(); this.#menuAC?.abort();