Skip to content

armgit / TREK

Public Imported from GitHub
0

The most complete open-source travel planner: real-time collaboration, bookings, budgets, packing lists, journals, offline PWA, SSO and an AI-ready MCP server, with planet-wide place search and routing built in.

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)

Maurice6e9f89a25f26
diff --git a/client/src/components/Map/glSupport.test.ts b/client/src/components/Map/glSupport.test.ts
index 2dd916162..31238012b 100644
--- a/client/src/components/Map/glSupport.test.ts
+++ b/client/src/components/Map/glSupport.test.ts
@@ -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);
diff --git a/client/src/components/Map/glSupport.ts b/client/src/components/Map/glSupport.ts
index 8b1469fb1..93a42a11d 100644
--- a/client/src/components/Map/glSupport.ts
+++ b/client/src/components/Map/glSupport.ts
@@ -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);
+ });
+}