mapbox / mapbox/mapbox-gl-draw
[Feature]: Support for `beforeId` parameter when adding features to control layer positioning
Nobody has claimed this yet.
- Dominant language
- JavaScript
- Stars
- 1.1k
- Forks
- 612
- Avg merge
- 8d 9h
- Merged PRs (30d)
- 5
Description
### Problem
Currently, when adding features to Mapbox GL Draw using `draw.add()`, the draw layers are always added on top of all existing layers in the map. This causes visual flashing when layers need to be re positioned using `map.moveLayer()` after creation, as users briefly see the layers in the wrong position before they're moved.
### Use Case
In our application, we need footprint draw layers to render below roof surface layers but above parcel layers.
Currently, we have to:
1. Add the draw feature (appears on top)
2. Use `map.on('idle', ...)` to detect when layers are added
3. Call `map.moveLayer()` to reposition them
This causes a visible flash as the layers appear on top first, then get moved to their correct position.
### Proposed Solution
Add support for a `beforeId` option when adding features, similar to Mapbox GL JS's `map.addLayer()`:
```ts
draw.add({
type: "Feature",
geometry: footprintGeometry,
properties: { ... }
}, {
beforeId: 'roof-surface-layer-0' // Position draw layers before this layer
})
```
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by tracing the draw.add() entry point through feature and layer creation, then compare its insertion behavior with Mapbox GL JS map.addLayer() and the current map.moveLayer() workaround. Add coverage showing that an optional beforeId places draw layers correctly without a visible intermediate position; done means the proposed call works for the stated layer-ordering use case.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript
- Domain
- frontend
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 48/100