From 7e04cf864fa416499d43112b15224b5960a9bf6d Mon Sep 17 00:00:00 2001 From: Sam Dornan Date: Thu, 29 Oct 2026 17:49:34 -0500 Subject: [PATCH] fix(frontend): let pad A toggle checkboxes, open read-only fields and keep focus after a pick (#5401) --- .../src/composables/useGamepad/index.test.ts | 98 +++++++++++++++---- frontend/src/composables/useGamepad/index.ts | 12 +-- .../src/lib/forms/RSelect/RSelect.stories.ts | 2 + frontend/src/lib/forms/RSelect/RSelect.vue | 8 +- frontend/src/utils/editable.test.ts | 18 ++++ frontend/src/utils/editable.ts | 25 +++-- 6 files changed, 126 insertions(+), 37 deletions(-) diff --git a/frontend/src/composables/useGamepad/index.test.ts b/frontend/src/composables/useGamepad/index.test.ts index e7becf4ab..84d1b5476 100644 --- a/frontend/src/composables/useGamepad/index.test.ts +++ b/frontend/src/composables/useGamepad/index.test.ts @@ -1,6 +1,6 @@ -import { mount } from "@vue/test-utils"; +import { flushPromises, mount } from "@vue/test-utils"; import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; -import { defineComponent } from "vue"; +import { defineComponent, h, ref } from "vue"; import { useInputModality } from "@/composables/useInputModality"; import { popEscapable, @@ -211,24 +211,26 @@ describe("useGamepad", () => { return field; } - it("clicks the focused control", () => { + it("clicks the focused control", async () => { const field = focusField(""); const click = vi.fn(); field.addEventListener("click", click); installOnMouse(padHolding(PAD_BUTTON.a)); step(); + await flushPromises(); expect(click).toHaveBeenCalledOnce(); }); - it("leaves a text field alone", () => { + it("leaves a text field alone", async () => { const field = focusField(""); const click = vi.fn(); field.addEventListener("click", click); installOnMouse(padHolding(PAD_BUTTON.a)); step(); + await flushPromises(); expect(click).not.toHaveBeenCalled(); }); @@ -236,24 +238,28 @@ describe("useGamepad", () => { it.each([ ["button", "button", ""], ["search field", "input", ""], - ])("picks the active option of an open %s combobox", (_, tag, close) => { - const field = focusField( - `<${tag} data-field role="combobox" aria-expanded="true" aria-activedescendant="opt-2">${close}` + - '', - ); - const picked = vi.fn(); - document.getElementById("opt-2")!.addEventListener("click", picked); - const fieldClick = vi.fn(); - field.addEventListener("click", fieldClick); + ])( + "picks the active option of an open %s combobox", + async (_, tag, close) => { + const field = focusField( + `<${tag} data-field role="combobox" aria-expanded="true" aria-activedescendant="opt-2">${close}` + + '', + ); + const picked = vi.fn(); + document.getElementById("opt-2")!.addEventListener("click", picked); + const fieldClick = vi.fn(); + field.addEventListener("click", fieldClick); - installOnMouse(padHolding(PAD_BUTTON.a)); - step(); + installOnMouse(padHolding(PAD_BUTTON.a)); + step(); + await flushPromises(); - expect(picked).toHaveBeenCalledOnce(); - expect(fieldClick).not.toHaveBeenCalled(); - }); + expect(picked).toHaveBeenCalledOnce(); + expect(fieldClick).not.toHaveBeenCalled(); + }, + ); - it("clicks a closed combobox itself", () => { + it("clicks a closed combobox itself", async () => { const field = focusField( '' + '
  • 1
  • ', @@ -263,9 +269,63 @@ describe("useGamepad", () => { installOnMouse(padHolding(PAD_BUTTON.a)); step(); + await flushPromises(); expect(click).toHaveBeenCalledOnce(); }); + + it.each([ + ["checkbox", ''], + ["read-only field", ''], + ])("clicks a focused %s", async (_, html) => { + const field = focusField(html); + const click = vi.fn(); + field.addEventListener("click", click); + + installOnMouse(padHolding(PAD_BUTTON.a)); + step(); + await flushPromises(); + + expect(click).toHaveBeenCalledOnce(); + }); + + it("picks the option a D-pad press in the same poll moved to", async () => { + const active = ref(1); + const picked = vi.fn(); + mount( + defineComponent(() => () => [ + h("button", { + "data-field": "", + role: "combobox", + "aria-expanded": "true", + "aria-activedescendant": `opt-${active.value}`, + onKeydown: (e: KeyboardEvent) => { + if (e.key === "ArrowDown") active.value += 1; + }, + }), + [1, 2].map((n) => + h("li", { + id: `opt-${n}`, + role: "option", + onClick: () => picked(n), + }), + ), + ]), + { attachTo: document.body }, + ); + document.querySelector("[data-field]")!.focus(); + // Settle into pad mode first, so the press is the only pending update. + let pad = padHolding(); + installOnMouse(() => [pad]); + setModality("pad"); + await flushPromises(); + + pad = padHolding(PAD_BUTTON.a, PAD_BUTTON["dpad-down"]); + step(); + await flushPromises(); + + expect(picked.mock.calls).toEqual([[2]]); + }); }); describe("B", () => { diff --git a/frontend/src/composables/useGamepad/index.ts b/frontend/src/composables/useGamepad/index.ts index 115227173..e991ffab3 100644 --- a/frontend/src/composables/useGamepad/index.ts +++ b/frontend/src/composables/useGamepad/index.ts @@ -32,7 +32,7 @@ // * Controller-test screen (ACTIONS_DISABLED_PATHS): built-in actions // are muted so every button can be pressed and inspected in place. import { useEventListener, useRafFn } from "@vueuse/core"; -import { onBeforeUnmount } from "vue"; +import { nextTick, onBeforeUnmount } from "vue"; import { useRoute, useRouter } from "vue-router"; import { useInputModality } from "@/composables/useInputModality"; import { @@ -187,12 +187,12 @@ export function useGamepad() { if (next.to !== route.path) void router.push(next.to); } - // Activates the currently focused element. Router-links, submit - // buttons, custom [role=button] divs all navigate/trigger via .click() - // regardless of whether the event was trusted: that's the escape - // hatch synthetic KeyboardEvents don't give us. + // A is polled before the D-pad, so wait for that poll's arrows to queue + // their render, then for the render, before reading the active option. function activateFocused() { - padActivationTarget(document.activeElement)?.click(); + queueMicrotask(() => { + void nextTick(() => padActivationTarget(document.activeElement)?.click()); + }); } // Opens the app-wide user menu. UserMenu.vue marks its activator with diff --git a/frontend/src/lib/forms/RSelect/RSelect.stories.ts b/frontend/src/lib/forms/RSelect/RSelect.stories.ts index 449403672..5a4d6f2b7 100644 --- a/frontend/src/lib/forms/RSelect/RSelect.stories.ts +++ b/frontend/src/lib/forms/RSelect/RSelect.stories.ts @@ -868,6 +868,8 @@ export const GamepadPickSearchable: Story = { expect(document.querySelector(".r-select__panel")).toBeNull(), ); await expect(field).toHaveTextContent("Super Nintendo"); + // Focus leaves the unmounted search field for the field, so the pad can move on. + await expect(field).toHaveFocus(); }); }, }; diff --git a/frontend/src/lib/forms/RSelect/RSelect.vue b/frontend/src/lib/forms/RSelect/RSelect.vue index c558b993d..5703585b7 100644 --- a/frontend/src/lib/forms/RSelect/RSelect.vue +++ b/frontend/src/lib/forms/RSelect/RSelect.vue @@ -728,6 +728,8 @@ function openMenu() { } function closeMenu() { if (!isOpen.value) return; + // The panel unmounts on close, so focus inside it goes back to the field. + const focusInPanel = !!panelRef.value?.contains(document.activeElement); isOpen.value = false; emit("close"); resetTypeahead(); @@ -737,6 +739,7 @@ function closeMenu() { // user expectation for "open select → type → pick → close → reopen" // not leaving a stale query that hides most rows. if (props.searchable && internalSearch.value) setSearch(""); + if (focusInPanel) focusFromInput(activatorRef.value); } function toggleMenu() { if (isOpen.value) closeMenu(); @@ -746,11 +749,6 @@ function toggleMenu() { usePopoverDismiss(isOpen, closeMenu, { reference: () => activatorRef.value, panel: () => panelRef.value, - onEscape: () => { - const focusInPanel = !!panelRef.value?.contains(document.activeElement); - closeMenu(); - if (focusInPanel) focusFromInput(activatorRef.value); - }, }); // Close when search is changed externally? No, keep open while diff --git a/frontend/src/utils/editable.test.ts b/frontend/src/utils/editable.test.ts index a58ad8ade..9957be8f6 100644 --- a/frontend/src/utils/editable.test.ts +++ b/frontend/src/utils/editable.test.ts @@ -15,6 +15,24 @@ describe("isEditable", () => { el.remove(); }); + it.each(["checkbox", "radio", "range", "submit"])( + "is false for an ", + (type) => { + const input = document.createElement("input"); + input.type = type; + expect(isEditable(input)).toBe(false); + }, + ); + + it.each(["input", "textarea"] as const)( + "is false for a read-only <%s>", + (tag) => { + const el = document.createElement(tag); + el.readOnly = true; + expect(isEditable(el)).toBe(false); + }, + ); + it("is false for a button, the document and nothing", () => { expect(isEditable(document.createElement("button"))).toBe(false); expect(isEditable(document)).toBe(false); diff --git a/frontend/src/utils/editable.ts b/frontend/src/utils/editable.ts index 72effbe60..f8b894e29 100644 --- a/frontend/src/utils/editable.ts +++ b/frontend/src/utils/editable.ts @@ -1,12 +1,23 @@ -/** True for form fields and contenteditable, whose keys and pad A are theirs. */ +// Inputs that take no typing: they act on a click, like a button. +const NON_TEXT_INPUT_TYPES = new Set([ + "button", + "checkbox", + "color", + "file", + "image", + "radio", + "range", + "reset", + "submit", +]); + +/** True for fields the user types into, whose keys and pad A are theirs. */ export function isEditable(el: EventTarget | null): boolean { if (!(el instanceof HTMLElement)) return false; - if ( - el instanceof HTMLInputElement || - el instanceof HTMLTextAreaElement || - el instanceof HTMLSelectElement - ) { - return true; + if (el instanceof HTMLInputElement) { + return !el.readOnly && !NON_TEXT_INPUT_TYPES.has(el.type); } + if (el instanceof HTMLTextAreaElement) return !el.readOnly; + if (el instanceof HTMLSelectElement) return true; return el.isContentEditable; }