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 max width for popups #7906

Merged
merged 8 commits into from
Mar 21, 2019
Merged
Show file tree
Hide file tree
Changes from 4 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
36 changes: 33 additions & 3 deletions src/ui/popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ import type {PointLike} from '@mapbox/point-geometry';
const defaultOptions = {
closeButton: true,
closeOnClick: true,
className: ''
className: '',
maxWidth: "240px"
};

export type Offset = number | PointLike | {[Anchor]: PointLike};
Expand All @@ -26,7 +27,8 @@ export type PopupOptions = {
closeOnClick?: boolean,
anchor?: Anchor,
offset?: Offset,
className?: string
className?: string,
maxWidth?: string
};

/**
Expand Down Expand Up @@ -65,6 +67,7 @@ export type PopupOptions = {
* var popup = new mapboxgl.Popup({offset: popupOffsets, className: 'my-class'})
* .setLngLat(e.lngLat)
* .setHTML("<h1>Hello World!</h1>")
* .setMaxWidth("300px")
* .addTo(map);
* @see [Display a popup](https://www.mapbox.com/mapbox-gl-js/example/popup/)
* @see [Display a popup on hover](https://www.mapbox.com/mapbox-gl-js/example/popup-on-hover/)
Expand Down Expand Up @@ -231,6 +234,30 @@ export default class Popup extends Evented {
return this.setDOMContent(frag);
}

/**
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has to be moved above the setMaxWidth (or set maxWidth if you go the getter/setter route) method. You should also add a doc comment for getMaxWidth.

* Returns the popup's max width.
*
* @returns {LngLat} The max width of the popup.
*/
get maxWidth() {
return this._container.style.maxWidth;
}

/**
* Sets the popup's max width.
*
* This is setting the CSS property maxWidth. It expects a string in "Npx" format, where N is some number.
*
* @param maxWidth A string representing the pixel value for the maximum width.
* @returns {Popup} `this`
*/
set maxWidth(maxWidth: string) {
console.log(this.options.maxWidth);
mzdraper marked this conversation as resolved.
Show resolved Hide resolved
this.options.maxWidth = maxWidth;
console.log(this.options.maxWidth);
this._update();
}

/**
* Sets the popup's content to the element provided as a DOM node.
*
Expand Down Expand Up @@ -275,13 +302,16 @@ export default class Popup extends Evented {
this._container = DOM.create('div', 'mapboxgl-popup', this._map.getContainer());
this._tip = DOM.create('div', 'mapboxgl-popup-tip', this._container);
this._container.appendChild(this._content);

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

if (this._container.style.maxWidth != this.options.maxWidth) {
mzdraper marked this conversation as resolved.
Show resolved Hide resolved
this._container.style.maxWidth = this.options.maxWidth;
}

if (this._map.transform.renderWorldCopies) {
this._lngLat = smartWrap(this._lngLat, this._pos, this._map.transform);
}
Expand Down
40 changes: 40 additions & 0 deletions test/unit/ui/popup.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,46 @@ test('Popup content can be set via setHTML', (t) => {
t.end();
});

test('Popup width maximum defaults to 240px', (t) => {
const map = createMap(t);

const popup = new Popup({closeButton: false})
.setLngLat([0, 0])
.addTo(map)
.setHTML("<span>Test</span>");

t.equal(popup.maxWidth, "240px");
t.end();
});

test('Popup width maximum can be set via using maxWidth option', (t) => {
const map = createMap(t);

const popup = new Popup({closeButton: false, maxWidth: "5px"})
.setLngLat([0, 0])
.addTo(map)
.setHTML("<span>Test</span>");

t.equal(popup.maxWidth, "5px");
t.end();
});

test('Popup width maximum can be set via maxWidth', (t) => {
const map = createMap(t);

const popup = new Popup({closeButton: false})
.setLngLat([0, 0])
.setHTML("<span>Test</span>")
.addTo(map);

popup.maxWidth = "5px";
t.equal(popup.maxWidth, "5px");

popup.maxWidth = "15px";
t.equal(popup.maxWidth, "15px");
t.end();
});

test('Popup content can be set via setDOMContent', (t) => {
const map = createMap(t);
const content = window.document.createElement('span');
Expand Down