Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 21 additions & 14 deletions src/ui/popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -577,27 +577,34 @@ export default class Popup extends Evented {

if (!map || !container || !pos) return 'bottom';

const padding = map.transform.padding;
const paddingTop = (padding.top || 0);
const paddingBottom = (padding.bottom || 0);
const paddingLeft = (padding.left || 0);
const paddingRight = (padding.right || 0);

const width = container.offsetWidth;
const height = container.offsetHeight;

const isTop = pos.y + bottomY < height;
const isBottom = pos.y > map.transform.height - height;
const isLeft = pos.x < width / 2;
const isRight = pos.x > map.transform.width - width / 2;
const isTop = pos.y + bottomY < height + paddingTop;
const isLeft = pos.x < width / 2 + paddingLeft;
const isRight = pos.x > map.transform.width - width / 2 - paddingRight;
const isTopOnEitherSide = pos.y + bottomY < height / 2 + paddingTop;
const isBottomOnEitherSide = pos.y > map.transform.height - height / 2 - paddingBottom;

if (isTop) {
if (isLeft) return 'top-left';
if (isRight) return 'top-right';
return 'top';
if (isLeft) {
if (isTopOnEitherSide) return 'top-left';
if (isBottomOnEitherSide) return 'bottom-left';
return 'left';
}
if (isBottom) {
if (isLeft) return 'bottom-left';
if (isRight) return 'bottom-right';

if (isRight) {
if (isTopOnEitherSide) return 'top-right';
if (isBottomOnEitherSide) return 'bottom-right';
return 'right';
}
if (isLeft) return 'left';
if (isRight) return 'right';

return 'bottom';
return isTop ? 'top' : 'bottom';
}

_updateClassList() {
Expand Down
21 changes: 21 additions & 0 deletions test/unit/ui/popup.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,27 @@ test('Popup', (t) => {
const point = args[1];
const transform = args[2];

test('Popup respects padding and changes anchor accordingly', (t) => {
const map = createMap(t);
map.setPadding({top: 10, bottom: 10, left: 10, right: 10});
const popup = new Popup()
.setText('Test')
.setLngLat([0, 0])
.addTo(map);
map._domRenderTaskQueue.run();

Object.defineProperty(popup.getElement(), 'offsetWidth', {value: 5});
Object.defineProperty(popup.getElement(), 'offsetHeight', {value: 5});

t.stub(map, 'project').returns(point);
t.stub(map.transform, 'locationPoint3D').returns(point);
popup.setLngLat([0, 0]);
map._domRenderTaskQueue.run();

t.ok(popup.getElement().classList.contains(`mapboxgl-popup-anchor-${anchor}`));
t.end();
});

test(`Popup automatically anchors to ${anchor}`, (t) => {
const map = createMap(t);
const popup = new Popup()
Expand Down