Skip to content

Commit 07f6096

Browse files
committed
Close menu recover active element
1 parent 25cede8 commit 07f6096

File tree

4 files changed

+18
-11
lines changed

4 files changed

+18
-11
lines changed

src/elements/menu.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,32 +7,35 @@ export interface MenuItem {
77
}
88

99
type MenuState = {
10+
activeElement: HTMLElement | null;
1011
open: boolean;
1112
menus: MenuItem[];
1213
x: number;
1314
y: number;
1415
};
1516

1617
export const menuStore = createStore<MenuState>({
18+
activeElement: null,
1719
open: false,
1820
menus: [],
1921
x: 0,
2022
y: 0,
2123
});
2224

23-
export function openMenu(x: number, y: number, menus: MenuItem[]) {
24-
updateStore(menuStore, { open: true, x, y, menus });
25+
export function openMenu(activeElement: HTMLElement | null, x: number, y: number, menus: MenuItem[]) {
26+
updateStore(menuStore, { open: true, x, y, menus, activeElement });
2527
}
2628

27-
export function closeMenu() {
29+
export function pointerDownHandle() {
30+
setTimeout(() => menuStore.activeElement?.focus());
2831
updateStore(menuStore, { open: false });
2932
}
3033

3134
@customElement('gem-panel-menu')
3235
@connectStore(menuStore)
3336
export class GemPanelMenuElement extends GemElement {
3437
mounted = () => {
35-
this.addEventListener('click', closeMenu);
38+
this.addEventListener('pointerdown', pointerDownHandle);
3639
};
3740

3841
render = () => {
@@ -84,7 +87,7 @@ export class GemPanelMenuElement extends GemElement {
8487
</style>
8588
<div part="menu" class="menu">
8689
${menuStore.menus.map(
87-
({ text, handle }) => html`<div part="menu-item" class="item" @click=${handle}>${text}</div>`,
90+
({ text, handle }) => html`<div part="menu-item" class="item" @pointerdown=${handle}>${text}</div>`,
8891
)}
8992
</div>
9093
`;

src/elements/panel-title.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ export class GemPanelTitleElement extends GemElement {
99
window: Window;
1010
panel: Panel;
1111

12-
#clickHandle = (evt: MouseEvent) => {
12+
// https://bugs.chromium.org/p/chromium/issues/detail?id=1206640
13+
#pointerDownHandle = (evt: MouseEvent) => {
1314
evt.stopPropagation();
1415
const { window, panel } = this;
1516
const defaultMenus = [
@@ -22,7 +23,13 @@ export class GemPanelTitleElement extends GemElement {
2223
handle: () => closeWindow({ window }),
2324
},
2425
];
25-
openMenu(evt.x, evt.y, [...(store.openPanelMenuBefore?.(panel, window) || []), ...defaultMenus]);
26+
setTimeout(() => {
27+
const activeElement = (this.getRootNode() as ShadowRoot)?.activeElement;
28+
openMenu(activeElement as HTMLElement, evt.x, evt.y, [
29+
...(store.openPanelMenuBefore?.(panel, window) || []),
30+
...defaultMenus,
31+
]);
32+
});
2633
};
2734

2835
render = () => {
@@ -73,7 +80,7 @@ export class GemPanelTitleElement extends GemElement {
7380
}
7481
</style>
7582
<slot></slot>
76-
<span part="panel-button" class="panel-button" @pointerdown=${this.#clickHandle}></span>
83+
<span part="panel-button" class="panel-button" @pointerdown=${this.#pointerDownHandle}></span>
7784
`;
7885
};
7986
}

src/elements/root.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,6 @@ export class GemPanelElement extends GemElement {
184184
panel-button,
185185
panel-loader
186186
"
187-
.fixed=${!window.isGridWindow()}
188187
.window=${window}
189188
></gem-panel-window>
190189
`,

src/elements/window.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -281,8 +281,6 @@ export class GemPanelWindowElement extends GemElement<State> {
281281
z-index: 1;
282282
height: 0;
283283
flex-grow: 1;
284-
display: flex;
285-
flex-direction: column;
286284
}
287285
:is(.window, .content):is(:focus, :focus-visible) {
288286
outline: none;

0 commit comments

Comments
 (0)