diff --git a/src/ui/popup.js b/src/ui/popup.js index 2732eb82857..a5a19116ae8 100644 --- a/src/ui/popup.js +++ b/src/ui/popup.js @@ -432,6 +432,18 @@ export default class Popup extends Evented { } } + /** + * Sets the popup's offset. + * + * @param offset Sets the popup's offset. + * @returns {Popup} `this` + */ + setOffset (offset?: Offset) { + this.options.offset = offset; + this._update(); + return this; + } + /** * Add or remove the given CSS class on the popup container, depending on whether the container currently has that class. * diff --git a/test/unit/ui/popup.test.js b/test/unit/ui/popup.test.js index 5de4adc716b..50f94065c8f 100644 --- a/test/unit/ui/popup.test.js +++ b/test/unit/ui/popup.test.js @@ -480,6 +480,22 @@ test('Popup is offset via an incomplete object offset option', (t) => { t.end(); }); +test('Popup offset can be set via setOffset', (t) => { + const map = createMap(t); + + const popup = new Popup({offset: 5}) + .setLngLat([0, 0]) + .setText('Test') + .addTo(map); + + t.equal(popup.options.offset, 5); + + popup.setOffset(10); + + t.equal(popup.options.offset, 10); + t.end(); +}); + test('Popup can be removed and added again (#1477)', (t) => { const map = createMap(t);