mapbox / mapbox/mapbox-gl-js

CSS warning shown when map container is not added to the DOM

Open
#9,494 0 comments 5 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug :lady_beetle:
Dominant language
TypeScript
Stars
12.4k
Forks
2.4k
PR merge metrics
No merged PRs in 30d

Description

When creating a new map instance using a container that is not yet added to the DOM we get this warning:

"This page appears to be missing CSS declarations for Mapbox GL JS, which may cause the map to display incorrectly. Please ensure your page includes mapbox-gl.css, as described in https://www.mapbox.com/mapbox-gl-js/api/."

If the container div is added to the DOM before the map is created the warning will not show. Beside this warning, it works perfectly fine to create a map for a container that is not in the DOM. We use this approach in our React setup, where the container is rendered at a later stage.

The issue seems to be with the current _detectMissingCSS() in src/ui/map.js

mapbox-gl-js version: 1.9.0

browser: Google Chrome

Steps to Trigger Behavior
const div = document.createElement('div');

// document.body.appendChild(div);

const map = new mapboxgl.Map({
  container: div
});

Check the console to see the warning.

If the container div is added to the DOM before the map is created the warning will not show.

Link to Demonstration

https://jsfiddle.net/mastermaps/nz3Ld5ar/8/

Expected Behavior

Don't show the warning when the CSS is actually present.

Actual Behavior

The warning shows even though the CSS is loaded.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with _detectMissingCSS() in src/ui/map.js and reproduce the warning using a container div that has not yet been added to the DOM. Compare that behavior with a DOM-attached container and verify that the warning is suppressed when the CSS is present.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
frontend
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
58/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.