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 6 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
34 changes: 31 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 All @@ -50,6 +52,7 @@ export type PopupOptions = {
* - an object of {@link Point}s specifing an offset for each anchor position
* Negative offsets indicate left and up.
* @param {string} [options.className] Space-separated CSS class names to add to popup container
* @param {string} [options.maxWidth] A string that sets the CSS property of the popup's maxWidth in pixels, eg "300px"
* @example
* var markerHeight = 50, markerRadius = 10, linearOffset = 25;
* var popupOffsets = {
Expand All @@ -65,6 +68,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 +235,27 @@ 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 {string} The max width of the popup.
*/
getMaxWidth() {
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`
*/
setMaxWidth(maxWidth: string) {
this.options.maxWidth = maxWidth;
this._update();
return this;
}

/**
* Sets the popup's content to the element provided as a DOM node.
*
Expand Down Expand Up @@ -275,13 +300,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) {
this._container.style.maxWidth = this.options.maxWidth;
}

if (this._map.transform.renderWorldCopies) {
this._lngLat = smartWrap(this._lngLat, this._pos, this._map.transform);
}
Expand Down
37 changes: 37 additions & 0 deletions test/unit/ui/popup.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,43 @@ 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.getMaxWidth(), "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.getMaxWidth(), "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>")
.setMaxWidth("5px")
.addTo(map);

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

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