Skip to content

Commit

Permalink
Fix menu button double-creates a context menu in some browsers
Browse files Browse the repository at this point in the history
  • Loading branch information
personalizedrefrigerator committed Aug 27, 2024
1 parent cf04894 commit 28714fe
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions packages/js-draw/src/tools/SelectionTool/SelectionTopMenu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export default class SelectionTopMenu implements SelectionBoxChild {
const anchor = this.getBBoxCanvasCoords().center;
showContextMenu(anchor);
};
this.addButton('...', (_event) => {
this.addButton('...', () => {
this.onClick();
});

Expand All @@ -49,16 +49,19 @@ export default class SelectionTopMenu implements SelectionBoxChild {

public remove() { this.element.remove(); }

private addButton(label: string, onClick: (event: MouseEvent)=>void) {
private addButton(label: string, onActivate: ()=>void) {
const button = document.createElement('button');
button.textContent = label;
button.onclick = (event) => {
onClick(event);
// To prevent editor event handlers from conflicting with those for the button,
// don't register a [click] handler. An onclick handler can be fired incorrectly
// in this case (in Chrome) after onClick is fired in onDragEnd, leading to a double
// on-click action.
button.onkeydown = (event) => {
if (event.key === 'Enter') onActivate();
};
this.element.appendChild(button);

// Update the bounding box of this in response to the new button.
// TODO: Use a ResizeObserver when available.
requestAnimationFrame(() => {
this.updatePosition();
});
Expand Down

0 comments on commit 28714fe

Please sign in to comment.