Skip to content

Commit

Permalink
add className option to Popup constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
Poluektov Dmitriy committed Apr 11, 2018
1 parent 6b96d69 commit 4ebba60
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/ui/popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ export type PopupOptions = {
closeButton: boolean,
closeOnClick: boolean,
anchor: Anchor,
offset: Offset
offset: Offset,
className: String
};

/**
Expand All @@ -46,8 +47,10 @@ export type PopupOptions = {
* - a {@link PointLike} specifying a constant offset
* - an object of {@link Point}s specifing an offset for each anchor position
* Negative offsets indicate left and up.
* @param {string} [options.className] A string with classes that will be added to popup container
* @example
* var markerHeight = 50, markerRadius = 10, linearOffset = 25;
* var popupContainerClassName = ['some', 'additional', 'class']
* var popupOffsets = {
* 'top': [0, 0],
* 'top-left': [0,0],
Expand All @@ -58,7 +61,7 @@ export type PopupOptions = {
* 'left': [markerRadius, (markerHeight - markerRadius) * -1],
* 'right': [-markerRadius, (markerHeight - markerRadius) * -1]
* };
* var popup = new mapboxgl.Popup({offset:popupOffsets})
* var popup = new mapboxgl.Popup({offset:popupOffsets, className: popupContainerClassName})
* .setLngLat(e.lngLat)
* .setHTML("<h1>Hello World!</h1>")
* .addTo(map);
Expand Down Expand Up @@ -274,6 +277,14 @@ export default class Popup extends Evented {
this._lngLat = smartWrap(this._lngLat, this._pos, this._map.transform);
}

const className: String = this.options.className;

if (className) {
className.split(' ').forEach((clsName) => {
this._container.classList.add(clsName);
});
}

const pos = this._pos = this._map.project(this._lngLat);

let anchor: Anchor = this.options.anchor;
Expand Down
16 changes: 16 additions & 0 deletions test/unit/ui/popup.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -446,3 +446,19 @@ test('Popup#remove is idempotent (#2395)', (t) => {
t.equal(map.getContainer().querySelectorAll('.mapboxgl-popup').length, 0);
t.end();
});

test('Popup#add classes from className option', (t) => {
const map = createMap();
const classNames = ['some', 'classes'];
new Popup({
className: classNames.join(' ')
})
.setText("Test")
.setLngLat([0, 0])
.addTo(map);

const popupContainer = map.getContainer().querySelector('.mapboxgl-popup');

t.ok(popupContainer.classList.contains(classNames[0]) && popupContainer.classList.contains(classNames[1]));
t.end();
});

0 comments on commit 4ebba60

Please sign in to comment.