diff --git a/CHANGELOG.md b/CHANGELOG.md index ddd538a645..cd34af4b80 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ ### 🐞 Bug fixes - Fix type definition on `localIdeographFontFamily` ([#3896](https://github.com/maplibre/maplibre-gl-js/pull/3896)) - Fix unwanted panning changes at the end of a panning motion ([#3872](https://github.com/maplibre/maplibre-gl-js/issues/3872)) +- Fix `close` events being fired for popups that aren't open - _...Add new stuff here..._ ## 4.1.1 diff --git a/src/ui/popup.test.ts b/src/ui/popup.test.ts index eeb4b212b5..7d02c68f45 100644 --- a/src/ui/popup.test.ts +++ b/src/ui/popup.test.ts @@ -132,6 +132,18 @@ describe('popup', () => { expect(onClose).toHaveBeenCalled(); }); + test('Popup does not fire close event when removed if it is not on the map', () => { + const onClose = jest.fn(); + + new Popup() + .setText('Test') + .setLngLat([0, 0]) + .on('close', onClose) + .remove(); + + expect(onClose).not.toHaveBeenCalled(); + }); + test('Popup fires open event when added', () => { const map = createMap(); const onOpen = jest.fn(); diff --git a/src/ui/popup.ts b/src/ui/popup.ts index 97bf0d48c3..9bcec231ba 100644 --- a/src/ui/popup.ts +++ b/src/ui/popup.ts @@ -258,10 +258,9 @@ export class Popup extends Evented { this._map.off('drag', this._onDrag); this._map._canvasContainer.classList.remove('maplibregl-track-pointer'); delete this._map; + this.fire(new Event('close')); } - this.fire(new Event('close')); - return this; };