Skip to content

Commit 418ec99

Browse files
committed
fix empty context menu to not show
1 parent 3ee657c commit 418ec99

2 files changed

Lines changed: 15 additions & 3 deletions

File tree

packages/core/src/parts/contextmenu.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,14 @@ export class LyraContextMenu extends LyraElement {
140140
this.contributions = [...wildcard, ...categoryMatches, ...specific];
141141
}
142142

143+
/** Returns true when registry contributions or part-supplied menu content exist. */
144+
private hasMenuBody(): boolean {
145+
this.refreshContributions();
146+
if (this.contributions.length > 0) return true;
147+
const partContent = this.partContextMenuRenderer ? this.partContextMenuRenderer() : nothing;
148+
return partContent !== nothing;
149+
}
150+
143151
/**
144152
* Gets the element at the given point, traversing shadow DOM boundaries recursively.
145153
* This is necessary because elementFromPoint() doesn't penetrate shadow roots.
@@ -187,7 +195,9 @@ export class LyraContextMenu extends LyraElement {
187195
}
188196
}
189197

190-
public show(position: { x: number, y: number }, mouseEvent?: MouseEvent) {
198+
/** Opens the menu at `position`. Returns false when there is nothing to show (no thin empty panel). */
199+
public show(position: { x: number, y: number }, mouseEvent?: MouseEvent): boolean {
200+
if (!this.hasMenuBody()) return false;
191201
if (mouseEvent) {
192202
this.triggerClickUnderCursor(mouseEvent);
193203
}
@@ -196,6 +206,7 @@ export class LyraContextMenu extends LyraElement {
196206
this.updateComplete.then(() => {
197207
document.addEventListener('pointerdown', this.boundHandleDocumentPointerDown, { capture: true });
198208
});
209+
return true;
199210
}
200211

201212
private onClose() {

packages/core/src/parts/part.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,9 @@ export abstract class LyraPart extends LyraContainer {
147147
private onContentContextMenu = (event: MouseEvent): void => {
148148
const contextMenu = this.renderRoot.querySelector('lyra-contextmenu') as LyraContextMenu | null;
149149
if (!contextMenu) return;
150-
event.preventDefault();
151-
contextMenu.show({ x: event.clientX, y: event.clientY }, event);
150+
if (contextMenu.show({ x: event.clientX, y: event.clientY }, event)) {
151+
event.preventDefault();
152+
}
152153
};
153154

154155
private syncIsEditorCapability(): void {

0 commit comments

Comments
 (0)