Vizzuality / layer-manager

A library to get a layer depending on provider and layer spec
https://layer-manager-docs.vercel.app
MIT License
18 stars 12 forks source link

Fix a bug where the initial opacity of a layer would be discarded #92

Closed clementprdhomme closed 4 years ago

clementprdhomme commented 4 years ago

This PR fixes an issue where, if a layer has an opacity set on the initial render, it would be discarded.

The problem was located in the Mapbox plugin, specifically in the setRender method. On instantiation, all the layers are added via the add method of LayerManager. This method calls requestLayer, which, among other things, calls the setOpacity and setRender methods of the plugin, in this order.

In setOpacity, the *-opacity paint properties are set accordingly to the top-level opacity property passed to the layer-manager. This is done using Mapbox' setPaintProperty method. Nevertheless, setRender would set the properties again (and others unrelated too) using setPaintProperty, disregarding the top-level opacity property. It would effectively overwrite the properties setOpacity updated.

To fix the issue, this PR adds a new method in the plugin, computePaintPropertyValue, which ensures that if a *-opacity property is to be set, its value will depend on the top-level opacity property.

Testing instructions

  1. Open sandbox/src/layers.js
  2. Add an opacity property with the value 0 to the “Protected areas” layer (after line 110):
    {
    id: 'protected-areas',
    name: 'Protected areas',
    +   opacity: 0,
    config: {}
    }
  3. Run the sandbox: yarn start:sandbox

Once the sandbox has opened in your browser, make sure the protected areas are not showing on the map. The legend will display the correct information patch or not.

Pivotal Tracker

Not tracked.