fix(ui): open the menu item the pointer stops on once the submenu grace ends

This commit is contained in:
neriousy 2026-11-19 14:43:15 +01:00
parent 88c90d08ad
commit 5180ce63fc
2 changed files with 114 additions and 18 deletions

View file

@ -1965,6 +1965,8 @@ test("the Windows titlebar menu keeps a submenu open while the pointer crosses o
await mockWorkspace(page, { name: "WindowsMenu", sessions: [] })
await page.goto(`/e2e/utils/windows-menu.html?${new URLSearchParams({ server: SERVER })}`)
await page.getByRole("button", { name: "OpenCode menu", exact: true }).click()
// Only `runFor` moves time, so Playwright's round trips don't slow the pointer below the speed the move simulates.
await holdClock(page)
const file = page.getByRole("menuitem", { name: "File", exact: true })
const fileBox = (await file.boundingBox())!
const from = { x: fileBox.x + fileBox.width / 2, y: fileBox.y + fileBox.height / 2 }
@ -1994,6 +1996,8 @@ test("the Windows titlebar menu highlights each parent item the pointer drifts d
await mockWorkspace(page, { name: "WindowsMenu", sessions: [] })
await page.goto(`/e2e/utils/windows-menu.html?${new URLSearchParams({ server: SERVER })}`)
await page.getByRole("button", { name: "OpenCode menu", exact: true }).click()
// Only `runFor` moves time, so the pointer doesn't linger on the items it crosses on the way to View.
await holdClock(page)
const view = page.getByRole("menuitem", { name: "View", exact: true })
const box = (await view.boundingBox())!
const from = { x: box.x + box.width / 2, y: box.y + box.height / 2 }
@ -2012,6 +2016,36 @@ test("the Windows titlebar menu highlights each parent item the pointer drifts d
await expect(go).toHaveAttribute("data-highlighted", "")
})
// A pointer that stopped on another parent item while heading for the open submenu left that item dead until the
// pointer moved again, with the submenu it never reached still open.
test("the Windows titlebar menu opens the parent item the pointer stops on while heading for another submenu", async ({
page,
}) => {
await page.clock.install()
await mockWorkspace(page, { name: "WindowsMenu", sessions: [] })
await page.goto(`/e2e/utils/windows-menu.html?${new URLSearchParams({ server: SERVER })}`)
await page.getByRole("button", { name: "OpenCode menu", exact: true }).click()
await holdClock(page)
const edit = page.getByRole("menuitem", { name: "Edit", exact: true })
const view = page.getByRole("menuitem", { name: "View", exact: true })
const editBox = (await edit.boundingBox())!
await page.mouse.move(editBox.x + 20, editBox.y + editBox.height / 2, { steps: 4 })
await page.clock.runFor(100)
await expect(edit).toHaveAttribute("aria-expanded", "true")
// One move down and to the right lands on View, on the way to Edit's submenu, and the pointer stops there.
const viewBox = (await view.boundingBox())!
await page.mouse.move(viewBox.x + viewBox.width / 2, viewBox.y + viewBox.height / 2)
await expect(edit).toHaveAttribute("data-highlighted", "")
// Kobalte's 300ms way to the submenu, then its hover delay.
await page.clock.runFor(300)
await expect(view).toHaveAttribute("data-highlighted", "")
await page.clock.runFor(100)
await expect(view).toHaveAttribute("aria-expanded", "true")
await expect(page.locator('[role="menu"].desktop-app-menu-sub')).toHaveCount(1)
})
test("the Windows menu shares the vertical sidebar's icon lane", async ({ page }) => {
await mockWorkspace(page, {
name: "WindowsMenu",

View file

@ -1,16 +1,17 @@
diff --git a/dist/chunk/3ARTUDH2.jsx b/dist/chunk/3ARTUDH2.jsx
index 4a2cfa59cfd0adec119e46de043c590af12ceb60..f132981eadbeaadeb579fa8e7ccc9fbe70459efc 100644
index 4a2cfa59cfd0adec119e46de043c590af12ceb60..eeb910652f9f6b150a5bf348f574d477ef70545d 100644
--- a/dist/chunk/3ARTUDH2.jsx
+++ b/dist/chunk/3ARTUDH2.jsx
@@ -197,6 +197,7 @@ function Menu(props) {
@@ -197,6 +197,8 @@ function Menu(props) {
]);
let pointerGraceTimeoutId = 0;
let pointerGraceIntent = null;
+ let pointerGraceOrigin = [0, 0];
+ let pointerGraceHeld;
let pointerDir = "right";
const [triggerId, setTriggerId] = createSignal();
const [contentId, setContentId] = createSignal();
@@ -263,7 +264,32 @@ function Menu(props) {
@@ -263,12 +265,55 @@ function Menu(props) {
};
const isPointerMovingToSubmenu = (e) => {
const isMovingTowards = pointerDir === pointerGraceIntent?.side;
@ -37,14 +38,37 @@ index 4a2cfa59cfd0adec119e46de043c590af12ceb60..f132981eadbeaadeb579fa8e7ccc9fbe
+ }
+ pointerGraceOrigin = [e.clientX, e.clientY];
+ window.clearTimeout(pointerGraceTimeoutId);
+ pointerGraceTimeoutId = window.setTimeout(() => {
+ pointerGraceIntent = null;
+ }, 300);
+ pointerGraceTimeoutId = window.setTimeout(endPointerGrace, 300);
+ return true;
+ };
+ // Patched: an item the pointer came to rest on while the grace held it back gets the pointer once the grace ends, as
+ // if the pointer had moved on it again. Otherwise it stays dead until the pointer moves, while the submenu the pointer
+ // never reached stays open.
+ const endPointerGrace = () => {
+ pointerGraceIntent = null;
+ const held = pointerGraceHeld;
+ pointerGraceHeld = void 0;
+ if (!held?.element.isConnected || !held.element.matches(":hover")) {
+ return;
+ }
+ held.element.dispatchEvent(new PointerEvent("pointermove", {
+ bubbles: true,
+ pointerType: "mouse",
+ clientX: held.clientX,
+ clientY: held.clientY
+ }));
};
const onItemEnter = (e) => {
if (isPointerMovingToSubmenu(e)) {
@@ -277,9 +303,12 @@ function Menu(props) {
e.preventDefault();
+ pointerGraceHeld = { element: e.currentTarget, clientX: e.clientX, clientY: e.clientY };
+ return;
}
+ pointerGraceHeld = void 0;
};
const onItemLeave = (e) => {
if (isPointerMovingToSubmenu(e)) {
@@ -277,9 +322,12 @@ function Menu(props) {
focusContent();
};
const onTriggerLeave = (e) => {
@ -57,20 +81,27 @@ index 4a2cfa59cfd0adec119e46de043c590af12ceb60..f132981eadbeaadeb579fa8e7ccc9fbe
};
createHideOutside({
isDisabled: () => {
@@ -355,7 +384,11 @@ function Menu(props) {
@@ -355,7 +403,13 @@ function Menu(props) {
onTriggerLeave,
setPointerDir: (dir) => pointerDir = dir,
setPointerGraceTimeoutId: (id) => pointerGraceTimeoutId = id,
- setPointerGraceIntent: (intent) => pointerGraceIntent = intent,
+ endPointerGrace,
+ setPointerGraceIntent: (intent) => {
+ pointerGraceIntent = intent;
+ pointerGraceHeld = void 0;
+ // The grace area starts 5px behind the pointer.
+ pointerGraceOrigin = intent ? [intent.area[0][0] + (intent.side === "right" ? 5 : -5), intent.area[0][1]] : [0, 0];
+ },
registerNestedMenu,
registerItemToParentDomCollection: parentDomCollectionContext?.registerItem,
registerTriggerId: createRegisterId(setTriggerId),
@@ -1503,8 +1536,7 @@ function MenuSubTrigger(props) {
@@ -1499,12 +1553,11 @@ function MenuSubTrigger(props) {
});
window.clearTimeout(parentMenuContext?.pointerGraceTimeoutId());
const pointerGraceTimeoutId = window.setTimeout(() => {
- parentMenuContext?.setPointerGraceIntent(null);
+ parentMenuContext?.endPointerGrace();
}, 300);
parentMenuContext?.setPointerGraceTimeoutId(pointerGraceTimeoutId);
} else {
@ -81,18 +112,19 @@ index 4a2cfa59cfd0adec119e46de043c590af12ceb60..f132981eadbeaadeb579fa8e7ccc9fbe
}
parentMenuContext?.setPointerGraceIntent(null);
diff --git a/dist/chunk/HGKTPMYY.js b/dist/chunk/HGKTPMYY.js
index a8fa9b06a97cb3d4d5029fc0c929d78dfdc6edd4..42fb6a330c42eaf0479360cb121c6e2a7c9bd401 100644
index a8fa9b06a97cb3d4d5029fc0c929d78dfdc6edd4..6c005c52ccf0a84dbd38415c21dfd2c5ad43af5b 100644
--- a/dist/chunk/HGKTPMYY.js
+++ b/dist/chunk/HGKTPMYY.js
@@ -119,6 +119,7 @@ function Menu(props) {
@@ -119,6 +119,8 @@ function Menu(props) {
const [local, others] = splitProps(mergedProps, ["open", "defaultOpen", "onOpenChange"]);
let pointerGraceTimeoutId = 0;
let pointerGraceIntent = null;
+ let pointerGraceOrigin = [0, 0];
+ let pointerGraceHeld;
let pointerDir = "right";
const [triggerId, setTriggerId] = createSignal();
const [contentId, setContentId] = createSignal();
@@ -187,7 +188,32 @@ function Menu(props) {
@@ -187,12 +189,55 @@ function Menu(props) {
};
const isPointerMovingToSubmenu = (e) => {
const isMovingTowards = pointerDir === pointerGraceIntent?.side;
@ -119,14 +151,37 @@ index a8fa9b06a97cb3d4d5029fc0c929d78dfdc6edd4..42fb6a330c42eaf0479360cb121c6e2a
+ }
+ pointerGraceOrigin = [e.clientX, e.clientY];
+ window.clearTimeout(pointerGraceTimeoutId);
+ pointerGraceTimeoutId = window.setTimeout(() => {
+ pointerGraceIntent = null;
+ }, 300);
+ pointerGraceTimeoutId = window.setTimeout(endPointerGrace, 300);
+ return true;
+ };
+ // Patched: an item the pointer came to rest on while the grace held it back gets the pointer once the grace ends, as
+ // if the pointer had moved on it again. Otherwise it stays dead until the pointer moves, while the submenu the pointer
+ // never reached stays open.
+ const endPointerGrace = () => {
+ pointerGraceIntent = null;
+ const held = pointerGraceHeld;
+ pointerGraceHeld = void 0;
+ if (!held?.element.isConnected || !held.element.matches(":hover")) {
+ return;
+ }
+ held.element.dispatchEvent(new PointerEvent("pointermove", {
+ bubbles: true,
+ pointerType: "mouse",
+ clientX: held.clientX,
+ clientY: held.clientY
+ }));
};
const onItemEnter = (e) => {
if (isPointerMovingToSubmenu(e)) {
@@ -201,9 +227,12 @@ function Menu(props) {
e.preventDefault();
+ pointerGraceHeld = { element: e.currentTarget, clientX: e.clientX, clientY: e.clientY };
+ return;
}
+ pointerGraceHeld = void 0;
};
const onItemLeave = (e) => {
if (isPointerMovingToSubmenu(e)) {
@@ -201,9 +246,12 @@ function Menu(props) {
focusContent();
};
const onTriggerLeave = (e) => {
@ -139,20 +194,27 @@ index a8fa9b06a97cb3d4d5029fc0c929d78dfdc6edd4..42fb6a330c42eaf0479360cb121c6e2a
};
createHideOutside({
isDisabled: () => {
@@ -276,7 +305,11 @@ function Menu(props) {
@@ -276,7 +324,13 @@ function Menu(props) {
onTriggerLeave,
setPointerDir: (dir) => pointerDir = dir,
setPointerGraceTimeoutId: (id) => pointerGraceTimeoutId = id,
- setPointerGraceIntent: (intent) => pointerGraceIntent = intent,
+ endPointerGrace,
+ setPointerGraceIntent: (intent) => {
+ pointerGraceIntent = intent;
+ pointerGraceHeld = void 0;
+ // The grace area starts 5px behind the pointer.
+ pointerGraceOrigin = intent ? [intent.area[0][0] + (intent.side === "right" ? 5 : -5), intent.area[0][1]] : [0, 0];
+ },
registerNestedMenu,
registerItemToParentDomCollection: parentDomCollectionContext?.registerItem,
registerTriggerId: createRegisterId(setTriggerId),
@@ -1217,8 +1250,7 @@ function MenuSubTrigger(props) {
@@ -1213,12 +1267,11 @@ function MenuSubTrigger(props) {
});
window.clearTimeout(parentMenuContext?.pointerGraceTimeoutId());
const pointerGraceTimeoutId = window.setTimeout(() => {
- parentMenuContext?.setPointerGraceIntent(null);
+ parentMenuContext?.endPointerGrace();
}, 300);
parentMenuContext?.setPointerGraceTimeoutId(pointerGraceTimeoutId);
} else {