fix(frontend): let pad A toggle checkboxes, open read-only fields and keep focus after a pick (#5401)

This commit is contained in:
Sam Dornan
2026-10-29 17:49:34 -05:00
committed by GitHub
parent 190a219c8c
commit 7e04cf864f
6 changed files with 126 additions and 37 deletions

View File

@@ -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("<button data-field>Go</button>");
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("<input data-field />");
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", "</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}` +
'<ul role="listbox"><li id="opt-1" role="option">1</li><li id="opt-2" role="option">2</li></ul>',
);
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}` +
'<ul role="listbox"><li id="opt-1" role="option">1</li><li id="opt-2" role="option">2</li></ul>',
);
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(
'<button data-field role="combobox" aria-expanded="false" aria-activedescendant="opt-1"></button>' +
'<li id="opt-1" role="option">1</li>',
@@ -263,9 +269,63 @@ describe("useGamepad", () => {
installOnMouse(padHolding(PAD_BUTTON.a));
step();
await flushPromises();
expect(click).toHaveBeenCalledOnce();
});
it.each([
["checkbox", '<input data-field type="checkbox" />'],
["read-only field", '<input data-field readonly value="Mar 15, 2024" />'],
])("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<HTMLElement>("[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", () => {

View File

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

View File

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

View File

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

View File

@@ -15,6 +15,24 @@ describe("isEditable", () => {
el.remove();
});
it.each(["checkbox", "radio", "range", "submit"])(
"is false for an <input type=%s>",
(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);

View File

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