Search code examples
eventsleafletcontextmenuhandler

How to add a event handler to catch leaflet contextmenu events?


I create a context menu like this:

var map = L.map('map', {
    minZoom: 2,
    maxZoom: 15,
    gestureHandling: ControlGestual,
    contextmenu: true,
        contextmenuWidth: 300,
    contextmenuItems: MenuContextual
});

but I also need some code to be executed when the context menu is closed(hidden). I see in the documentation that there is a contextmenu.hide event, but I don't know how to create a handler for this event.

I would appreciate your help. Many thanks in advance.


Solution

  • The event is fired on the map, so add:

    map.on('contextmenu.hide', (e)=>{
        console.log(e)
    });