Skip to content

Commit

Permalink
feat(layer): support context menu events (#372) (h/t @chwallen)
Browse files Browse the repository at this point in the history
  • Loading branch information
chwallen authored Mar 17, 2022
1 parent 0eb0759 commit 5a17c2d
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 3 deletions.
11 changes: 11 additions & 0 deletions src/components/Layer/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,17 @@ type Props = LayerSpecification & {
* https://www.mapbox.com/mapbox-gl-js/api/#Map#queryRenderedFeatures
*/
onLeave?: (event: InteractionEvent) => any;

/**
* Called when the layer is right-clicked.
* @callback
* @param {Object} event - The mouse event.
* @param {[Number, Number]} event.lngLat - The coordinates of the pointer
* @param {Array} event.features - The features under the pointer,
* using Mapbox's queryRenderedFeatures API:
* https://www.mapbox.com/mapbox-gl-js/api/#Map#queryRenderedFeatures
*/
onContextMenu?: (event: InteractionEvent) => any;

/**
* Radius to detect features around a clicked/hovered point
Expand Down
30 changes: 29 additions & 1 deletion src/components/Layer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ const eventListeners = [
['onClick', 'click'],
['onHover', 'mousemove'],
['onEnter', 'mouseenter'],
['onLeave', 'mouseleave']
['onLeave', 'mouseleave'],
['onContextMenu', 'contextmenu']
];

type InteractionEvent = { ...MapMouseEvent, features?: GeoJSONFeature[] };
Expand Down Expand Up @@ -70,6 +71,17 @@ type Props = {|
*/
onLeave?: (event: InteractionEvent) => any,

/**
* Called when the layer is right-clicked.
* @callback
* @param {Object} event - The mouse event.
* @param {[Number, Number]} event.lngLat - The coordinates of the pointer
* @param {Array} event.features - The features under the pointer,
* using Mapbox's queryRenderedFeatures API:
* https://www.mapbox.com/mapbox-gl-js/api/#Map#queryRenderedFeatures
*/
onContextMenu?: (event: InteractionEvent) => any,

/**
* Radius to detect features around a clicked/hovered point
*/
Expand All @@ -95,6 +107,8 @@ class Layer extends PureComponent<Props> {

_onLeave: (event: MapMouseEvent) => void;

_onContextMenu: (event: MapMouseEvent) => void;

static displayName = 'Layer';

static defaultProps = {
Expand Down Expand Up @@ -179,6 +193,7 @@ class Layer extends PureComponent<Props> {
onHover,
onEnter,
onLeave,
onContextMenu,
...layer
} = props;

Expand Down Expand Up @@ -277,6 +292,19 @@ class Layer extends PureComponent<Props> {
this.props.onLeave({ ...event, features });
};

_onContextMenu = (event: MapMouseEvent) => {
const position: [number, number] = [event.point.x, event.point.y];
const features = queryRenderedFeatures(
this._map,
this._id,
position,
this.props.radius
);

// $FlowFixMe
this.props.onContextMenu({ ...event, features });
}

render() {
return createElement(MapContext.Consumer, {}, (map) => {
if (map) {
Expand Down
5 changes: 3 additions & 2 deletions src/components/Layer/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,11 +210,12 @@ test('handlers', () => {
onHover={handler}
onEnter={handler}
onLeave={handler}
onContextMenu={handler}
/>
]
});

expect(handler).toHaveBeenCalledTimes(4);
expect(handler).toHaveBeenCalledTimes(5);

wrapper.setProps({
children: [
Expand All @@ -223,7 +224,7 @@ test('handlers', () => {
]
});

expect(handler).toHaveBeenCalledTimes(4);
expect(handler).toHaveBeenCalledTimes(5);

// wrapper.setProps({ children: [] });
});
Expand Down

0 comments on commit 5a17c2d

Please sign in to comment.