Coder Social home page Coder Social logo

gabidavila / react-usa-map Goto Github PK

View Code? Open in Web Editor NEW
82.0 4.0 86.0 839 KB

React component with all USA States with customizable options. No D3 needed.

Home Page: http://react-usa-map-demo.herokuapp.com/

License: MIT License

JavaScript 100.00%
react map usa svg svg-map

react-usa-map's Introduction

react-usa-map | A simple SVG USA map rendering on React

Build Status codebeat badge

This is an alternate version for you that just want a simple customizable map on HTML. This maps shows states delimitations including DC, Alaska, and Hawaii. D3 is not needed.

It uses the Albers projection.

Live: http://react-usa-map-demo.herokuapp.com

Code: http://github.com/gabidavila/react-usa-map-demo

Installation

It requires react 16.13.1 or higher. Run:

yarn add react-usa-map

or

npm install react-usa-map --save

Usage

The below example shows the mandatory onClick event.

import React, { Component } from 'react';
import USAMap from "react-usa-map";

class App extends Component {
  /* mandatory */
  mapHandler = (event) => {
    alert(event.target.dataset.name);
  };

  render() {
    return (
      <div className="App">
        <USAMap onClick={this.mapHandler} />
      </div>
    );
  }
}

export default App;

Example with optional props, App.js:

import React, { Component } from 'react';
import './App.css'; /* optional for styling like the :hover pseudo-class */
import USAMap from "react-usa-map";

class App extends Component {
  /* mandatory */
  mapHandler = (event) => {
    alert(event.target.dataset.name);
  };

  /* optional customization of filling per state and calling custom callbacks per state */
  statesCustomConfig = () => {
    return {
      "NJ": {
        fill: "navy",
        clickHandler: (event) => console.log('Custom handler for NJ', event.target.dataset)
      },
      "NY": {
        fill: "#CC0000"
      }
    };
  };

  render() {
    return (
      <div className="App">
        <USAMap customize={this.statesCustomConfig()} onClick={this.mapHandler} />
      </div>
    );
  }
}

export default App;

App.css:

path {
  pointer-events: all;
}
path:hover {
  opacity: 0.50;
  cursor: pointer;
}

All optional props:

prop description
title Content for the Title attribute on the svg
width The width for rendering, numeric, no px suffix
height The height for rendering, numeric, no px suffix
defaultFill The default color for filling
customize Optional customization of filling per state

Additionally each path tag has an abbreviation of the current state followed by a state class:

<path fill="#{custom color or #D3D3D3}" data-name="CA" class="CA state" d="...{polygon dimensions here}..."></path>

License

MIT.

Sources

The map is sourced from Wikimedia and is under Creative Commons Attribution-Share Alike 3.0 Unported license. This package is inspired on the react-us-state-map package, in fact the initial SVG class system is based on it.

Contributing

Fork and PR. Not much fuss, I will be try to be as responsive as possible.

Maintainer

Package maintaned by Gabriela D'Ávila Ferrara, website.

react-usa-map's People

Contributors

alithanar avatar dependabot[bot] avatar emman27 avatar gabidavila avatar lachlanjc avatar maxterry avatar sachinlamba avatar willmendesneto avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

react-usa-map's Issues

IE11 support

Might be a good idea to add to the documentation that IE11 doesn't support:
event.target.data-set.name
it needs to be updated to event.target.getAttribute('data-name') to work in IE11

Unable to resolve dependency tree

When attempting to install react-usa-map on linux (npm install react-usa-map --save)
I got the following errors:

While resolving: [email protected]
Found: [email protected]
node_modules/react
react@"^17.0.2" from the root project

Could not resolve dependency:
peer react@"^16.13.1" from [email protected]
node_modules/react-usa-map
react-usa-map@"*" from the root project

Any suggestions on how to resolve this issue?

Just passing to say thanks :+1:

Hi, I just want to say thanks for sharing this component! I hope to contribute a bit as soon as I get some time.

Thank you!

Filling for DC doesn't work

I fill all states with a color including DC but DC stays gray

AK: {fill: "#B5C460"}
AL: {fill: "#B5C460"}
AR: {fill: "#B5C460"}
AZ: {fill: "#B5C460"}
CA: {fill: "#B5C460"}
CO: {fill: "#B5C460"}
CT: {fill: "#B5C460"}
DC: {fill: "#B5C460"}
DE: {fill: "#B5C460"}
FL: {fill: "#B5C460"}
GA: {fill: "#B5C460"}
HI: {fill: "#B5C460"}
IA: {fill: "#B5C460"}
ID: {fill: "#B5C460"}
IL: {fill: "#B5C460"}
IN: {fill: "#B5C460"}
KS: {fill: "#B5C460"}
KY: {fill: "#B5C460"}
LA: {fill: "#B5C460"}
MA: {fill: "#B5C460"}
MD: {fill: "#B5C460"}
ME: {fill: "#B5C460"}
MI: {fill: "#B5C460"}
MN: {fill: "#B5C460"}
MO: {fill: "#B5C460"}
MS: {fill: "#B5C460"}
MT: {fill: "#B5C460"}
NC: {fill: "#B5C460"}
ND: {fill: "#B5C460"}
NE: {fill: "#B5C460"}
NH: {fill: "#B5C460"}
NJ: {fill: "#B5C460"}
NM: {fill: "#B5C460"}
NV: {fill: "#B5C460"}
NY: {fill: "#B5C460"}
OH: {fill: "#B5C460"}
OK: {fill: "#B5C460"}
OR: {fill: "#B5C460"}
PA: {fill: "#B5C460"}
RI: {fill: "#B5C460"}
SC: {fill: "#B5C460"}
SD: {fill: "#B5C460"}
TN: {fill: "#B5C460"}
TX: {fill: "#B5C460"}
UT: {fill: "#B5C460"}
VA: {fill: "#B5C460"}
VT: {fill: "#B5C460"}
WA: {fill: "#B5C460"}
WI: {fill: "#B5C460"}
WV: {fill: "#B5C460"}
WY: {fill: "#B5C460"}

Screenshot 2020-07-29 at 13 59 06

react-javascript: 'define' is not defined

When I fork the app, I get the following error:


Generating development Javascript Bundle failed

       react-usa-map/dist/index.js
       2:39 ERROR 'define' is not defined
       3:5 ERROR 'define' is not defined

Map responsiveness

Is there a way we can embed this map in a responsive way?

I only see we can set width and height props with px values.

Add Tests

Tests are currently not supported. Add them to the package.

Add state-specific classes to customize

Currently the className for USAState paths is fixed as ${props.state} state and I would like to be able to pass in additional classes for each state. I imagine this would look something like: <USAMap customize={{ "CO": { class: "active" }, "NJ": { class: "active" }} />. My specific use case is being able to adjust stroke on states, but adjustable classes seems like a good way to make all style adjustable.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.