Skip to content

Support global state expression for layer visibility #6495

@hiddewie

Description

@hiddewie

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

visibility: 'visible' | 'none' | void;
and
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

getLayoutAffectingGlobalStateRefs(): Set<string> {
needs to recognise that visibility can contain an expression and impact the rendering of a layer.

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