Giter Site home page Giter Site logo

victorcazanave / react-svg-map Goto Github PK

View Code? Open in Web Editor NEW
223.0 4.0 47.0 1.47 MB

A set of React.js components to display an interactive SVG map

Home Page: https://victorcazanave.github.io/react-svg-map/

License: MIT License

JavaScript 99.00% SCSS 1.00%
react component svg interactive map checkbox radiobutton

react-svg-map's Introduction

react-svg-map

npm version Build Status codecov Dependency Status peerDependencies Status

A set of React.js components to display an interactive SVG map.

React SVG Map

Demo

Take a look at the live demo!

Installation

npm

npm install --save react-svg-map

yarn

yarn add react-svg-map

Usage

โš ๏ธ Breaking change from v1

This package does not include maps anymore!

You have to install the map you need from svg-maps or you can use your own map. See maps section for more details.

If you are still using the 1.x.x version, look at the v1 documentation.

๐ŸŒ Simple SVG Map

This is the base component to display an SVG map.

  • Import SVGMap component from react-svg-map
  • Import the map you want
  • Optionally, import react-svg-map/lib/index.css if you want to apply the default styles
import React from "react";
import ReactDOM from "react-dom";
import Taiwan from "@svg-maps/taiwan";
import { SVGMap } from "react-svg-map";
import "react-svg-map/lib/index.css";

class App extends React.Component {
  constructor(props) {
    super(props);
  }

  render() {
    return <SVGMap map={Taiwan} />;
  }
}

ReactDOM.render(<App />, document.getElementById("app"));

API

Prop Type Default Description
map Object required Describe SVG map to display. See maps section for more details.
className String 'svg-map' CSS class of <svg>.
role String 'none' ARIA role of <svg>.
locationClassName String|Function 'svg-map__location' CSS class of each <path>. The function parameters are the location object and the location index.
locationTabIndex String|Function '0' Tabindex each <path>. The function parameters are the location object and the location index.
locationRole String 'none' ARIA role of each <path>.
locationAriaLabel Function location.name ARIA label of each <path>. The function parameters are the location object and the location index.
onLocationMouseOver Function Invoked when the user puts the mouse over a location.
onLocationMouseOut Function Invoked when the user puts the mouse out of a location.
onLocationMouseMove Function Invoked when the user moves the mouse on a location.
onLocationClick Function Invoked when the user clicks on a location.
onLocationKeyDown Function Invoked when the user hits a keyboard key on a location.
onLocationFocus Function Invoked when the user focuses a location.
onLocationBlur Function Invoked when the user unfocuses a location.
isLocationSelected Function Executed to determine if a location is selected. This property is used to set the aria-checked HTML attribute.
childrenBefore Node "Slot" before all the locations (<path>).
childrenAfter Node "Slot" after all the locations (<path>).

โ˜‘๏ธ Checkbox SVG Map

This is an implementation of SVGMap that behaves like a group of checkboxes.
It is based on this WAI-ARIA example to support keyboard navigation and be accessible.

  • Import CheckboxSVGMap component from react-svg-map
  • Import the map you want
  • Optionally, import react-svg-map/lib/index.css if you want to apply the default styles
import React from "react";
import ReactDOM from "react-dom";
import Taiwan from "@svg-maps/taiwan";
import { CheckboxSVGMap } from "react-svg-map";
import "react-svg-map/lib/index.css";

class App extends React.Component {
  constructor(props) {
    super(props);
  }

  render() {
    return <CheckboxSVGMap map={Taiwan} />;
  }
}

ReactDOM.render(<App />, document.getElementById("app"));

API

Prop Type Default Description
map Object required Describe SVG map to display. See maps section for more details.
className String 'svg-map' CSS class of <svg>.
locationClassName String|Function 'svg-map__location' CSS class of each <path>. The function parameters are the location object and the location index.
locationAriaLabel Function location.name ARIA label of each <path>. The function parameters are the location object and the location index.
selectedLocationIds String[] List of ids of the initial selected locations. It is used only when the component is mounted and is not synchronized when updated.
onChange Function Invoked when the user selects/deselects a location. The list of selected locations is passed as parameter.
onLocationMouseOver Function Invoked when the user puts the mouse over a location.
onLocationMouseOut Function Invoked when the user puts the mouse out of a location.
onLocationMouseMove Function Invoked when the user moves the mouse on a location.
onLocationFocus Function Invoked when the user focuses a location.
onLocationBlur Function Invoked when the user unfocuses a location.
childrenBefore Node "Slot" before all the locations (<path>).
childrenAfter Node "Slot" after all the locations (<path>).

๐Ÿ”˜ Radio SVG Map

This is an implementation of SVGMap that behaves like a group of radio buttons.
It is based on this WAI-ARIA example to support keyboard navigation and be accessible.

  • Import RadioSVGMap component from react-svg-map
  • Import the map you want
  • Optionally, import react-svg-map/lib/index.css if you want to apply the default styles
import React from "react";
import ReactDOM from "react-dom";
import Taiwan from "@svg-maps/taiwan";
import { RadioSVGMap } from "react-svg-map";
import "react-svg-map/lib/index.css";

class App extends React.Component {
  constructor(props) {
    super(props);
  }

  render() {
    return <RadioSVGMap map={Taiwan} />;
  }
}

ReactDOM.render(<App />, document.getElementById("app"));

API

Prop Type Default Description
map Object required Describe SVG map to display. See maps section for more details.
className String 'svg-map' CSS class of <svg>.
locationClassName String|Function 'svg-map__location' CSS class of each <path>. The function parameters are the location object and the location index.
locationAriaLabel Function location.name ARIA label of each <path>. The function parameters are the location object and the location index.
selectedLocationId String id of the initial selected location. It is used only when the component is mounted and is not synchronized when updated.
onChange Function Invoked when the user selects a location. The selected location is passed as parameter.
onLocationMouseOver Function Invoked when the user puts the mouse over a location.
onLocationMouseOut Function Invoked when the user puts the mouse out of a location.
onLocationMouseMove Function Invoked when the user moves the mouse on a location.
onLocationFocus Function Invoked when the user focuses a location.
onLocationBlur Function Invoked when the user unfocuses a location.
childrenBefore Node "Slot" before all the locations (<path>).
childrenAfter Node "Slot" after all the locations (<path>).

Maps

Existing maps

Since v2.0.0 this package does not provide maps anymore. All the existing maps have been moved to the independant svg-maps project because they may be useful for other components/projects.

Custom maps

You can modify existing maps or create your own.

Modify a map

  1. Import the map to modify
  2. Create a new object from this map
  3. Pass this new object as map prop of <SVGMap /> component
import React from "react";
import Taiwan from "@svg-maps/taiwan";
import { SVGMap } from "react-svg-map";

class App extends React.Component {
  constructor(props) {
    super(props);

    // Create new map object
    this.customTaiwan = {
      ...Taiwan,
      label: "Custom map label",
      locations: Taiwan.locations.map(location => {
        // Modify each location
      })
    };
  }

  render() {
    return <SVGMap map={this.customTaiwan} />;
  }
}

It is recommended to not modify the SVG properties (viewBox, path), because it may break the map's display.

Create a map

If you create a new map (other country, city...), feel free to contribute to svg-maps project!

react-svg-map's People

Contributors

amonrarivers avatar dependabot[bot] avatar hopkira avatar ronicayu avatar victorcazanave 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  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-svg-map's Issues

Map Labels

Is there a way to add labels to the states?

Control path opacity

Hi, thanks for the useful tool!

I'm wondering if there's an handy way to control the opacity of a map region (so an SVG path).

I'm building an heatmap with an opacity scale based on data incoming from an API, so is not really possible to do that as in the Heatmap example.

Or rather, it's not possible without defining a CSS class for each possible opacity value, that's not something I really wanna do :)

Any suggestions?

Thanks a lot

how add terrain type

how i add terrain type, example:
image

the horizontal and vertical line, identify the terrain type

Styling the map

Hi,
I am trying to change the colors on the map, but whenever I set a custom locationClassName the clicked countries are surrounded with a large rectangle instead of the country borders.
How is it possible to style colours etc without changing the functionality of the map?

Disable click behaviour on Checkbox map

Hi,

Is it possible to disable all click behaviour for the Checkbox map?

I have a world map and I am currently highlighting certain countries using the selectedLocationIds but I do not want the click behaviour to fill / unfill the countries upon clicking.

Is there a better way to approach this other than using the Checkbox map?

Thanks

Custom style for each location

I need to apply a specific className to each location, depending on the location status. I'm using this because I have 3 possible status for each location. "Only morning", "Only afternoon" or "All day". This application is for a university project and using SVG to create a custom map is awesome because it's easier.

But locationClassName applies to all locations.

Is there any coming features to do this?

Map markers

Is there any way to add markers to the map?

Based on condition i want to select the state

I have the condition on the basis I want to select the state and show that state is selected how I can do that?

function handleclick(id, stateName) {
console.log("im clilck");
setStateCode(id);
setStateName(stateName);
setDisplayState(stateName);
}

Custom the SVG map color by edit the CSS

I am a bit confused to change the color. And then I try to find the CSS file that control the color. And here it is.

.svg-map {
  width: 100%;
  height: auto;
  stroke: #666;
  stroke-width: 1;
  stroke-linecap: round;
  stroke-linejoin: round; }
  .svg-map__location {
    fill: #a1d99b;
    cursor: pointer; }
    .svg-map__location:focus, .svg-map__location:hover {
      fill: #b8e2b3;
      outline: 0; }
    .svg-map__location[aria-checked=true] {
      fill: #f4bc44; }

I just want to say to everyone who need to modify the CSS, this is the default.

Instead of using default import "react-svg-map/lib/index.css"; You can save your custom CSS.

It will really helps if there is a way to custom the color easier.

selectedLocationIds not working on checkbox svg map

I'm trying to add initial value to map, but the type is missing in type script, and also react shows Invariant Violation error.
error:

Unhandled Runtime Error

Invariant Violation: Minified React error #188; visit http://facebook.github.io/react/docs/error-decoder.html?invariant=188 for the full message or use the non-minified dev environment for full errors and additional helpful warnings.

Call Stack
p
node_modules/react-svg-map/lib/index.js (9:297)
findDOMNode
node_modules/react-svg-map/lib/index.js (9:88932)
value
node_modules/react-svg-map/lib/index.js (1:5955)
commitLifeCycles
node_modules/react-dom/cjs/react-dom.development.js (20663:0)
commitLayoutEffects
node_modules/react-dom/cjs/react-dom.development.js (23426:0)

Roadmap v2

  • Use SVG files instead of JS files (if possible)
  • Provide CheckboxSVGMap and RadioSVGMap components
  • Handle keyboard navigation (a11y)
  • Add custom CSS class prop
  • Generate 1 CSS file per map
  • Add more maps (EU, Germany, Spain, Italy, Japan, Korea...)
  • Support Yarn
  • Externalize maps files?

Any thoughts or ideas?

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.