Skip to content

Added support for setStyleLayerProperty - #4051

Merged
mfazekas merged 4 commits into
rnmapbox:mainfrom
msaqlain:feat/set-style-layer-property
Nov 17, 2026
Merged

mfazekas merged 4 commits into
rnmapbox:mainfrom
msaqlain:feat/set-style-layer-property

Conversation

@msaqlain

@msaqlain msaqlain commented Oct 17, 2025 •

Copy link
Copy Markdown
Contributor

Description

Added MapView#setStyleLayerProperty(layerId, property, value), a thin wrapper over the Mapbox SDK's setStyleLayerProperty on iOS and Android. It's handy for layers that come from styleURL (for example showing or hiding groups of them) without declaring them as layer components. value can be any JSON value, including expressions, and the returned promise rejects if the SDK reports an error (unknown layer or property, invalid value).

Checklist

  • I've read CONTRIBUTING.md
  • I updated the doc/other generated code with running yarn generate in the root folder
  • I have tested the new feature on /example app.
    • In V11 mode/ios
    • In New Architecture mode/ios
    • In V11 mode/android
    • In New Architecture mode/android
  • I added/updated a sample - if a new feature was implemented (/example)

Screenshot OR Video

Component to reproduce the issue you're fixing

Test screen used on the iOS simulator and Android emulator (valid visibility/color/expression changes plus the invalid layer and invalid property error paths):

import { useRef, useState } from 'react';
import { Button, Text } from 'react-native';
import { Camera, MapView } from '@rnmapbox/maps';

const StyleLayerPropertyTest = () => {
  const mapRef = useRef<MapView>(null);
  const [result, setResult] = useState('-');

  const run = async (label: string, layer: string, prop: string, value: any) => {
    try {
      await mapRef.current?.setStyleLayerProperty(layer, prop, value);
      setResult(`${label}: OK`);
    } catch (e: any) {
      setResult(`${label}: REJECTED ${e?.message}`);
    }
  };

  return (
    <>
      <Button title="hide" onPress={() => run('hide', 'building', 'visibility', 'none')} />
      <Button title="show" onPress={() => run('show', 'building', 'visibility', 'visible')} />
      <Button title="red" onPress={() => run('red', 'building', 'fill-color', '#ff0000')} />
      <Button title="opacity expr" onPress={() => run('expr', 'building', 'fill-opacity', ['interpolate', ['linear'], ['zoom'], 14, 0.2, 18, 1])} />
      <Button title="bad layer" onPress={() => run('badlayer', 'nope-layer', 'visibility', 'none')} />
      <Button title="bad prop" onPress={() => run('badprop', 'building', 'nope-prop', 1)} />
      <Text>{result}</Text>
      <MapView ref={mapRef} style={{ flex: 1 }}>
        <Camera centerCoordinate={[-74.005974, 40.712776]} zoomLevel={15.5} />
      </MapView>
    </>
  );
};

export default StyleLayerPropertyTest;

Results on the iOS simulator (iPhone 17) and Android emulator: hide/show, fill-color and the fill-opacity expression all apply. The invalid layer rejects with Layer 'nope-layer' is not in style and the invalid property with fill layer doesn't support this property: nope-prop on both platforms.

@msaqlain
msaqlain temporarily deployed to CI with Mapbox Tokens October 17, 2025 10:34 — with GitHub Actions Inactive
@msaqlain
msaqlain temporarily deployed to CI with Mapbox Tokens October 17, 2025 10:34 — with GitHub Actions Inactive
@msaqlain
msaqlain temporarily deployed to CI with Mapbox Tokens October 17, 2025 10:34 — with GitHub Actions Inactive
@msaqlain
msaqlain temporarily deployed to CI with Mapbox Tokens October 17, 2025 10:34 — with GitHub Actions Inactive
@msaqlain
msaqlain temporarily deployed to CI with Mapbox Tokens October 17, 2025 10:34 — with GitHub Actions Inactive
@msaqlain
msaqlain temporarily deployed to CI with Mapbox Tokens October 17, 2025 10:34 — with GitHub Actions Inactive
@msaqlain
msaqlain temporarily deployed to CI with Mapbox Tokens October 17, 2025 10:34 — with GitHub Actions Inactive
@msaqlain

Copy link
Copy Markdown
Contributor Author

@mfazekas Created a new PR with the latest version. closing an old one #3870.

Added an example too. Thanks

@mfazekas

Copy link
Copy Markdown
Contributor

@msaqlain thanks much, can you also add some motivation?!

How is this different from our existing solution:

         <FillLayer id="building" existing style={{ visibility }} />

https://rnmapbox.github.io/docs/examples/Map/ShowAndHideLayer

@msaqlain

Copy link
Copy Markdown
Contributor Author

Yes sure @mfazekas

When we are only loading map using styleURL that contains all the Layers & SourceLayers we dont have references to all the FillLayer, LineLayer or other shapes.

To toggle SourceLayers (composite, satellite) we have this method setSourceVisibility

mapRef.current?.setSourceVisibility(
      shouldBeVisible,
      'composite',
      mode
);

Similarly if you want to toggle a specific layer we dont have any method available. To enable this my in my PR i am using setStyleLayerProperty

mapRef?.current?.setStyleLayerProperty(
      layerId,
      'visibility',
      'visible'
);

@mfazekas

Copy link
Copy Markdown
Contributor

Similarly if you want to toggle a specific layer we dont have any method available. To enable this my in my PR i am using setStyleLayerProperty

mapRef?.current?.setStyleLayerProperty(
      layerId,
      'visibility',
      'visible'
);

I think we do have this:

const [visibility, setVisbility] = useState('visible')
...
<FillLayer id="building" existing style={{ visibility }} />

This will change a specific layer of(building) visibility. Note the existing flag, that will not create new layer but will lookup up the layer by id.

@msaqlain

Copy link
Copy Markdown
Contributor Author

@mfazekas If we use this approach as you mentioned, then in my scenario, I have to render 300 layers using FillLayer, which I need to enable and disable based on my internal grouping.

image

So to achieve this without explicitly mentioning FillLayer i can load map using styleURL only and then using map ref i can enable disable layers.

@msaqlain

msaqlain commented Dec 9, 2025

Copy link
Copy Markdown
Contributor Author

@mfazekas Any update on this?

@mfazekas

mfazekas commented Dec 9, 2025

Copy link
Copy Markdown
Contributor

@msaqlain we'd need CI to pass

@msaqlain
msaqlain had a problem deploying to CI with Mapbox Tokens January 3, 2026 23:45 — with GitHub Actions Failure
@msaqlain
msaqlain had a problem deploying to CI with Mapbox Tokens January 3, 2026 23:45 — with GitHub Actions Failure
@msaqlain
msaqlain had a problem deploying to CI with Mapbox Tokens January 3, 2026 23:45 — with GitHub Actions Failure
@benoit-ctrl

Copy link
Copy Markdown

Any update ?

Pass the value through to the SDK as any JSON value (so numbers, colors
and expressions work, not only strings), reject the promise when the SDK
reports an error, move the iOS method onto RNMBXMapView after the old
architecture manager was removed, and rewrite the example as a TSX
function component.
@mfazekas

Copy link
Copy Markdown
Contributor

Thanks @msaqlain! I brought this up to date with main and made a few changes so it matches the SDK call directly: value now accepts any style value (numbers, colors, expressions), and the promise rejects with the SDK's error for an unknown layer or property. I also turned the example into a small TSX component. I checked it on iOS and Android. Let me know if anything here doesn't fit your use case.

@mfazekas
mfazekas merged commit f0c855c into rnmapbox:main Nov 17, 2026
10 checks passed

This branch had an error being deployed

1 failed (outdated) and 1 inactive deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants