diff --git a/frontend/src/v2/composables/useSpatialNav/index.test.ts b/frontend/src/v2/composables/useSpatialNav/index.test.ts index 403afd5c6..a48ea990e 100644 --- a/frontend/src/v2/composables/useSpatialNav/index.test.ts +++ b/frontend/src/v2/composables/useSpatialNav/index.test.ts @@ -132,6 +132,23 @@ describe("useSpatialNav", () => { vi.unstubAllGlobals(); }); + it("skips controls a roving tabindex has taken out of the tab order", () => { + el("tab").setAttribute("tabindex", "-1"); + el("play").focus(); + + expect(press("ArrowDown").defaultPrevented).toBe(false); + expect(document.activeElement).toBe(el("play")); + }); + + it("leaves the arrows to an open popup's activator", () => { + el("tab").setAttribute("aria-haspopup", "listbox"); + el("tab").setAttribute("aria-expanded", "true"); + el("tab").focus(); + + expect(press("ArrowUp").defaultPrevented).toBe(false); + expect(document.activeElement).toBe(el("tab")); + }); + it("stays out of the way while a game is running", () => { storePlaying().setPlaying(true); el("play").focus(); diff --git a/frontend/src/v2/composables/useSpatialNav/index.ts b/frontend/src/v2/composables/useSpatialNav/index.ts index bd9493021..dc068eec0 100644 --- a/frontend/src/v2/composables/useSpatialNav/index.ts +++ b/frontend/src/v2/composables/useSpatialNav/index.ts @@ -34,6 +34,15 @@ const ARROW_OWNING_ROLES = new Set([ function ownsArrowKeys(el: HTMLElement): boolean { if (el.isContentEditable) return true; + // An open popup's activator, such as RSelect's, steers its list by arrow. + const popup = el.getAttribute("aria-haspopup"); + if ( + popup && + popup !== "false" && + el.getAttribute("aria-expanded") === "true" + ) { + return true; + } if (el instanceof HTMLInputElement) return !BUTTON_LIKE_INPUTS.has(el.type); if (el instanceof HTMLTextAreaElement || el instanceof HTMLSelectElement) { return true; diff --git a/frontend/src/v2/utils/spatialNav.ts b/frontend/src/v2/utils/spatialNav.ts index d45477b61..ed376b683 100644 --- a/frontend/src/v2/utils/spatialNav.ts +++ b/frontend/src/v2/utils/spatialNav.ts @@ -8,8 +8,10 @@ export const FOCUSABLE_SELECTOR = [ "input:not([disabled])", "select:not([disabled])", "textarea:not([disabled])", - "[tabindex]:not([tabindex='-1'])", -].join(","); + "[tabindex]", +] + .map((selector) => `${selector}:not([tabindex='-1'])`) + .join(","); export type SpatialDirection = "up" | "down" | "left" | "right";