Skip to content

Allowing user-defined interaction handlers, and splitting them into a submodule #8886

@arindam1993

Description

@arindam1993

Motivation

We have a number of issues in our issue queue that are regarding customizing various things regarding interactivity.

The idea with this proposal is to make this easier to do for developers by allowing them to bind their own event handlers declaratively at map init time, moving what we do in bind_handlers.js up and out so developers can integrate their own implementations and not necessarily have to rely on passing options and or activating/deactivating handlers that we have.

We can take this one step further, and extract our handlers and controls into separate npm packages ( similar to what babel and turf do ) so users not using our implementations don't have to pay the cost of the increased bundle size.

Design Alternative

The current status quo of adding additional options to Map, and or exposing configuration though map.{handlerName}.

This has the potential of adding a lot of cruft and complexity over-time as we try to accomodate additional requests.

Design

  • A newly constructed Map is interactive:false by default, and this option gets removed from the Map constructor.
  • Add a map.bindInteractionHandlers(handlers: EventHandlers) method, this is how you would add interactivity to the map.
type EventHandlers = {
    mousemove: function() {..},
    mousedown: function() {..},
    mouseup: function() {..},
    ....,
}
  • Separate out our interaction handlers to a separate sub-module, in order to enable the current state of interactive: true would look something like this.
import mapboxgl-interaction-handlers from '@mapbox/mapbox-gl-event handlers';
import mapboxgl from 'mapbox-gl-js';

// create an non-interactive map
const map = new Map(..);
// Add interactivity after the fact
map.bindInteractionHandlers(mapboxgl-interaction-handlers);

Pros:

  • Lower bundle size
  • Allows develops to fork our just interaction handlers and use them without having to fork entirety of gl-js if they need a different interaction paradigm.
  • Developers already using existing gesture processing libraries like hammer.js can have better integration with the rest of their app.

Cons:

  • API Breaking change :(
  • Requires some refactoring within gl-js, we currently query the interaction state globally on map for things like zooming hackily throughout the codebase.
  • Additional overhead on our side for maintaining an additional module and publishing and releasing it.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions