Skip to content

Commit d2435cb

Browse files
committed
refactor(ui): centralize dropdown menu lifecycle
1 parent 1b82131 commit d2435cb

4 files changed

Lines changed: 68 additions & 70 deletions

File tree

‎ui/src/components/catalog-session-menu.ts‎

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { html } from "lit";
22
import { property } from "lit/decorators.js";
33
import { t } from "../i18n/index.ts";
44
import { OpenClawLightDomElement } from "../lit/openclaw-element.ts";
5+
import { DropdownMenuController } from "./dropdown-menu-controller.ts";
56
import { icons } from "./icons.ts";
67
import { promoteToPopoverTopLayer } from "./menu-surface.ts";
78
import "./web-awesome.ts";
@@ -16,36 +17,16 @@ class CatalogSessionMenu extends OpenClawLightDomElement {
1617
@property({ attribute: false }) terminalDisabled = false;
1718
@property({ attribute: false }) onAction: (action: CatalogSessionMenuAction) => void = () => {};
1819
@property({ attribute: false }) onClose: () => void = () => {};
20+
readonly menuLifecycle = new DropdownMenuController(this, {
21+
getTrigger: () => this.trigger,
22+
onClose: () => this.onClose(),
23+
});
1924

2025
override connectedCallback() {
2126
super.connectedCallback();
22-
document.addEventListener("keydown", this.handleDocumentKeydown, true);
2327
promoteToPopoverTopLayer(this);
2428
}
2529

26-
override disconnectedCallback() {
27-
document.removeEventListener("keydown", this.handleDocumentKeydown, true);
28-
super.disconnectedCallback();
29-
}
30-
31-
protected override firstUpdated(): void {
32-
const dropdown = this.querySelector<HTMLElement & { updateComplete?: Promise<unknown> }>(
33-
"wa-dropdown",
34-
);
35-
void Promise.resolve(dropdown?.updateComplete).then(() => {
36-
this.querySelector<HTMLElement>("wa-dropdown-item:not([disabled])")?.focus();
37-
});
38-
}
39-
40-
private readonly handleDocumentKeydown = (event: KeyboardEvent) => {
41-
if (event.key === "Escape") {
42-
event.preventDefault();
43-
event.stopPropagation();
44-
this.trigger?.focus();
45-
this.onClose();
46-
}
47-
};
48-
4930
private run(action: CatalogSessionMenuAction) {
5031
// Dispatch while the controller still owns the menu snapshot; close clears it synchronously.
5132
this.onAction(action);
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import type { ReactiveController, ReactiveControllerHost } from "lit";
2+
3+
type DropdownMenuHost = ReactiveControllerHost & HTMLElement;
4+
5+
export class DropdownMenuController implements ReactiveController {
6+
private generation = 0;
7+
8+
constructor(
9+
private readonly host: DropdownMenuHost,
10+
private readonly options: {
11+
getTrigger: () => HTMLElement | null;
12+
onClose: () => void;
13+
onKeydown?: (event: KeyboardEvent) => void;
14+
},
15+
) {
16+
host.addController(this);
17+
}
18+
19+
hostConnected(): void {
20+
document.addEventListener("keydown", this.handleDocumentKeydown, true);
21+
const generation = ++this.generation;
22+
void this.focusFirstItem(generation);
23+
}
24+
25+
hostDisconnected(): void {
26+
this.generation += 1;
27+
document.removeEventListener("keydown", this.handleDocumentKeydown, true);
28+
}
29+
30+
private readonly handleDocumentKeydown = (event: KeyboardEvent) => {
31+
if (event.key !== "Escape") {
32+
this.options.onKeydown?.(event);
33+
return;
34+
}
35+
event.preventDefault();
36+
event.stopPropagation();
37+
this.options.getTrigger()?.focus();
38+
this.options.onClose();
39+
};
40+
41+
private async focusFirstItem(generation: number): Promise<void> {
42+
await this.host.updateComplete;
43+
const dropdown = this.host.querySelector<HTMLElement & { updateComplete?: Promise<unknown> }>(
44+
"wa-dropdown",
45+
);
46+
await dropdown?.updateComplete;
47+
if (this.host.isConnected && generation === this.generation) {
48+
this.host.querySelector<HTMLElement>("wa-dropdown-item:not([disabled])")?.focus();
49+
}
50+
}
51+
}

‎ui/src/components/native-link-menu.ts‎

Lines changed: 6 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { html } from "lit";
22
import { property } from "lit/decorators.js";
33
import { t } from "../i18n/index.ts";
44
import { OpenClawLightDomElement } from "../lit/openclaw-element.ts";
5+
import { DropdownMenuController } from "./dropdown-menu-controller.ts";
56
import { icons } from "./icons.ts";
67
import { activateMenuShortcut, menuShortcutHint } from "./menu-shortcuts.ts";
78
import "./web-awesome.ts";
@@ -15,35 +16,11 @@ export class NativeLinkMenu extends OpenClawLightDomElement {
1516
@property({ attribute: false }) onAction: (action: NativeLinkMenuAction) => void = () => {};
1617
@property({ attribute: false }) onClose: () => void = () => {};
1718

18-
override connectedCallback() {
19-
super.connectedCallback();
20-
document.addEventListener("keydown", this.handleDocumentKeydown, true);
21-
}
22-
23-
override disconnectedCallback() {
24-
document.removeEventListener("keydown", this.handleDocumentKeydown, true);
25-
super.disconnectedCallback();
26-
}
27-
28-
protected override firstUpdated() {
29-
const dropdown = this.querySelector<HTMLElement & { updateComplete?: Promise<unknown> }>(
30-
"wa-dropdown",
31-
);
32-
void Promise.resolve(dropdown?.updateComplete).then(() => {
33-
this.querySelector<HTMLElement>("wa-dropdown-item:not([disabled])")?.focus();
34-
});
35-
}
36-
37-
private readonly handleDocumentKeydown = (event: KeyboardEvent) => {
38-
if (event.key === "Escape") {
39-
event.preventDefault();
40-
event.stopPropagation();
41-
this.trigger?.focus();
42-
this.onClose();
43-
return;
44-
}
45-
activateMenuShortcut(this, event);
46-
};
19+
readonly menuLifecycle = new DropdownMenuController(this, {
20+
getTrigger: () => this.trigger,
21+
onClose: () => this.onClose(),
22+
onKeydown: (event) => activateMenuShortcut(this, event),
23+
});
4724

4825
private runAction(action: NativeLinkMenuAction) {
4926
this.onClose();

‎ui/src/components/session-menu.ts‎

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
import { t } from "../i18n/index.ts";
1111
import { EDITOR_IDS, EDITOR_LABELS, type EditorId } from "../lib/editor-links.ts";
1212
import { OpenClawLightDomElement } from "../lit/openclaw-element.ts";
13+
import { DropdownMenuController } from "./dropdown-menu-controller.ts";
1314
import { icons } from "./icons.ts";
1415
import { activateMenuShortcut, menuShortcutHint } from "./menu-shortcuts.ts";
1516
import { promoteToPopoverTopLayer } from "./menu-surface.ts";
@@ -83,33 +84,21 @@ class SessionMenu extends OpenClawLightDomElement {
8384
@property({ attribute: false }) onAction: (action: SessionMenuAction) => void = () => {};
8485
@property({ attribute: false }) onClose: () => void = () => {};
8586
@state() private iconPickerOpen = false;
87+
readonly menuLifecycle = new DropdownMenuController(this, {
88+
getTrigger: () => this.trigger,
89+
onClose: () => this.onClose(),
90+
onKeydown: (event) => activateMenuShortcut(this, event),
91+
});
8692

8793
override connectedCallback() {
8894
super.connectedCallback();
89-
document.addEventListener("keydown", this.handleDocumentKeydown, true);
9095
// Sidebar-hosted menus live inside the nav stacking context (z-index 10),
9196
// which paints below the sidebar resizer divider (z-index 20); promoting
9297
// the menu to the popover top layer keeps app chrome from bleeding
9398
// through it (same pattern as openclaw-native-link-menu).
9499
promoteToPopoverTopLayer(this);
95100
}
96101

97-
override disconnectedCallback() {
98-
document.removeEventListener("keydown", this.handleDocumentKeydown, true);
99-
super.disconnectedCallback();
100-
}
101-
102-
private readonly handleDocumentKeydown = (event: KeyboardEvent) => {
103-
if (event.key === "Escape") {
104-
event.preventDefault();
105-
event.stopPropagation();
106-
this.trigger?.focus();
107-
this.onClose();
108-
return;
109-
}
110-
activateMenuShortcut(this, event);
111-
};
112-
113102
private runAction(action: SessionMenuAction) {
114103
this.onClose();
115104
this.onAction(action);

0 commit comments

Comments
 (0)