fix(desktop): keep context submenu open while scrolling
This commit is contained in:
1 parent
2980e32c9d
commit
7ae36ee832
4 files changed
+96
-5
No files matched your search
@@ -96,4 +96,46 @@ describe("CustomContextMenu lifecycle", () => {
|
||||
|
||||
app.unmount();
|
||||
});
|
||||
|
||||
it("keeps the menu open when scrolling inside a scrollable submenu", async () => {
|
||||
const children = Array.from({ length: 40 }, (_, index) => ({ label: `Copy option ${index}` }));
|
||||
const root = defineComponent({
|
||||
setup() {
|
||||
return () =>
|
||||
h(
|
||||
CustomContextMenu,
|
||||
{ items: [{ label: "Copy", children }] },
|
||||
{
|
||||
default: ({ onContextMenu }: { onContextMenu: (event: MouseEvent) => void }) => h("div", { id: "context-target", onContextmenu: onContextMenu }, "Target"),
|
||||
},
|
||||
);
|
||||
},
|
||||
});
|
||||
const container = document.createElement("div");
|
||||
mountedContainers.push(container);
|
||||
document.body.append(container);
|
||||
const app = createApp(root);
|
||||
app.mount(container);
|
||||
|
||||
const target = container.querySelector("#context-target");
|
||||
target?.dispatchEvent(new MouseEvent("contextmenu", { bubbles: true, clientX: 10, clientY: 20 }));
|
||||
await nextTick();
|
||||
|
||||
const copyTrigger = Array.from(document.body.querySelectorAll("button")).find((button) => button.textContent?.includes("Copy"));
|
||||
expect(copyTrigger).toBeTruthy();
|
||||
copyTrigger?.dispatchEvent(new MouseEvent("mouseenter", { bubbles: true, clientX: 20, clientY: 30 }));
|
||||
await nextTick();
|
||||
|
||||
const submenu = Array.from(document.body.querySelectorAll("[data-dbx-context-menu]")).find((el) => el.textContent?.includes("Copy option 0"));
|
||||
expect(submenu).toBeTruthy();
|
||||
submenu?.dispatchEvent(new Event("scroll", { bubbles: true }));
|
||||
await nextTick();
|
||||
expect(document.body.textContent).toContain("Copy option 0");
|
||||
|
||||
document.dispatchEvent(new Event("scroll", { bubbles: true }));
|
||||
await nextTick();
|
||||
expect(document.body.textContent).not.toContain("Copy option 0");
|
||||
|
||||
app.unmount();
|
||||
});
|
||||
});
|
||||
+26
-1
@@ -1,5 +1,8 @@
|
||||
/**
|
||||
* @vitest-environment happy-dom
|
||||
*/
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import { createContextMenuRegistry } from "@/components/ui/customContextMenuRegistry";
|
||||
import { CONTEXT_MENU_SCROLL_ROOT_ATTR, createContextMenuRegistry } from "@/components/ui/customContextMenuRegistry";
|
||||
|
||||
function callsFor(spy: ReturnType<typeof vi.spyOn>, eventName: string) {
|
||||
return spy.mock.calls.filter(([name]) => name === eventName);
|
||||
@@ -34,6 +37,28 @@ describe("customContextMenuRegistry", () => {
|
||||
registrations.forEach((registration) => registration.dispose());
|
||||
});
|
||||
|
||||
it("does not close open menus when scroll originates inside a context menu root", () => {
|
||||
const documentTarget = document;
|
||||
const windowTarget = window;
|
||||
const registry = createContextMenuRegistry(documentTarget, windowTarget);
|
||||
const closeMenu = vi.fn();
|
||||
const registration = registry.register(closeMenu);
|
||||
registration.setOpen(true);
|
||||
|
||||
const menuRoot = document.createElement("div");
|
||||
menuRoot.setAttribute(CONTEXT_MENU_SCROLL_ROOT_ATTR, "");
|
||||
document.body.append(menuRoot);
|
||||
|
||||
menuRoot.dispatchEvent(new Event("scroll", { bubbles: true }));
|
||||
expect(closeMenu).not.toHaveBeenCalled();
|
||||
|
||||
document.dispatchEvent(new Event("scroll", { bubbles: true }));
|
||||
expect(closeMenu).toHaveBeenCalledOnce();
|
||||
|
||||
menuRoot.remove();
|
||||
registration.dispose();
|
||||
});
|
||||
|
||||
it("removes disposed callbacks and detaches listeners after the final host", () => {
|
||||
const documentTarget = new EventTarget();
|
||||
const windowTarget = new EventTarget();
|
||||
|
||||
Reference in new issue
Block a user