Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

react-world-map

heatherbooker1.8kWTFPL2.4.0

clickable world map built with React with css changes

react, react-component, world, map, clickable, select, continents

readme

React world map

  • clickable world map with continent areas
  • click on an area to select it (approx. continents)
  • selecting it will change the className --> you add css to change the color
  • built of SVG paths

Demo

Codepen (interactive demo)

gif of map being clicked and changing colour

usage:

npm install react-world-map --save

or, include

<script src=""https://cdn.jsdelivr.net/gh/heatherbooker/react-world-map@master/dist/index.js"></script>

in your html to use the script directly.

then in your index.jsx or main.jsx (see bottom for css):

Controlled component:

<WorldMap selected={ 'sa' } onSelect={ (continent) => {} } />

Uncontrolled component:

<WorldMap />

Uncontrolled component also supports multiple selection using the multiple = { true } prop. Example:

<WorldMap
  multiple={ true }
/>

Full usage:

const React, { useState } = require('react');
const ReactDOM = require('react-dom');
const WorldMap = require('react-world-map');

function YourMainComponent() {
  const [selected, onSelect] = useState(null);

  return (
    <>
      <h1> Hello World Map!</h1>
      <WorldMap selected={ selected } onSelect={ onSelect } />
    </>
  );
}

ReactDOM.render(
  <YourMainComponent />,
  document.getElementById('react-app')
)

Props

  • onSelect: (continentCode) => void
  • selected: continentCode

whereas the list of possible continents is as follows:

  • na - North America
  • sa - South America
  • af - Africa
  • eu - Europe
  • as - Asia
  • oc - Oceania

make sure to set your css to target the map:

.map-selected {
  fill: #E3DA37;
}

.map-unselected {
  fill: #699EAA;
}

.map-selected:hover, .map-unselected:hover {
  cursor: pointer;
}

Please feel free to fork and PR if you think you have developed something useful!

license: WTFPL

bonus: blog post on the making of this map :)

changelog

Changelog

2.4.0 - 2022-01-07

  • Accept react 17 as peer dependency (as well as 16), thanks @VWRoli !

2.3.0 - 2021-02-04

  • Add support for multiple selection in uncontrolled map.

2.2.0 - 2020-01-19

  • Use controlled component if selected OR onSelect props are given
  • 2.1.0 was stupid, that should have been a branch and not smack dab on main branch -_-

2.1.0 - 2020-01-19

  • Check specifically for 'undefined' value in props.selected
  • Add console warning for passing only 1 out of 2 props for controlled component

2.0.0 - 2020-09-28

BREAKING VERSION CHANGE from 1.0.13!

  • Upgrade react, react-dom versions to 16 (set as peer dependencies)
  • Add selected and onSelect optional props

And things you probably care less about (dev process):

  • Upgrade webpack, babel
  • Add typescript (internal only)
  • Update demo

1.0.13 - 2016-06-24

Wow that was a while ago. The component emitted browser events instead of actually using React. Super weird choice I made back then :joy: