Skip to content

Commit d22cf89

Browse files
committed
[frontend] close menu when item clicked
1 parent 03d74be commit d22cf89

File tree

1 file changed

+16
-17
lines changed

1 file changed

+16
-17
lines changed

frontend/src/Menu.tsx

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ export const Menu: Component<{
66
y: number;
77
items: { label: string; action?: () => void }[];
88
}> = function (cx) {
9+
const close = () => {
10+
cx.root.remove();
11+
browser.unfocusframes = false;
12+
};
13+
914
cx.mount = () => {
1015
browser.unfocusframes = true;
1116
document.body.appendChild(cx.root);
@@ -15,22 +20,8 @@ export const Menu: Component<{
1520
if (this.x > maxX) this.x = maxX;
1621
if (this.y > maxY) this.y = maxY;
1722

18-
document.body.addEventListener(
19-
"click",
20-
() => {
21-
cx.root.remove();
22-
browser.unfocusframes = false;
23-
},
24-
{ once: true }
25-
);
26-
document.body.addEventListener(
27-
"contextmenu",
28-
() => {
29-
cx.root.remove();
30-
browser.unfocusframes = false;
31-
},
32-
{ once: true }
33-
);
23+
document.body.addEventListener("click", close, { once: true });
24+
document.body.addEventListener("contextmenu", close, { once: true });
3425

3526
cx.root.addEventListener("click", (e) => {
3627
e.stopPropagation();
@@ -39,7 +30,15 @@ export const Menu: Component<{
3930
return (
4031
<div style={use`--x: ${this.x}px; --y: ${this.y}px;`}>
4132
{use(this.items).mapEach((item) => (
42-
<button on:click={() => item.action?.()}>{item.label}</button>
33+
<button
34+
on:click={(e: MouseEvent) => {
35+
item.action?.();
36+
close();
37+
e.stopPropagation();
38+
}}
39+
>
40+
{item.label}
41+
</button>
4342
))}
4443
</div>
4544
);

0 commit comments

Comments
 (0)