MapboxLayer is a component that allows you to add and manage layers in a Mapbox map. It provides a convenient way to add layers with proper event handling and lifecycle management.
The component is used to add layers to your Mapbox map. It requires a layer specification and can optionally specify where to position the layer relative to other layers.
<script setup lang="ts">
const layerSpec = {
id: 'my-layer',
type: 'circle',
source: 'my-source',
paint: {
'circle-radius': 6,
'circle-color': '#B42222'
}
};
</script>
<template>
<MapboxMap>
<MapboxSource id="my-source" :source="{ type: 'geojson', data: myData }" />
<MapboxLayer :layer="layerSpec" />
</MapboxMap>
</template>
type MapboxLayerProps = {
/**
* The layer specification.
* @see {@link LayerSpecification|LayerSpecification}
* @see {@link https://docs.mapbox.com/mapbox-gl-js/api/map/#map#addlayer|Mapbox GL Docs - addLayer}
* @see {@link https://docs.mapbox.com/mapbox-gl-js/style-spec/#layers|Mapbox GL Docs - layers}
*/
layer: LayerSpecification;
/**
* The ID of an existing layer to insert the new layer before.
* @see {@link https://docs.mapbox.com/mapbox-gl-js/api/map/#addlayer-parameters-beforeid|Mapbox GL Docs - addLayer beforeId}
*/
beforeLayerId?: string;
/**
* Event handler for the mousedown event.
* @see {@link https://docs.mapbox.com/mapbox-gl-js/api/map/#map.event:mousedown|Mapbox GL Docs - mousedown event}
*/
onMousedown?: (event: MapEventOf<"mousedown">) => void;
/**
* Event handler for the mouseup event.
* @see {@link https://docs.mapbox.com/mapbox-gl-js/api/map/#map.event:mouseup|Mapbox GL Docs - mouseup event}
*/
onMouseup?: (event: MapEventOf<"mouseup">) => void;
/**
* Event handler for the mouseover event.
* @see {@link https://docs.mapbox.com/mapbox-gl-js/api/map/#map.event:mouseover|Mapbox GL Docs - mouseover event}
*/
onMouseover?: (event: MapEventOf<"mouseover">) => void;
/**
* Event handler for the mousemove event.
* @see {@link https://docs.mapbox.com/mapbox-gl-js/api/map/#map.event:mousemove|Mapbox GL Docs - mousemove event}
*/
onMousemove?: (event: MapEventOf<"mousemove">) => void;
/**
* Event handler for the mouseenter event.
* @see {@link https://docs.mapbox.com/mapbox-gl-js/api/map/#map.event:mouseenter|Mapbox GL Docs - mouseenter event}
*/
onMouseenter?: (event: MapEventOf<"mouseenter">) => void;
/**
* Event handler for the mouseleave event.
* @see {@link https://docs.mapbox.com/mapbox-gl-js/api/map/#map.event:mouseleave|Mapbox GL Docs - mouseleave event}
*/
onMouseleave?: (event: MapEventOf<"mouseleave">) => void;
/**
* Event handler for the mouseout event.
* @see {@link https://docs.mapbox.com/mapbox-gl-js/api/map/#map.event:mouseout|Mapbox GL Docs - mouseout event}
*/
onMouseout?: (event: MapEventOf<"mouseout">) => void;
/**
* Event handler for the click event.
* @see {@link https://docs.mapbox.com/mapbox-gl-js/api/map/#map.event:click|Mapbox GL Docs - click event}
*/
onClick?: (event: MapEventOf<"click">) => void;
/**
* Event handler for the dblclick event.
* @see {@link https://docs.mapbox.com/mapbox-gl-js/api/map/#map.event:dblclick|Mapbox GL Docs - dblclick event}
*/
onDblclick?: (event: MapEventOf<"dblclick">) => void;
/**
* Event handler for the contextmenu event.
* @see {@link https://docs.mapbox.com/mapbox-gl-js/api/map/#map.event:contextmenu|Mapbox GL Docs - contextmenu event}
*/
onContextmenu?: (event: MapEventOf<"contextmenu">) => void;
/**
* Event handler for the touchstart event.
* @see {@link https://docs.mapbox.com/mapbox-gl-js/api/map/#map.event:touchstart|Mapbox GL Docs - touchstart event}
*/
onTouchstart?: (event: MapEventOf<"touchstart">) => void;
/**
* Event handler for the touchend event.
* @see {@link https://docs.mapbox.com/mapbox-gl-js/api/map/#map.event:touchend|Mapbox GL Docs - touchend event}
*/
onTouchend?: (event: MapEventOf<"touchend">) => void;
/**
* Event handler for the touchcancel event.
* @see {@link https://docs.mapbox.com/mapbox-gl-js/api/map/#map.event:touchcancel|Mapbox GL Docs - touchcancel event}
*/
onTouchcancel?: (event: MapEventOf<"touchcancel">) => void;
};
| Prop | Default | Type |
|---|---|---|
layer* | LayerSpecificationLayer specification object that defines the layer properties | |
beforeLayerId | stringThe ID of an existing layer to insert the new layer before |
| Event | Payload |
|---|---|
click | (event: MapEventOf<'click'>) => voidEvent handler for the |
contextmenu | (event: MapEventOf<'contextmenu'>) => voidEvent handler for the |
dblclick | (event: MapEventOf<'dblclick'>) => voidEvent handler for the |
mousedown | (event: MapEventOf<'mousedown'>) => voidEvent handler for the |
mouseenter | (event: MapEventOf<'mouseenter'>) => voidEvent handler for the |
mouseleave | (event: MapEventOf<'mouseleave'>) => voidEvent handler for the |
mousemove | (event: MapEventOf<'mousemove'>) => voidEvent handler for the |
mouseout | (event: MapEventOf<'mouseout'>) => voidEvent handler for the |
mouseover | (event: MapEventOf<'mouseover'>) => voidEvent handler for the |
mouseup | (event: MapEventOf<'mouseup'>) => voidEvent handler for the |
touchcancel | (event: MapEventOf<'touchcancel'>) => voidEvent handler for the |
touchend | (event: MapEventOf<'touchend'>) => voidEvent handler for the |
touchstart | (event: MapEventOf<'touchstart'>) => voidEvent handler for the |
| Slot | Payload |
|---|---|
default | Content to render inside the layer |
| Attribute | Value | Type |
|---|---|---|
id | {layer.id} | string |