Skip to content

Commit 34ae92f

Browse files
committed
feat(NavigationControl): add NavigationControl #116
1 parent 78e0ff7 commit 34ae92f

File tree

5 files changed

+86
-1
lines changed

5 files changed

+86
-1
lines changed

docs/navigation-control.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
A `NavigationControl` control contains zoom buttons and a compass.
2+
3+
```jsx
4+
<MapGL
5+
style={{ width: '100%', height: '400px' }}
6+
mapStyle='mapbox://styles/mapbox/light-v9'
7+
accessToken={MAPBOX_ACCESS_TOKEN}
8+
latitude={37.78}
9+
longitude={-122.41}
10+
zoom={11}
11+
>
12+
<NavigationControl showCompass showZoom position='top-left' />
13+
</MapGL>
14+
```

flow-typed/types.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import type Map from 'mapbox-gl/src/ui/map';
44
import type Popup from 'mapbox-gl/src/ui/popup';
55
import type Marker from 'mapbox-gl/src/ui/marker';
6+
import type NavigationControl from 'mapbox-gl/src/ui/control/navigation_control';
67
import type { Map as ImmutableMap } from 'immutable';
78
import type { MapMouseEvent, MapTouchEvent } from 'mapbox-gl/src/ui/events';
89

@@ -20,6 +21,8 @@ declare type MapboxPopup = Popup;
2021

2122
declare type MapboxMarker = Marker;
2223

24+
declare type MapboxNavigationControl = NavigationControl;
25+
2326
declare type MapboxLngLatBoundsLike = LngLatBoundsLike;
2427

2528
declare type MapboxLayerSpecification = LayerSpecification;
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// @flow
2+
3+
import { PureComponent, createElement } from 'react';
4+
5+
import MapContext from '../MapContext';
6+
import mapboxgl from '../../utils/mapbox-gl';
7+
8+
type Props = {
9+
/* If true the compass button is included. */
10+
showCompass: boolean,
11+
12+
/* If true the zoom-in and zoom-out buttons are included. */
13+
showZoom: boolean,
14+
15+
/* A string representing the position of the control on the map. */
16+
position?: 'top-left' | 'top-right' | 'bottom-left' | 'bottom-right'
17+
};
18+
19+
/**
20+
* A `NavigationControl` control contains zoom buttons and a compass.
21+
*/
22+
class NavigationControl extends PureComponent<Props> {
23+
_map: MapboxMap;
24+
25+
_control: MapboxNavigationControl;
26+
27+
static defaultProps = {
28+
position: 'top-right'
29+
};
30+
31+
componentDidMount() {
32+
const map: MapboxMap = this._map;
33+
const { showCompass, showZoom, position } = this.props;
34+
35+
const control: MapboxNavigationControl = new mapboxgl.NavigationControl({
36+
showCompass,
37+
showZoom
38+
});
39+
40+
map.addControl(control, position);
41+
this._control = control;
42+
}
43+
44+
componentWillUnmount() {
45+
if (!this._map) {
46+
return;
47+
}
48+
49+
this._map.removeControl(this._control);
50+
}
51+
52+
render() {
53+
return createElement(MapContext.Consumer, {}, (map) => {
54+
if (map) {
55+
this._map = map;
56+
}
57+
return null;
58+
});
59+
}
60+
}
61+
62+
export default NavigationControl;

src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ export { default as Source } from './components/Source';
66
export { default as Popup } from './components/Popup';
77
export { default as Marker } from './components/Marker';
88
export { default as Cluster } from './components/Cluster';
9+
export { default as NavigationControl } from './components/NavigationControl';

styleguide.config.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ module.exports = {
2323
'src/components/Layer/index.js',
2424
'src/components/Popup/index.js',
2525
'src/components/Marker/index.js',
26-
'src/components/Cluster/index.js'
26+
'src/components/Cluster/index.js',
27+
'src/components/NavigationControl/index.js'
2728
]
2829
},
2930
{
@@ -80,6 +81,10 @@ module.exports = {
8081
{
8182
name: 'Change Map style',
8283
content: 'docs/change-map-style.md'
84+
},
85+
{
86+
name: 'Navigation Control',
87+
content: 'docs/navigation-control.md'
8388
}
8489
]
8590
}

0 commit comments

Comments
 (0)