fix(map): a GL map follows its box, not only the window, so closing the plan pane on a tablet leaves no black band (#3023)

Mapbox GL measures its container on window resizes only. In the app on a
tablet the trip's plan pane closes beside the map without one: the box grew
to the full width while the canvas kept its old size, and the rest stayed
black. Every GL map built through buildGlMap now watches its container and
resizes once per frame while it changes; the observer goes with the map.

Closes #3023
This commit is contained in:
Maurice
2026-11-09 12:29:37 +01:00
parent a3c7c3d76a
commit 6e9f89a25f
2 changed files with 125 additions and 1 deletions
@@ -105,6 +105,88 @@ describe('buildGlMap', () => {
});
});
describe('followContainerSize', () => {
type Observed = { callback: ResizeObserverCallback; target: Element | null; disconnected: boolean };
const observers: Observed[] = [];
function fakeMap() {
let onRemove: (() => void) | undefined;
const container = document.createElement('div');
return {
map: {
getContainer: () => container,
resize: vi.fn(),
once: (_type: 'remove', listener: () => void) => {
onRemove = listener;
},
},
container,
remove: () => onRemove?.(),
};
}
beforeEach(() => {
observers.length = 0;
vi.useFakeTimers();
vi.stubGlobal(
'ResizeObserver',
class {
private readonly entry: Observed;
constructor(callback: ResizeObserverCallback) {
this.entry = { callback, target: null, disconnected: false };
observers.push(this.entry);
}
observe(target: Element) {
this.entry.target = target;
}
disconnect() {
this.entry.disconnected = true;
}
}
);
});
afterEach(() => {
vi.useRealTimers();
vi.unstubAllGlobals();
});
const fire = () => observers[0].callback([], {} as ResizeObserver);
it('FE-MAP-GLSUP-020: resizes the canvas when its box changes size without a window resize (#3023)', () => {
const { map, container } = fakeMap();
expect(buildGlMap(() => map)).toBe(map);
expect(observers[0].target).toBe(container);
fire();
fire();
fire();
expect(map.resize).not.toHaveBeenCalled();
vi.advanceTimersToNextFrame();
expect(map.resize).toHaveBeenCalledTimes(1);
fire();
vi.advanceTimersToNextFrame();
expect(map.resize).toHaveBeenCalledTimes(2);
});
it('FE-MAP-GLSUP-021: stops watching once the map is removed, a pending resize included', () => {
const { map, remove } = fakeMap();
buildGlMap(() => map);
fire();
remove();
vi.advanceTimersToNextFrame();
expect(observers[0].disconnected).toBe(true);
expect(map.resize).not.toHaveBeenCalled();
});
it('FE-MAP-GLSUP-022: leaves a map without a container alone', () => {
expect(buildGlMap(() => ({ remove: vi.fn() }))).toEqual({ remove: expect.any(Function) });
expect(observers).toHaveLength(0);
});
});
describe('useGlRenderer', () => {
it('FE-MAP-GLSUP-010: asks the canvas for webgl2 only', () => {
const getContext = browserWithWebGL2(false);
+43 -1
View File
@@ -75,10 +75,52 @@ export function initialMapStyle(provider: GlMapProvider, style: string): string
export function buildGlMap<T>(construct: () => T): T | null {
try {
return construct();
const map = construct();
if (isResizableGlMap(map)) followContainerSize(map);
return map;
} catch (err) {
if (!isGpuInitFailure(err)) throw err;
reportWebGLFailure();
return null;
}
}
interface ResizableGlMap {
getContainer(): HTMLElement;
resize(): unknown;
once(type: 'remove', listener: () => void): unknown;
}
function isResizableGlMap(map: unknown): map is ResizableGlMap {
const candidate = map as Partial<ResizableGlMap> | null;
return (
typeof candidate?.getContainer === 'function' &&
typeof candidate.resize === 'function' &&
typeof candidate.once === 'function'
);
}
/**
* Keeps a GL map's canvas the size of its box. Mapbox GL measures its container
* on window resizes only, so a box that grew without one (the trip's plan pane
* closing beside the map in the app on a tablet, a foldable unfolding) kept the
* old canvas, with the rest of the box left empty and black (#3023). MapLibre
* watches its container itself; one more measure per frame costs it nothing.
* Several changes in one frame (the pane's slide) make a single resize.
*/
function followContainerSize(map: ResizableGlMap): void {
if (typeof ResizeObserver === 'undefined') return;
let frame = 0;
const observer = new ResizeObserver(() => {
if (frame) return;
frame = requestAnimationFrame(() => {
frame = 0;
map.resize();
});
});
observer.observe(map.getContainer());
map.once('remove', () => {
observer.disconnect();
if (frame) cancelAnimationFrame(frame);
});
}