diff --git a/src/lib/layout/ContextMenu.svelte b/src/lib/layout/ContextMenu.svelte index d46ea9d..fc53b8a 100644 --- a/src/lib/layout/ContextMenu.svelte +++ b/src/lib/layout/ContextMenu.svelte @@ -14,7 +14,8 @@ let { x, y, items, onClose, label = "Context actions" }: Props = $props(); $effect(() => { - const menuX = x, menuY = y, entries = items, name = label; + // Read the items once. A change of state behind an open menu, such as a template reload, must not rebuild it and close its submenu. + const menuX = x, menuY = y, name = label, entries = untrack(() => items); const previous = document.activeElement instanceof HTMLElement ? document.activeElement : null; function close(restoreFocus = false) { root.remove(); diff --git a/tests/smoke/workspace/manageTemplates.smoke.mjs b/tests/smoke/workspace/manageTemplates.smoke.mjs index b8643d2..391c34a 100644 --- a/tests/smoke/workspace/manageTemplates.smoke.mjs +++ b/tests/smoke/workspace/manageTemplates.smoke.mjs @@ -33,6 +33,13 @@ await runSmoke(name, async ({ page }) => { await row("a.md").waitFor(); await page.getByRole("navigation", { name: "Workspace files" }).click({ button: "right", position: { x: 90, y: 360 } }); await item("New note").hover(); + // A template reload behind the open menu keeps the menu and its open submenu (src/lib/layout/ContextMenu.svelte). + const submenu = page.getByRole("menu", { name: "New note", exact: true }); + await submenu.waitFor(); + await submenu.evaluate(node => { node.dataset.smokeMark = "open"; }); + await page.evaluate(() => window.dispatchEvent(new Event("runestone-templates-changed"))); + await page.waitForTimeout(300); + assert.equal(await page.locator('[data-smoke-mark="open"]:popover-open').count(), 1, "A template reload keeps the open submenu"); await item("Daily").click(); await row("Daily.md").waitFor(); assert.equal(await file("Daily.md"), "# Daily\n\n## Tasks");