fix(v2): address Greptile review on spatial navigation

Leave the arrows to an expanded popup's activator, so an open RSelect
keeps focus, and keep tabindex=-1 anchors and buttons out of the
candidates, so a roving grid is entered at its current cell.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Georges-Antoine Assi
2026-09-27 20:25:42 -04:00
parent 21ccdda7a7
commit 4b81aba42e
3 changed files with 30 additions and 2 deletions

View File

@@ -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();

View File

@@ -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;

View File

@@ -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";