mapbox-gl-contextmenu - v1.6.0
    Preparing search index...

    Class MapboxContextMenu

    A context menu for Mapbox GL JS and Maplibre GL JS.

    const menu = new MapboxContextMenu({ theme: "dark" });

    menu.addItem(new ContextMenuItem({ label: "Center here" })
    .on("click", (e) => map.flyTo({ center: e.lngLat })));

    menu.addTo(map);

    Hierarchy

    • default
      • MapboxContextMenu
    Index

    Constructors

    • Creates a new context menu for Mapbox GL JS or Maplibre GL JS.

      Parameters

      • Optionaloptions: MapboxContextMenuOptions

        Configuration options for the context menu.

        Configuration options for creating a Mapbox context menu. Extends ContextMenuOptions.

        • OptionalclassName?: string

          Custom CSS class name for the menu element.

        • Optionaltheme?: ContextMenuTheme

          The color theme: "light", "dark", or "auto" (follows system preference). Defaults to "auto".

        • Optionalwidth?: string | number

          The menu width as a CSS value (e.g., "200px") or number in pixels.

      Returns MapboxContextMenu

    Accessors

    • set className(value: string): void

      Sets the CSS class name for the menu element.

      Parameters

      • value: string

        The class name to apply. Will be combined with the base menu class.

      Returns void

    • get theme(): ContextMenuTheme

      Gets the theme of the context menu.

      Returns ContextMenuTheme

      The current theme setting: "light", "dark", or "auto".

    • set theme(value: ContextMenuTheme): void

      Sets the theme of the context menu.

      Parameters

      • value: ContextMenuTheme

        The theme to use: "light" for light mode, "dark" for dark mode, or "auto" to follow the system preference.

      Returns void

    • get width(): string | number | undefined

      Gets the width of the context menu.

      Returns string | number | undefined

      The width as a string (e.g., "200px") or number (in pixels), or undefined if not set.

    • set width(value: string | number | undefined): void

      Sets the width of the context menu.

      Parameters

      • value: string | number | undefined

        The width as a string (e.g., "200px") or number (in pixels), or undefined to use the default width.

      Returns void

    Methods

    • Adds a menu item to the end of the context menu.

      Parameters

      • item: MenuItem

        The menu item to add. Can be a ContextMenuItem, ContextMenuSeparator, or ContextMenuSub.

      Returns this

      The context menu instance for method chaining.

    • Adds the context menu to a Mapbox GL JS or Maplibre GL JS map.

      Parameters

      • map: Map$1

        The map instance.

      • Optionaltarget: LayerTarget

        Optional target to restrict the menu to. Can be:

        • A layer ID string (e.g., "building")
        • An array of layer IDs (e.g., ["building", "poi"])
        • A TargetDescriptor for Mapbox GL JS v3.9.0+ (e.g., { featuresetId: "buildings", importId: "basemap" })

      Returns this

    • Fires an event, calling all registered handlers for the event type.

      Type Parameters

      • K extends keyof ContextMenuEvents

      Parameters

      • type: K

        The event type to fire.

      • event: ContextMenuEvents[K]

        The event data to pass to the handlers.

      Returns this

      The instance for method chaining.

    • Inserts a menu item at the specified index.

      Parameters

      • index: number

        The index at which to insert the item. If the index is out of bounds, the item will be added at the end.

      • item: MenuItem

        The menu item to insert. Can be a ContextMenuItem, ContextMenuSeparator, or ContextMenuSub.

      Returns this

      The context menu instance for method chaining.

    • Checks whether there are any registered handlers for the specified event type.

      Type Parameters

      • K extends keyof ContextMenuEvents

      Parameters

      • type: K

        The event type to check.

      Returns boolean

      true if there are registered handlers for the event type, false otherwise.

    • Unregisters an event handler for the specified event type.

      Type Parameters

      • K extends keyof ContextMenuEvents

      Parameters

      • type: K

        The event type to stop listening for.

      • handler: EventHandler<ContextMenuEvents[K]>

        The function to remove from the event handlers.

      Returns this

      The instance for method chaining.

    • Registers an event handler for the specified event type.

      Type Parameters

      • K extends keyof ContextMenuEvents

      Parameters

      • type: K

        The event type to listen for.

      • handler: EventHandler<ContextMenuEvents[K]>

        The function to call when the event is fired.

      Returns this

      The instance for method chaining.

    • Removes a menu item from the context menu, doing any clean up necessary.

      Parameters

      • item: MenuItem

        The menu item to remove.

      Returns this

      The context menu instance for method chaining.