Skip to content

Commit effa64b

Browse files
committed
Add maxBounds prop, disable scroll zoom during..
Add `maxBounds` prop, disable scroll zoom during change in bounds
1 parent 8286d9b commit effa64b

1 file changed

Lines changed: 22 additions & 3 deletions

File tree

lib/Mapbox/Map.js

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { arraysAreEqual, objectFromEntries } from '../utils'
88
export default class Map extends React.Component {
99
static propTypes = {
1010
bounds: PropTypes.array.isRequired,
11+
maxBounds: PropTypes.array,
1112
minZoom: PropTypes.number,
1213
maxZoom: PropTypes.number,
1314
disableRotation: PropTypes.bool,
@@ -20,6 +21,7 @@ export default class Map extends React.Component {
2021
static defaultProps = {
2122
minZoom: 3,
2223
maxZoom: 16,
24+
maxBounds: null,
2325
disableRotation: false,
2426
onPan: () => {},
2527
onZoom: () => {},
@@ -45,14 +47,15 @@ export default class Map extends React.Component {
4547
}
4648

4749
componentDidMount() {
48-
const { minZoom, maxZoom, disableRotation, onPan, onZoom, onZoomEnd, bounds } = this.props
50+
const { minZoom, maxZoom, disableRotation, onPan, onZoom, onZoomEnd, bounds, maxBounds } = this.props
4951

5052
this.map = new mapboxgl.Map({
5153
container: this.mapNodeRef.current,
5254
style: clone(this.style),
5355
minZoom,
5456
maxZoom,
5557
bounds,
58+
maxBounds,
5659
attributionControl: false
5760
})
5861

@@ -70,6 +73,7 @@ export default class Map extends React.Component {
7073

7174
this.map.on('moveend', () => {
7275
this.mapIsMoving = false
76+
this.enableInteraction()
7377
setTimeout(() => {
7478
if (!this.mapIsMoving) {
7579
const bounds = this.map.getBounds().toArray()
@@ -88,18 +92,25 @@ export default class Map extends React.Component {
8892

8993
this.map.on('zoomend', () => {
9094
const bounds = this.map.getBounds().toArray()
95+
96+
this.enableInteraction()
9197
onZoomEnd(this.map.getZoom(), [...bounds[0], ...bounds[1]].map(val => parseFloat(val.toFixed(9))))
9298
})
9399

94100
this.forceUpdate()
95101
}
96102

97103
componentDidUpdate(prevProps) {
98-
const { bounds, disableRotation } = this.props
104+
const { bounds, maxBounds, disableRotation } = this.props
99105
if (!arraysAreEqual(prevProps.bounds, bounds)) {
106+
this.disableInteraction()
100107
this.map.fitBounds(bounds)
101108
}
102109

110+
if (!arraysAreEqual(prevProps.maxBounds, maxBounds)) {
111+
this.map.setMaxBounds(maxBounds)
112+
}
113+
103114
if (prevProps.disableRotation !== disableRotation) {
104115
if (disableRotation) {
105116
this.map.dragRotate.disable()
@@ -112,12 +123,20 @@ export default class Map extends React.Component {
112123
}
113124
}
114125

126+
enableInteraction = () => {
127+
this.map.scrollZoom.enable()
128+
}
129+
130+
disableInteraction = () => {
131+
this.map.scrollZoom.disable()
132+
}
133+
115134
updateStyle = () => {
116135
if (this.map) {
117136
if (this.map.isStyleLoaded()) {
118137
this.style = this.mergeStyles(this.style, this.map.getStyle())
119138
}
120-
139+
121140
this.map.setStyle(clone(this.style))
122141
}
123142

0 commit comments

Comments
 (0)