Skip to content

Commit 105d04d

Browse files
committed
Update ContextMenu to fire show and hide events
1 parent e96c2b4 commit 105d04d

3 files changed

Lines changed: 25 additions & 10 deletions

File tree

examples/basic/index.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@ import {
88

99
mapboxgl.accessToken = import.meta.env.VITE_MAPBOX_ACCESS_TOKEN;
1010

11+
const hint = document.getElementById("hint");
12+
13+
function dismissHint() {
14+
if (hint && !hint.classList.contains("hidden")) {
15+
hint.classList.add("hidden");
16+
}
17+
}
18+
1119
const map = new mapboxgl.Map({
1220
container: "map",
1321
center: [-122.4194, 37.7749],
@@ -57,14 +65,6 @@ function createCommonItems() {
5765
];
5866
}
5967

60-
// Dismiss hint on first right-click
61-
const hint = document.getElementById("hint");
62-
map.on("contextmenu", () => {
63-
if (hint && !hint.classList.contains("hidden")) {
64-
hint.classList.add("hidden");
65-
}
66-
});
67-
6868
map.on("load", () => {
6969
// General context menu (anywhere on the map)
7070
const contextMenu = new MapboxContextMenu({ width: 200 });
@@ -73,6 +73,7 @@ map.on("load", () => {
7373
contextMenu.addItem(item);
7474
}
7575

76+
contextMenu.on("show", dismissHint);
7677
contextMenu.addTo(map);
7778

7879
// Building-specific context menu
@@ -97,5 +98,6 @@ map.on("load", () => {
9798

9899
buildingMenu.addItem(buildingInfoItem);
99100

101+
buildingMenu.on("show", dismissHint);
100102
buildingMenu.addTo(map, { featuresetId: "buildings", importId: "basemap" });
101103
});

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "mapbox-gl-contextmenu",
3-
"version": "1.4.0",
3+
"version": "1.5.0",
44
"description": "A contextmenu plugin for Mapbox GL JS",
55
"type": "module",
66
"main": "./dist/index.cjs",

src/components/ContextMenu/ContextMenu.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { ContextMenuContext, MenuItem } from "../../types";
2+
import { Evented } from "../../util/evented";
23
import { isFocusable } from "../../util/focusable";
34
import ContextMenuItem from "../ContextMenuItem/ContextMenuItem";
45
import ContextMenuSubmenu from "../ContextMenuSubmenu/ContextMenuSubmenu";
@@ -16,7 +17,14 @@ export interface ContextMenuOptions {
1617
width?: string | number;
1718
}
1819

19-
export default class ContextMenu {
20+
export type ContextMenuEvents = {
21+
/** Fired when the context menu is shown. */
22+
show: void;
23+
/** Fired when the context menu is hidden. */
24+
hide: void;
25+
};
26+
27+
export default class ContextMenu extends Evented<ContextMenuEvents> {
2028
protected _items: MenuItem[] = [];
2129
protected _className: string;
2230
protected _theme: ContextMenuTheme;
@@ -31,6 +39,7 @@ export default class ContextMenu {
3139
private _onEscapeLeft: (() => void) | null = null;
3240

3341
constructor(options?: ContextMenuOptions) {
42+
super();
3443
this._className = options?.className
3544
? `${styles.menu} ${options.className}`
3645
: styles.menu;
@@ -218,6 +227,8 @@ export default class ContextMenu {
218227
// Check if mouse is already over a menu item (menu appeared under cursor)
219228
this._focusItemUnderMouse();
220229
}
230+
231+
this.fire("show", undefined as void);
221232
}
222233

223234
/**
@@ -248,6 +259,8 @@ export default class ContextMenu {
248259
item.closeSubmenu();
249260
}
250261
});
262+
263+
this.fire("hide", undefined as void);
251264
}
252265

253266
private _focusItem(index: number): void {

0 commit comments

Comments
 (0)