mirror of
https://github.com/rommapp/romm.git
synced 2026-12-03 10:33:22 +01:00
fix(v2): close select, combobox and date popovers before their overlay
RSelect, RComboboxField and RDateField never registered on the escape stack, whose window capture listener runs first. Escape (or pad B) with one of them open inside a dialog closed the dialog and left the popover open; outside an overlay, pad B navigated back. Add useEscapable and register each popover while its panel is shown. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XZW9ChvmiTKXYYWY6JvTKU
This commit is contained in:
@@ -1,6 +1,11 @@
|
||||
import { mount } from "@vue/test-utils";
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||
import { nextTick } from "vue";
|
||||
import {
|
||||
type EscapableEntry,
|
||||
popEscapable,
|
||||
pushEscapable,
|
||||
} from "@/v2/lib/overlays/RDialog/escapeStack";
|
||||
import RComboboxField from "./RComboboxField.vue";
|
||||
|
||||
describe("RComboboxField rules", () => {
|
||||
@@ -35,3 +40,49 @@ describe("RComboboxField rules", () => {
|
||||
wrapper.unmount();
|
||||
});
|
||||
});
|
||||
|
||||
describe("RComboboxField inside an overlay", () => {
|
||||
const dialog: EscapableEntry = { close: vi.fn(), persistent: false };
|
||||
|
||||
afterEach(() => popEscapable(dialog));
|
||||
|
||||
function pressEscape(el: Element) {
|
||||
el.dispatchEvent(
|
||||
new KeyboardEvent("keydown", { key: "Escape", bubbles: true }),
|
||||
);
|
||||
}
|
||||
|
||||
it("closes its suggestions on Escape, leaving the dialog open", async () => {
|
||||
pushEscapable(dialog);
|
||||
const wrapper = mount(RComboboxField, {
|
||||
props: { modelValue: [], items: ["rpg", "racing"] },
|
||||
attachTo: document.body,
|
||||
});
|
||||
const input = wrapper.get("input");
|
||||
|
||||
await input.trigger("focus");
|
||||
expect(document.querySelector(".r-combobox-field__panel")).not.toBeNull();
|
||||
|
||||
pressEscape(input.element);
|
||||
await nextTick();
|
||||
|
||||
expect(document.querySelector(".r-combobox-field__panel")).toBeNull();
|
||||
expect(dialog.close).not.toHaveBeenCalled();
|
||||
wrapper.unmount();
|
||||
});
|
||||
|
||||
it("does not hold Escape back when it has nothing to show", async () => {
|
||||
pushEscapable(dialog);
|
||||
const wrapper = mount(RComboboxField, {
|
||||
props: { modelValue: [], items: [] },
|
||||
attachTo: document.body,
|
||||
});
|
||||
const input = wrapper.get("input");
|
||||
|
||||
await input.trigger("focus");
|
||||
pressEscape(input.element);
|
||||
|
||||
expect(dialog.close).toHaveBeenCalledOnce();
|
||||
wrapper.unmount();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -40,6 +40,7 @@ import {
|
||||
import { useChromeLabels } from "@/v2/lib/a11y/chromeLabels";
|
||||
import RIcon from "@/v2/lib/primitives/RIcon/RIcon.vue";
|
||||
import RTag from "@/v2/lib/primitives/RTag/RTag.vue";
|
||||
import { useEscapable } from "@/v2/lib/overlays/RDialog/useEscapable";
|
||||
import { useRFormRegistration } from "../RForm/context";
|
||||
|
||||
defineOptions({ inheritAttrs: false });
|
||||
@@ -299,6 +300,12 @@ onBeforeUnmount(() => {
|
||||
document.removeEventListener("pointerdown", onDocPointerDown, true);
|
||||
});
|
||||
|
||||
useEscapable(
|
||||
computed(() => isOpen.value && hasSuggestions.value),
|
||||
closePanel,
|
||||
() => panelRef.value,
|
||||
);
|
||||
|
||||
// Reset active highlight when the suggestion set changes.
|
||||
watch(suggestions, () => {
|
||||
activeIndex.value = -1;
|
||||
|
||||
@@ -1,5 +1,11 @@
|
||||
import { enableAutoUnmount, mount } from "@vue/test-utils";
|
||||
import { afterEach, beforeAll, describe, expect, it } from "vitest";
|
||||
import { afterEach, beforeAll, describe, expect, it, vi } from "vitest";
|
||||
import { nextTick } from "vue";
|
||||
import {
|
||||
type EscapableEntry,
|
||||
popEscapable,
|
||||
pushEscapable,
|
||||
} from "@/v2/lib/overlays/RDialog/escapeStack";
|
||||
import RDateField from "./RDateField.vue";
|
||||
|
||||
// Runs in a timezone west of UTC on purpose: the picker emits UTC midnight,
|
||||
@@ -138,3 +144,25 @@ describe("RDateField", () => {
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("RDateField inside an overlay", () => {
|
||||
const dialog: EscapableEntry = { close: vi.fn(), persistent: false };
|
||||
|
||||
afterEach(() => popEscapable(dialog));
|
||||
|
||||
it("closes the calendar on Escape and refocuses the field, leaving the dialog open", async () => {
|
||||
pushEscapable(dialog);
|
||||
const wrapper = await openPicker();
|
||||
expect(dayCell(15)).not.toBeNull();
|
||||
|
||||
dayCell(15)!.dispatchEvent(
|
||||
new KeyboardEvent("keydown", { key: "Escape", bubbles: true }),
|
||||
);
|
||||
await nextTick();
|
||||
await nextTick();
|
||||
|
||||
expect(dayCell(15)).toBeNull();
|
||||
expect(dialog.close).not.toHaveBeenCalled();
|
||||
expect(document.activeElement).toBe(wrapper.get("input").element);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -33,6 +33,7 @@ import {
|
||||
watch,
|
||||
} from "vue";
|
||||
import { useChromeLabels } from "@/v2/lib/a11y/chromeLabels";
|
||||
import { useEscapable } from "../../overlays/RDialog/useEscapable";
|
||||
import RBtn from "../../primitives/RBtn/RBtn.vue";
|
||||
import RIcon from "../../primitives/RIcon/RIcon.vue";
|
||||
import RTextField from "../RTextField/RTextField.vue";
|
||||
@@ -366,14 +367,7 @@ function onPanelKeydown(evt: KeyboardEvent) {
|
||||
case "Escape":
|
||||
evt.preventDefault();
|
||||
evt.stopPropagation();
|
||||
close();
|
||||
// Send focus back to the field so tab order doesn't get stranded
|
||||
// on a teleported panel that just unmounted.
|
||||
nextTick(() => {
|
||||
(
|
||||
referenceEl.value?.querySelector("input") as HTMLElement | null
|
||||
)?.focus();
|
||||
});
|
||||
dismiss();
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
@@ -448,6 +442,17 @@ onMounted(() => {
|
||||
onBeforeUnmount(() => {
|
||||
document.removeEventListener("pointerdown", onDocPointerDown, true);
|
||||
});
|
||||
|
||||
function dismiss() {
|
||||
close();
|
||||
// Send focus back to the field so tab order doesn't get stranded
|
||||
// on a teleported panel that just unmounted.
|
||||
nextTick(() => {
|
||||
(referenceEl.value?.querySelector("input") as HTMLElement | null)?.focus();
|
||||
});
|
||||
}
|
||||
|
||||
useEscapable(isOpen, dismiss, () => panelRef.value);
|
||||
</script>
|
||||
|
||||
<template>
|
||||
|
||||
@@ -1,7 +1,12 @@
|
||||
import { mount } from "@vue/test-utils";
|
||||
import { afterEach, describe, expect, it } from "vitest";
|
||||
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||
import { nextTick } from "vue";
|
||||
import { useInputModality } from "@/v2/composables/useInputModality";
|
||||
import {
|
||||
type EscapableEntry,
|
||||
popEscapable,
|
||||
pushEscapable,
|
||||
} from "@/v2/lib/overlays/RDialog/escapeStack";
|
||||
import RTooltip from "@/v2/lib/structural/RTooltip/RTooltip.vue";
|
||||
import RSelect from "./RSelect.vue";
|
||||
|
||||
@@ -193,3 +198,44 @@ describe("RSelect rules", () => {
|
||||
wrapper.unmount();
|
||||
});
|
||||
});
|
||||
|
||||
describe("RSelect inside an overlay", () => {
|
||||
const dialog: EscapableEntry = {
|
||||
close: vi.fn(),
|
||||
persistent: false,
|
||||
};
|
||||
|
||||
afterEach(() => {
|
||||
popEscapable(dialog);
|
||||
vi.mocked(dialog.close).mockClear();
|
||||
});
|
||||
|
||||
it("closes only its own menu on Escape, leaving the dialog open", async () => {
|
||||
pushEscapable(dialog);
|
||||
const wrapper = mount(RSelect, {
|
||||
props: { items: ["a", "b"], modelValue: "a", hideDetails: true },
|
||||
attachTo: document.body,
|
||||
});
|
||||
|
||||
await wrapper.get(".r-select__field").trigger("keydown", { key: "Enter" });
|
||||
expect(document.querySelector(".r-select__panel")).not.toBeNull();
|
||||
|
||||
wrapper
|
||||
.get(".r-select__field")
|
||||
.element.dispatchEvent(
|
||||
new KeyboardEvent("keydown", { key: "Escape", bubbles: true }),
|
||||
);
|
||||
await nextTick();
|
||||
|
||||
expect(document.querySelector(".r-select__panel")).toBeNull();
|
||||
expect(dialog.close).not.toHaveBeenCalled();
|
||||
|
||||
wrapper
|
||||
.get(".r-select__field")
|
||||
.element.dispatchEvent(
|
||||
new KeyboardEvent("keydown", { key: "Escape", bubbles: true }),
|
||||
);
|
||||
expect(dialog.close).toHaveBeenCalledOnce();
|
||||
wrapper.unmount();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -39,6 +39,7 @@ import {
|
||||
import { useInputModality } from "@/v2/composables/useInputModality";
|
||||
import { useChromeLabels } from "@/v2/lib/a11y/chromeLabels";
|
||||
import { shouldAutofocusSearch } from "@/v2/utils/autofocus";
|
||||
import { useEscapable } from "../../overlays/RDialog/useEscapable";
|
||||
import RDivider from "../../primitives/RDivider/RDivider.vue";
|
||||
import RIcon from "../../primitives/RIcon/RIcon.vue";
|
||||
import RProgressCircular from "../../primitives/RProgressCircular/RProgressCircular.vue";
|
||||
@@ -745,6 +746,16 @@ onBeforeUnmount(() => {
|
||||
document.removeEventListener("pointerdown", onDocPointerDown, true);
|
||||
});
|
||||
|
||||
useEscapable(
|
||||
isOpen,
|
||||
() => {
|
||||
const focusInPanel = !!panelRef.value?.contains(document.activeElement);
|
||||
closeMenu();
|
||||
if (focusInPanel) activatorRef.value?.focus();
|
||||
},
|
||||
() => panelRef.value,
|
||||
);
|
||||
|
||||
// Close when search is changed externally? No, keep open while
|
||||
// editing search. Reset active index to 0 when filter changes so
|
||||
// keyboard nav stays sensible.
|
||||
|
||||
28
frontend/src/v2/lib/overlays/RDialog/useEscapable.ts
Normal file
28
frontend/src/v2/lib/overlays/RDialog/useEscapable.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
import { onBeforeUnmount, type Ref, watch } from "vue";
|
||||
import {
|
||||
type EscapableEntry,
|
||||
popEscapable,
|
||||
pushEscapable,
|
||||
} from "./escapeStack";
|
||||
|
||||
/** Keep a popover on the escape stack while `isOpen`, so Escape and pad B
|
||||
* close it before any overlay it sits in. Returns the stack entry. */
|
||||
export function useEscapable(
|
||||
isOpen: Readonly<Ref<boolean>>,
|
||||
close: () => void,
|
||||
panel?: () => HTMLElement | null,
|
||||
): EscapableEntry {
|
||||
const entry: EscapableEntry = { close, persistent: false, panel };
|
||||
|
||||
watch(
|
||||
isOpen,
|
||||
(open) => {
|
||||
if (open) pushEscapable(entry);
|
||||
else popEscapable(entry);
|
||||
},
|
||||
{ immediate: true },
|
||||
);
|
||||
onBeforeUnmount(() => popEscapable(entry));
|
||||
|
||||
return entry;
|
||||
}
|
||||
Reference in New Issue
Block a user