Skip to content

Commit

Permalink
✨ Maps!
Browse files Browse the repository at this point in the history
  • Loading branch information
dej611 committed Oct 8, 2017
1 parent 367d8f6 commit 34c65c2
Show file tree
Hide file tree
Showing 5 changed files with 165 additions and 4 deletions.
26 changes: 23 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,10 @@
"classnames": "^2.2.5",
"design-web-toolkit": "github:italia/design-web-toolkit",
"jquery": "^3.2.1",
"leaflet": "^1.2.0",
"react": "^15.6.1",
"react-dom": "^15.6.1",
"react-leaflet": "^1.7.0",
"tablesaw": "^3.0.3"
},
"jest": {
Expand Down
57 changes: 57 additions & 0 deletions src/components/Map/Map.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import React from 'react';
import PropTypes from 'prop-types';
import {Map, TileLayer} from 'react-leaflet';
export {
LayersControl,
MapLayer,
GridLayer,
WMSTileLayer,
ImageOverlay,
LayerGroup,
Marker,
Path,
Circle,
CircleMarker,
FeatureGroup,
GeoJSON,
Polygon,
Polyline,
Rectangle,
Popup,
Tooltip,
} from 'react-leaflet';

class OSMap extends React.Component {
state = {
isLoaded: false,
};
componentDidMount() {
this.setState({loaded: true});
}

render() {
// Avoid to load Leaflet on SSR or in NodeJS
if (!this.state.loaded) {
return null;
}

const {children, height, ...rest} = this.props;

return (
<Map {...rest} style={{height: `${height}px`}}>
<TileLayer
attribution="&amp;copy <a href=&quot;http://osm.org/copyright&quot;>OpenStreetMap</a> contributors"
url="http://{s}.tile.osm.org/{z}/{x}/{y}.png"
/>
{children}
</Map>
);
}
}

OSMap.propTypes = {
children: PropTypes.node,
height: PropTypes.number,
};

export default OSMap;
77 changes: 77 additions & 0 deletions src/components/Map/Map.story.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import React from 'react';
import {storiesOf} from '@storybook/react';
import {withInfo} from '@storybook/addon-info';
import {Map, TileLayer, Marker, Popup} from 'react-leaflet';
import L from 'leaflet';

const position = [41.9, 12.49];
const icon = L.divIcon({
html:
'<div style="background: blue; width: 55px; padding-left: 2px;"><img src="https://italia.github.io/design-web-toolkit/theme/docs/logo-it.svg" height="42" width="42"/></div>',
});

const NiceMap = ({attribution, tilesUrl}) => (
<Map center={position} zoom={10} style={{height: 500}}>
<TileLayer attribution={attribution} url={tilesUrl} />
<Marker position={position} icon={icon}>
<Popup>
<span>
I'm here and I'm a component.<br /> With OpenStreet Map
tiles.
</span>
</Popup>
</Marker>
</Map>
);

storiesOf('Map', module)
.add(
'OpenStreet Layer',
withInfo('OpenStreet Layer')(() => (
<NiceMap
tilesUrl={'http://{s}.tile.osm.org/{z}/{x}/{y}.png'}
attribution={
'&copy; <a href=&quot;http://osm.org/copyright&quot;>OpenStreetMap</a> contributors'
}
/>
))
)
.add(
'Stamen.Toner',
withInfo('Stamen.Toner')(() => (
<NiceMap
attribution={
'Map tiles by <a href="http://stamen.com">Stamen Design</a>, <a href="http://creativecommons.org/licenses/by/3.0">CC BY 3.0</a> &mdash; Map data &copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>'
}
tilesUrl={
'https://stamen-tiles-{s}.a.ssl.fastly.net/toner/{z}/{x}/{y}.png'
}
/>
))
)
.add(
'CartoDB Positron',
withInfo('CartoDB Positron')(() => (
<NiceMap
attribution={
'&copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a> &copy; <a href="http://cartodb.com/attributions">CartoDB</a>'
}
tilesUrl={
'https://cartodb-basemaps-{s}.global.ssl.fastly.net/light_all/{z}/{x}/{y}.png'
}
/>
))
)
.add(
'Stamen Watercolor',
withInfo('Stamen Watercolor')(() => (
<NiceMap
attribution={
'Map tiles by <a href="http://stamen.com">Stamen Design</a>, <a href="http://creativecommons.org/licenses/by/3.0">CC BY 3.0</a> &mdash; Map data &copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>'
}
tilesUrl={
'https://stamen-tiles-{s}.a.ssl.fastly.net/watercolor/{z}/{x}/{y}.png'
}
/>
))
);
7 changes: 6 additions & 1 deletion src/index.css
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
@import './styles/build.css';
@import '~leaflet/dist/leaflet.css';
@import './styles/build.css';

.leaflet-container {
width: 100%;
}

0 comments on commit 34c65c2

Please sign in to comment.