-
-
Notifications
You must be signed in to change notification settings - Fork 932
Description
User Story
I would like to create a layer that is "togglable" to be visible/invisible with a global-state expression.
For example:
{
id: 'historical-stations',
layout: {
visible: ['global-state', 'feature-enabled'],
}
}The visibility attribute of the layout properties of a layer is handled specifically, and does not seem to support any expressions.
See https://maplibre.org/maplibre-style-spec/layers/#visibility and
maplibre-gl-js/src/style/style_layer.ts
Line 90 in 5e0f8f7
| visibility: 'visible' | 'none' | void; |
maplibre-gl-js/src/style/style_layer.ts
Lines 230 to 233 in 5e0f8f7
| if (name === 'visibility') { | |
| this.visibility = value; | |
| return; | |
| } |
It does not make sense for the visibility to support expressions related to the properties of a feature, but it does makes sense to support expressions related to global-state.
Rationale
Currently, making layers dynamically visible requires dynamically adding or deleting layers, or alternatively attaching metadata to every layer and dynamically reading the metadata to toggle the layer visibility.
The MapLibre style specification is made to be statically defined, instead of dynamic. After the addition of the global map state through the proposal in maplibre/maplibre-style-spec#886 (https://maplibre.org/maplibre-style-spec/expressions/#global-state), we can now define map features with defaults. Such features can be used to make the map dynamic using a static style, in most properties except for visibility.
Impact
Make it easier for map developers to create MapLibre-powered maps that behave more dynamically based on the user preferences. Such styles will also be more reusable because the map style itself documents the global state parameters, instead of depending on client-side scripting to make manipulate the map layers.
The code must be updated to allow expressions for the visibility layer property.
The code must be updated to evaluate the expression (if any) to determine the resolved layer visibility.
At the least, the implementation in
maplibre-gl-js/src/style/style_layer.ts
Line 179 in 5e0f8f7
| getLayoutAffectingGlobalStateRefs(): Set<string> { |
visibility can contain an expression and impact the rendering of a layer.