fix(atlas): the note tooltip scrolls instead of zooming the map (#2153)

Leaflet's wheel handler on the map container cancels the native scroll, so the wheel zoomed the world under the note; the tooltip stops scroll and click propagation now. Hovering back to the marker no longer closes the tooltip mid-way, the flip threshold matches the real tooltip height, and a touch swipe scrolls the note instead of panning the map.
This commit is contained in:
Maurice
2026-09-02 19:00:35 +02:00
committed by Maurice
parent 5feced54d2
commit e1af2049f9
5 changed files with 171 additions and 17 deletions
+3
View File
@@ -148,6 +148,9 @@ body { height: 100%; overflow: auto; overscroll-behavior: none; -webkit-overflow
.atlas-tooltip-scroll-inner {
max-height: min(200px, 40vh);
overflow-y: hidden;
/* Leaflet puts touch-action: none on the map container; without widening it
back here a touch swipe over the note can never scroll it (#2153). */
touch-action: pan-y;
}
.atlas-tooltip-scroll-inner::-webkit-scrollbar { width: 6px; }
.atlas-tooltip-scroll-inner::-webkit-scrollbar-thumb { background: rgba(255,255,255,0.25); border-radius: 3px; }
+30 -5
View File
@@ -1,6 +1,7 @@
import { describe, it, expect } from 'vitest';
import {
A2_TO_A3,
bucketTooltipHeight,
bucketTooltipNeedsScroll,
bucketTooltipPlacement,
bucketTooltipWidth,
@@ -310,36 +311,60 @@ describe('bucketTooltipWidth (#2153)', () => {
});
});
describe('bucketTooltipHeight (#2153)', () => {
it('is the 200px content cap plus the tooltip chrome, offset and arrow margin', () => {
expect(bucketTooltipHeight(1080)).toBe(242);
});
it('follows the 40vh cap down on a short viewport', () => {
expect(bucketTooltipHeight(400)).toBe(202);
});
});
describe('bucketTooltipPlacement (#2153)', () => {
const viewport = { width: 1000, height: 1080 };
it('opens above a marker with room on all sides, centred with no horizontal nudge', () => {
const placement = bucketTooltipPlacement({ x: 500, y: 400 }, 1000, 480);
const placement = bucketTooltipPlacement({ x: 500, y: 400 }, viewport, 480);
expect(placement.direction).toBe('top');
expect(placement.offset).toEqual([0, -14]);
});
it('flips to open below the marker when there is no room above it', () => {
const placement = bucketTooltipPlacement({ x: 500, y: 100 }, 1000, 480);
const placement = bucketTooltipPlacement({ x: 500, y: 100 }, viewport, 480);
expect(placement.direction).toBe('bottom');
expect(placement.offset[1]).toBe(14);
});
it('still flips for a marker that clears 220px but not the tooltip itself', () => {
// 225px of room used to read as "fits above", which clipped the top 17px away.
const placement = bucketTooltipPlacement({ x: 500, y: 225 }, viewport, 480);
expect(placement.direction).toBe('bottom');
});
it('keeps the tooltip above when neither side fits but there is more room above', () => {
// Landscape phone, 300px tall: the tooltip wants 162px and gets 152 above, 132 below.
const placement = bucketTooltipPlacement({ x: 500, y: 160 }, { width: 1000, height: 300 }, 480);
expect(placement.direction).toBe('top');
});
it('nudges right when centring the tooltip would clip the left edge', () => {
// Marker at x=50 with a 480px-wide tooltip centred on it would start at
// x=-190; the offset needs to push the tooltip's left edge to the margin (8px).
const placement = bucketTooltipPlacement({ x: 50, y: 400 }, 1000, 480);
const placement = bucketTooltipPlacement({ x: 50, y: 400 }, viewport, 480);
const defaultLeft = 50 - 480 / 2;
expect(placement.offset[0]).toBe(8 - defaultLeft);
});
it('nudges left when centring the tooltip would clip the right edge', () => {
const placement = bucketTooltipPlacement({ x: 950, y: 400 }, 1000, 480);
const placement = bucketTooltipPlacement({ x: 950, y: 400 }, viewport, 480);
const defaultLeft = 950 - 480 / 2;
const clampedLeft = 1000 - 8 - 480;
expect(placement.offset[0]).toBe(clampedLeft - defaultLeft);
});
it('does not nudge horizontally when the tooltip already fits', () => {
const placement = bucketTooltipPlacement({ x: 500, y: 400 }, 1000, 480);
const placement = bucketTooltipPlacement({ x: 500, y: 400 }, viewport, 480);
expect(placement.offset[0]).toBe(0);
});
});
+14 -6
View File
@@ -218,6 +218,13 @@ export function bucketTooltipWidth(viewportWidth: number): number {
return Math.min(480, viewportWidth - 32)
}
/** Vertical space (CSS px) the bucket-list tooltip claims beside its marker: the
* .atlas-tooltip-scroll-inner cap of min(200px, 40vh), plus .atlas-tooltip's padding and
* border (2x10 + 2x1), plus the 14px offset and Leaflet's 6px .leaflet-tooltip-top margin. */
export function bucketTooltipHeight(viewportHeight: number): number {
return Math.min(200, viewportHeight * 0.4) + 22 + 20
}
export interface TooltipPlacement {
direction: 'top' | 'bottom'
offset: [number, number]
@@ -225,18 +232,19 @@ export interface TooltipPlacement {
/** Keeps the bucket-list marker tooltip on-screen (#2153): Leaflet has no viewport
* awareness, so flip below the marker when there's no room above, and nudge the
* horizontal offset to keep it within [margin, viewportWidth - margin]. */
* horizontal offset to keep it within [margin, viewport.width - margin]. */
export function bucketTooltipPlacement(
markerScreen: { x: number; y: number },
viewportWidth: number,
viewport: { width: number; height: number },
tooltipWidth: number,
opts: { margin?: number; topThreshold?: number } = {},
opts: { margin?: number } = {},
): TooltipPlacement {
const margin = opts.margin ?? 8
const topThreshold = opts.topThreshold ?? 220
const flip = markerScreen.y < topThreshold
const roomAbove = markerScreen.y - margin
const roomBelow = viewport.height - markerScreen.y - margin
const flip = roomAbove < bucketTooltipHeight(viewport.height) && roomBelow > roomAbove
const defaultLeft = markerScreen.x - tooltipWidth / 2
const clampedLeft = Math.min(Math.max(defaultLeft, margin), viewportWidth - margin - tooltipWidth)
const clampedLeft = Math.min(Math.max(defaultLeft, margin), viewport.width - margin - tooltipWidth)
const dx = clampedLeft - defaultLeft
return { direction: flip ? 'bottom' : 'top', offset: [dx, flip ? 14 : -14] }
}
+111 -5
View File
@@ -30,6 +30,18 @@ interface MockLayer {
getBounds: ReturnType<typeof vi.fn>;
}
interface MockMarker {
/** One list per event: the bucket marker binds two mouseover handlers. */
handlers: Record<string, ((e?: unknown) => void)[]>;
tooltipEl: HTMLElement;
on: ReturnType<typeof vi.fn>;
off: ReturnType<typeof vi.fn>;
getLatLng: ReturnType<typeof vi.fn>;
bindTooltip: ReturnType<typeof vi.fn>;
getTooltip: () => { options: Record<string, unknown> } | undefined;
closeTooltip: ReturnType<typeof vi.fn>;
}
const lf = vi.hoisted(() => ({
mapHandlers: {} as Record<string, ((e?: unknown) => void)[]>,
geoJson: [] as unknown[],
@@ -44,6 +56,12 @@ const lf = vi.hoisted(() => ({
mapsCreated: 0,
mapsRemoved: 0,
markers: 0,
bucketMarkers: [] as MockMarker[],
// Where the map places a marker inside its container; the container itself sits at
// 0,0 in jsdom, so this doubles as the marker's screen position.
markerPoint: { x: 500, y: 400 },
scrollPropagationOff: [] as HTMLElement[],
clickPropagationOff: [] as HTMLElement[],
map: null as null | { setView: ReturnType<typeof vi.fn> },
reset() {
this.mapHandlers = {};
@@ -57,6 +75,10 @@ const lf = vi.hoisted(() => ({
this.mapsCreated = 0;
this.mapsRemoved = 0;
this.markers = 0;
this.bucketMarkers = [];
this.markerPoint = { x: 500, y: 400 };
this.scrollPropagationOff = [];
this.clickPropagationOff = [];
},
}));
@@ -97,6 +119,8 @@ vi.mock('leaflet', () => {
return layer;
};
const mapContainer = document.createElement('div');
const map = {
setView: vi.fn(() => map),
on: vi.fn((event: string, cb: () => void) => { (lf.mapHandlers[event] ||= []).push(cb); return map; }),
@@ -117,6 +141,8 @@ vi.mock('leaflet', () => {
hasLayer: vi.fn(() => lf.hasLayer),
createPane: vi.fn((name: string) => { lf.panes[name] = { style: {} }; }),
getPane: vi.fn((name: string) => lf.panes[name]),
getContainer: vi.fn(() => mapContainer),
latLngToContainerPoint: vi.fn(() => lf.markerPoint),
};
// Published so a test can assert the map actually moved (place search flies
// there before the region lookup returns).
@@ -131,21 +157,31 @@ vi.mock('leaflet', () => {
divIcon: vi.fn(() => ({})),
marker: vi.fn(() => {
lf.markers += 1;
let tooltip: { options: Record<string, unknown>; getElement: () => null } | undefined;
const m = {
on: vi.fn(() => m),
const handlers: Record<string, ((e?: unknown) => void)[]> = {};
const tooltipEl = document.createElement('div');
let tooltip: { options: Record<string, unknown>; getElement: () => HTMLElement } | undefined;
const m: MockMarker = {
handlers,
tooltipEl,
on: vi.fn((event: string, cb: (e?: unknown) => void) => { (handlers[event] ||= []).push(cb); return m; }),
off: vi.fn(() => m),
getLatLng: vi.fn(() => ({ lat: 0, lng: 0 })),
bindTooltip: vi.fn(() => {
tooltip = { options: { direction: 'top', offset: [0, -14] }, getElement: () => null };
bindTooltip: vi.fn((html: string, options: Record<string, unknown>) => {
tooltipEl.innerHTML = html;
tooltip = { options: { ...options }, getElement: () => tooltipEl };
return m;
}),
getTooltip: vi.fn(() => tooltip),
closeTooltip: vi.fn(),
};
lf.bucketMarkers.push(m);
return m;
}),
layerGroup: vi.fn(() => ({ addTo: vi.fn(() => ({})) })),
DomEvent: {
disableScrollPropagation: vi.fn((el: HTMLElement) => { lf.scrollPropagationOff.push(el); }),
disableClickPropagation: vi.fn((el: HTMLElement) => { lf.clickPropagationOff.push(el); }),
},
geoJSON: vi.fn((data: { features?: Record<string, unknown>[] }, options: Record<string, unknown>) => {
const record = { data, options, entries: [] as unknown[], styles: [] as unknown[], attached: false };
const style = options?.style as ((f: unknown) => unknown) | undefined;
@@ -1257,4 +1293,74 @@ describe('useAtlas', () => {
expect(atlas.confirmAction?.type).not.toBe('choose-region');
});
});
// ── Bucket-list note tooltip (#2153) ───────────────────────────────────────
describe('the note tooltip on a bucket-list marker', () => {
const kyoto = { id: 1, name: 'Kyoto', lat: 35, lng: 135, country_code: 'JP', notes: 'Kinkaku-ji at opening time', target_date: null };
const fire = (marker: MockMarker, event: string) => (marker.handlers[event] ?? []).forEach((cb) => cb());
async function mountWithMarker(): Promise<MockMarker> {
await mountAtlas({ bucket: [kyoto] });
await waitFor(() => expect(lf.bucketMarkers.length).toBeGreaterThan(0));
return lf.bucketMarkers[lf.bucketMarkers.length - 1];
}
it('FE-HOOK-ATLAS-057: binds the note as an interactive, scrollable tooltip', async () => {
const marker = await mountWithMarker();
expect(marker.bindTooltip).toHaveBeenCalledWith(
expect.stringContaining('atlas-tooltip-scroll-inner'),
expect.objectContaining({ className: 'atlas-tooltip atlas-tooltip-scrollable', interactive: true }),
);
});
it('FE-HOOK-ATLAS-058: flips the tooltip below a marker with no room for it above', async () => {
const marker = await mountWithMarker();
lf.markerPoint = { x: 500, y: 400 };
act(() => fire(marker, 'mouseover'));
expect(marker.getTooltip()!.options).toMatchObject({ direction: 'top', offset: [0, -14] });
// 230px of headroom clears the old 220px threshold but not the 242px the tooltip
// really occupies, which is what kept clipping its top edge.
lf.markerPoint = { x: 500, y: 230 };
act(() => fire(marker, 'mouseover'));
expect(marker.getTooltip()!.options).toMatchObject({ direction: 'bottom', offset: [0, 14] });
});
it('FE-HOOK-ATLAS-059: keeps wheel and touch gestures over the tooltip away from the map', async () => {
const marker = await mountWithMarker();
act(() => fire(marker, 'tooltipopen'));
expect(lf.scrollPropagationOff).toContain(marker.tooltipEl);
expect(lf.clickPropagationOff).toContain(marker.tooltipEl);
});
describe('handing the pointer between marker and tooltip', () => {
beforeEach(() => vi.useFakeTimers({ shouldAdvanceTime: true }));
afterEach(() => vi.useRealTimers());
it('FE-HOOK-ATLAS-060: stays open when the pointer walks back from the tooltip onto the marker', async () => {
const marker = await mountWithMarker();
act(() => fire(marker, 'tooltipopen'));
act(() => { marker.tooltipEl.dispatchEvent(new MouseEvent('mouseleave')); });
act(() => fire(marker, 'mouseover'));
await act(async () => { await vi.advanceTimersByTimeAsync(400); });
expect(marker.closeTooltip).not.toHaveBeenCalled();
});
it('FE-HOOK-ATLAS-061: closes shortly after the pointer leaves the tooltip for good', async () => {
const marker = await mountWithMarker();
act(() => fire(marker, 'tooltipopen'));
act(() => { marker.tooltipEl.dispatchEvent(new MouseEvent('mouseleave')); });
await act(async () => { await vi.advanceTimersByTimeAsync(400); });
expect(marker.closeTooltip).toHaveBeenCalled();
});
});
});
});
+13 -1
View File
@@ -1102,7 +1102,11 @@ export function useAtlas() {
const point = map.latLngToContainerPoint(marker.getLatLng())
const screenX = containerRect.left + point.x
const screenY = containerRect.top + point.y
const placement = bucketTooltipPlacement({ x: screenX, y: screenY }, window.innerWidth, bucketTooltipWidth(window.innerWidth))
const placement = bucketTooltipPlacement(
{ x: screenX, y: screenY },
{ width: window.innerWidth, height: window.innerHeight },
bucketTooltipWidth(window.innerWidth),
)
tooltip.options.direction = placement.direction
tooltip.options.offset = placement.offset
})
@@ -1118,11 +1122,19 @@ export function useAtlas() {
const scheduleClose = () => { closeTimer = setTimeout(() => marker.closeTooltip(), 200) }
marker.off('mouseout', marker.closeTooltip, marker)
marker.on('mouseout', scheduleClose)
// The way back from the tooltip onto the marker fires the tooltip's mouseleave
// first, so the close it just scheduled has to be taken back here.
marker.on('mouseover', cancelClose)
marker.on('tooltipopen', () => {
const el = marker.getTooltip()?.getElement()
if (!el) return
el.addEventListener('mouseenter', cancelClose)
el.addEventListener('mouseleave', scheduleClose)
// The tooltip pane lives inside the map container, where Leaflet cancels both
// gestures before they reach the note: the wheel would zoom the map and a touch
// swipe would pan it instead of scrolling.
L.DomEvent.disableScrollPropagation(el)
L.DomEvent.disableClickPropagation(el)
const inner = el.querySelector<HTMLElement>('.atlas-tooltip-scroll-inner')
if (inner) inner.style.overflowY = bucketTooltipNeedsScroll(inner.scrollHeight, inner.clientHeight) ? 'auto' : 'hidden'
})