Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to close popup on map move #9163

Merged
merged 1 commit into from
Jan 7, 2020
Merged
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
18 changes: 13 additions & 5 deletions src/ui/popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export type Offset = number | PointLike | {[Anchor]: PointLike};
export type PopupOptions = {
closeButton?: boolean,
closeOnClick?: boolean,
closeOnMove?: boolean,
anchor?: Anchor,
offset?: Offset,
className?: string,
Expand All @@ -39,6 +40,8 @@ export type PopupOptions = {
* top right corner of the popup.
* @param {boolean} [options.closeOnClick=true] If `true`, the popup will closed when the
* map is clicked.
* @param {boolean} [options.closeOnMove=false] If `true`, the popup will closed when the
* map moves.
* @param {string} [options.anchor] - A string indicating the part of the Popup that should
* be positioned closest to the coordinate set via {@link Popup#setLngLat}.
* Options are `'center'`, `'top'`, `'bottom'`, `'left'`, `'right'`, `'top-left'`,
Expand Down Expand Up @@ -92,7 +95,7 @@ export default class Popup extends Evented {
constructor(options: PopupOptions) {
super();
this.options = extend(Object.create(defaultOptions), options);
bindAll(['_update', '_onClickClose', 'remove'], this);
bindAll(['_update', '_onClose', 'remove'], this);
}

/**
Expand All @@ -104,7 +107,11 @@ export default class Popup extends Evented {
addTo(map: Map) {
this._map = map;
if (this.options.closeOnClick) {
this._map.on('click', this._onClickClose);
this._map.on('click', this._onClose);
}

if (this.options.closeOnMove) {
this._map.on('move', this._onClose);
}

this._map.on('remove', this.remove);
Expand Down Expand Up @@ -162,7 +169,8 @@ export default class Popup extends Evented {

if (this._map) {
this._map.off('move', this._update);
this._map.off('click', this._onClickClose);
this._map.off('move', this._onClose);
this._map.off('click', this._onClose);
this._map.off('remove', this.remove);
this._map.off('mousemove');
delete this._map;
Expand Down Expand Up @@ -390,7 +398,7 @@ export default class Popup extends Evented {
this._closeButton.type = 'button';
this._closeButton.setAttribute('aria-label', 'Close popup');
this._closeButton.innerHTML = '×';
this._closeButton.addEventListener('click', this._onClickClose);
this._closeButton.addEventListener('click', this._onClose);
}

}
Expand Down Expand Up @@ -460,7 +468,7 @@ export default class Popup extends Evented {
applyAnchorClass(this._container, anchor, 'popup');
}

_onClickClose() {
_onClose() {
this.remove();
}
}
Expand Down
26 changes: 26 additions & 0 deletions test/unit/ui/popup.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,32 @@ test('Popup has no close button if closeButton option is false', (t) => {
t.end();
});

test('Popup does not close on map move events when the closeOnMove option is false', (t) => {
const map = createMap(t);
const popup = new Popup({closeOnMove: false})
.setText('Test')
.setLngLat([0, 0])
.addTo(map);

map.setCenter([-10, 0]); // longitude bounds: [-370, 350]

t.ok(popup.isOpen());
t.end();
});

test('Popup closes on map move events when the closeOnMove option is true', (t) => {
const map = createMap(t);
const popup = new Popup({closeOnMove: true})
.setText('Test')
.setLngLat([0, 0])
.addTo(map);

map.setCenter([-10, 0]); // longitude bounds: [-370, 350]

t.ok(!popup.isOpen());
t.end();
});

test('Popup fires close event when removed', (t) => {
const map = createMap(t);
const onClose = t.spy();
Expand Down