Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Canvas width/height don't update when container's width/height are changed #2338

Open
dlbnco opened this issue Jan 9, 2024 · 5 comments
Open

Comments

@dlbnco
Copy link

dlbnco commented Jan 9, 2024

Description

After upgrading to v7, I noticed this behavior in my application which has maps displayed in different dimensions that often change.

image

Map with initial size

When a map container changes it's width/height, the underlying canvas that actually renders the map does not update to follow the change, resulting in a incorrectly sized render.

image

Map with new size and outdated canvas

If the browser window is resized, then the canvas will update as expected.

image

Map with corrected canvas after browser window resize

Expected Behavior

The underlying canvas should follow container dimension changes immediately.

Steps to Reproduce

This is reproducible in any of the hosted lib examples, e.g.: https://visgl.github.io/react-map-gl/examples/controls

  1. In your browser develop tools, search for the element that has the following class: container padding-top--md padding-bottom--lg
  2. Change the max-width to unset — this will increase the element's width
  3. Notice that the map won't fill the new width immediately;
  4. Resize your browser window and observe the map now resizing as expected.

Environment

  • Framework version: 7.1.7
  • Map library: Mapbox
  • Browser: Safari/Firefox
  • OS: macOS Sonoma

Logs

No response

@dlbnco dlbnco added the bug label Jan 9, 2024
@dlbnco
Copy link
Author

dlbnco commented Jan 9, 2024

I was able to workaround this by manually calling mapboxgl.Map.resize() inside a useEffect that also gets triggered when those dimensions change. Maybe this is a clue for a solution.

@Pessimistress
Copy link
Collaborator

This is not a bug. v7 follows mapbox-gl's own behavior, which does not monitor the resize of your container. You are supposed to call resize yourself.

@Pessimistress Pessimistress removed the bug label Jan 9, 2024
@dlbnco
Copy link
Author

dlbnco commented Jan 10, 2024

So this kinda falls into this item? (from the Upgrading docs):

All map controls' props are now strictly aligned with their mapbox-gl counterparts. In heading this direction, we are able to remove a significant amount of custom code and have the components behave more predictably for developers switching from the native library. If your application is relying on an old feature that is no longer supported, please open a topic on Discussion so we can review on a case-by-case basis.

Adding this feature can be considered but I'm also fine with the workaround above. Mentioning it in the documentation could also be helpful—spent quite a lot of time debugging and making sure it was not an issue with my implementation.

You are supposed to call resize yourself.

What explains it fixing itself when the browser window is resized? Is mapbox-gl handling it?

@dlbnco dlbnco changed the title [Bug] Canvas width/height don't update when container's width/height are changed Canvas width/height don't update when container's width/height are changed Jan 10, 2024
@Pessimistress
Copy link
Collaborator

What explains it fixing itself when the browser window is resized? Is mapbox-gl handling it?

Yes. mapbox-gl's trackResize option (default true) adds a listener to window.resize.

@neelduttahere
Copy link

neelduttahere commented May 8, 2024

I was able to workaround this by manually calling mapboxgl.Map.resize() inside a useEffect that also gets triggered when those dimensions change. Maybe this is a clue for a solution.

Hey @dlbnco can you elaborate? mapbox-gl.Map does not seem to be exporting resize(), how were you able to work around this?

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

No branches or pull requests

3 participants