fix(regions): keep map labels of nearby regions apart
ci / lint (push) Skipped
ci / test (push) Skipped
ci / build (push) Skipped
ci / image (push) Skipped
ci / lint (pull_request) Successful in 11m19s
ci / test (pull_request) Successful in 13m11s
ci / build (pull_request) Successful in 18m31s
ci / image (pull_request) Successful in 5m52s

Regions a few kilometres apart drew their labels on top of each other.
As on the landing page, a label now runs to the right of its point, or
to the left where the right side would cover another pin or leave the
map; a pin with room on neither side shows only its dot and reveals its
name on hover. Placement reruns while zooming and panning.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
This commit is contained in:
Sebastian UnterschützandClaude Opus 5.5 committed 2026-11-29 11:06:42 +01:00
1 parent d91d600c90
commit 401a361422
3 files changed
+74 -1

No files matched your search

+16
View File
@@ -164,6 +164,22 @@
font-size: 12px;
}
}
/* Pins whose label would cover another one turn it to the left of their
point, or show only their dot until hovered. */
.is-flipped > .region-pin {
flex-direction: row-reverse;
padding: 3px 5px 3px 9px;
translate: calc(16px - 100%);
}
.maplibregl-marker:is(.is-flipped, .is-compact):not(.is-hover, :hover) { z-index: 2; }
.is-compact > .region-pin { gap: 0; padding: 3px 5px; }
.is-compact:not(.is-hover, :hover) > .region-pin > :last-child { display: none; }
.is-compact.is-hover > .region-pin,
.is-compact:hover > .region-pin { gap: 6px; padding: 3px 9px 3px 5px; }
@media (pointer: coarse) {
.is-flipped > .region-pin { padding: 6px 8px 6px 11px; }
.is-compact > .region-pin { padding: 6px 8px; }
}
.region-pin:focus-visible { outline: 2px solid var(--color-maintenance); outline-offset: 2px; }
.region-pick {
width: 14px;
+44
View File
@@ -698,6 +698,7 @@ for (const el of document.querySelectorAll("input[data-tz-default]")) {
fit();
const selecting = box.dataset.regionMap === "select";
const markers = [];
for (const item of items) {
// The list row (a label or table row) and the marker light up together.
const row = item.closest("label, tr") ?? item;
@@ -708,6 +709,7 @@ for (const el of document.querySelectorAll("input[data-tz-default]")) {
const holder = document.createElement("div");
holder.append(pin);
const marker = new gl.Marker({ element: holder, anchor: "left", offset: [-8, 0] }).setLngLat(lngLat(item)).addTo(map);
markers.push({ holder, pin, marker });
const hover = (on) => {
holder.classList.toggle("is-hover", on);
row.classList.toggle("is-hover", on);
@@ -743,6 +745,48 @@ for (const el of document.querySelectorAll("input[data-tz-default]")) {
});
}
}
// Labels run to the right of their point, or to the left where that
// would cover another pin or leave the map, as with regions a few
// kilometres apart. A pin with room on neither side shows only its dot
// and its name on hover; the list names every region. No label covers
// the dot of another pin.
const place = () => {
const width = box.clientWidth;
const height = box.clientHeight;
if (!width) return;
for (const m of markers) m.holder.classList.remove("is-flipped", "is-compact");
const spots = markers.map((m) => {
const at = map.project(m.marker.getLngLat());
const h = m.pin.offsetHeight;
return { m, at, w: m.pin.offsetWidth, h, dot: { left: at.x - 8, right: at.x + 12, top: at.y - h / 2, bottom: at.y + h / 2 } };
});
// The rounded ends of two pills may touch.
const hits = (a, b) => a.left + 4 < b.right - 4 && b.left + 4 < a.right - 4 && a.top < b.bottom && b.top < a.bottom;
const labels = [];
const fits = (a, self) =>
a.left >= 0 && a.right <= width && a.top >= 0 && a.bottom <= height &&
!labels.some((b) => hits(a, b)) &&
// Dots stacked on its own one stay beside it either way.
!spots.some((s) => s !== self && !hits(s.dot, self.dot) && hits(a, s.dot));
for (const s of spots.sort((a, b) => b.at.x - a.at.x)) {
const right = { ...s.dot, right: s.at.x - 8 + s.w };
const left = { ...s.dot, left: s.at.x + 8 - s.w, right: s.at.x + 8 };
if (fits(right, s)) {
labels.push(right);
} else if (fits(left, s)) {
s.m.holder.classList.add("is-flipped");
labels.push(left);
} else {
s.m.holder.classList.add("is-compact");
}
}
};
map.on("load", place);
map.on("zoom", place);
map.on("moveend", place);
map.on("resize", place);
if (selecting) return;
let pick;
map.on("click", (ev) => {