├── .eslintignore
├── .eslintrc
├── .gitignore
├── .prettierrc.json
├── LICENSE
├── README.md
├── jestconfig.json
├── package-lock.json
├── package.json
├── src
├── components
│ └── mapSvg
│ │ ├── IranMap.tsx
│ │ ├── IranMapWrapper.tsx
│ │ └── iran-map.css
├── data
│ └── provinces.ts
├── index.ts
└── interfaces.ts
├── styleMock.tsx
├── tests
└── common.test.tsx
└── tsconfig.json
/.eslintignore:
--------------------------------------------------------------------------------
1 | node_modules
2 | dist
--------------------------------------------------------------------------------
/.eslintrc:
--------------------------------------------------------------------------------
1 | {
2 | "root": true,
3 | "extends": [
4 | "prettier",
5 | "plugin:prettier/recommended",
6 | "eslint:recommended",
7 | "plugin:react/recommended",
8 | "plugin:@typescript-eslint/eslint-recommended",
9 | "plugin:@typescript-eslint/recommended"
10 | ],
11 | "parser": "@typescript-eslint/parser",
12 | "plugins": ["@typescript-eslint", "prettier", "react", "react-hooks"],
13 | "rules": {
14 | "react-hooks/rules-of-hooks": "error",
15 | "react-hooks/exhaustive-deps": "warn",
16 | "@typescript-eslint/no-non-null-assertion": "off",
17 | "@typescript-eslint/ban-ts-comment": "off",
18 | "@typescript-eslint/no-explicit-any": "off"
19 | },
20 | "settings": {
21 | "react": {
22 | "version": "detect"
23 | }
24 | },
25 | "env": {
26 | "browser": true,
27 | "node": true
28 | },
29 | "globals": {
30 | "JSX": true
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | /node_modules
2 | dist
3 | yarn-error.log
4 | npm-debug.log
5 | .parcel-cache
--------------------------------------------------------------------------------
/.prettierrc.json:
--------------------------------------------------------------------------------
1 | {
2 | "bracketSpacing": true,
3 | "singleQuote": true,
4 | "trailingComma": "all",
5 | "tabWidth": 2,
6 | "semi": false,
7 | "printWidth": 120,
8 | "jsxSingleQuote": true,
9 | "endOfLine": "auto"
10 | }
11 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2023 Sima Mojtahedi
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 |
2 | # Iran Map
3 |
4 | 
5 | 
6 | 
7 | 
8 |
9 | This is an interactive and responsive SVG map of Iran which is a powerful tool for visualizing data related to Iran's provinces. It allows users to customize the colors of each province based on data values and additionally, users can click on each province to change it's color and retrieve it's name using a simple function.
10 |
11 | This package is perfect for anyone looking to create data visualizations related to Iran or for anyone who needs an interactive map of Iran's provinces.
12 |
13 | ## Demo
14 |
15 | [**Live Demo**](https://simamojtahedi.github.io/react-iran-map/)
16 |
17 |
18 |
19 | 
20 |
21 |
22 |
23 | ## Installation
24 |
25 | `npm i react-iran-map`
26 |
27 | ## Usage
28 | ```jsx
29 | import { IranMap } from 'react-iran-map'
30 | import mapData from '../data.tsx'
31 |
32 | function App() {
33 | const selectProvinceHandler = (province) => {
34 | console.log(province)
35 | }
36 |
37 | return (
38 |
39 |
50 |
51 | )
52 | }
53 |
54 | export default App
55 | ```
56 |
57 |
58 | ## Data schema
59 |
60 | ```jsx
61 | const mapData = {
62 | ardabil: 0,
63 | isfahan: 20,
64 | alborz: 11,
65 | ilam: 18,
66 | eastAzerbaijan: 10,
67 | westAzerbaijan: 20,
68 | bushehr: 15,
69 | tehran: 3,
70 | chaharmahalandBakhtiari: 25,
71 | southKhorasan: 29,
72 | razaviKhorasan: 11,
73 | northKhorasan: 19,
74 | khuzestan: 12,
75 | zanjan: 18,
76 | semnan: 9,
77 | sistanAndBaluchestan: 3,
78 | fars: 7,
79 | qazvin: 35,
80 | qom: 30,
81 | kurdistan: 24,
82 | kerman: 23,
83 | kohgiluyehAndBoyerAhmad: 2,
84 | kermanshah: 7,
85 | golestan: 18,
86 | gilan: 14,
87 | lorestan: 7,
88 | mazandaran: 28,
89 | markazi: 25,
90 | hormozgan: 14,
91 | hamadan: 19,
92 | yazd: 32,
93 | }
94 | ```
95 | ## Available attributes
96 |
97 |
98 | | Name | Type | Required | Description |
99 | | :------------------------ | :--------- | :------- | :------------------------------------------------------------------------------------------------------- |
100 | | `data` | `object` | True | An object that has 'Data schema' items as provinces |
101 | | `colorRange` | `string` | True | It defines the range of color based on it's value and it should be rgb color like this : `"30, 70, 181"` |
102 | | `width` | `number` | False | Width of the map |
103 | | `textColor` | `string` | False | Color of map texts |
104 | | `defaultSelectedProvince` | `string` | False | You can set one of the provinces from 'Data schema' as default selected province |
105 | | `deactiveProvinceColor` | `string` | False | It set color of the provinces with `0` value |
106 | | `selectedProvinceColor` | `string` | False | Color of the clicked province |
107 | | `tooltipTitle` | `string` | False | You can add a name for the value in tooltip |
108 | | `selectProvinceHandler` | `function` | False | You can define a function like example above to get selected province name |
109 | ## Authors
110 |
111 | - [@simamojtahedi](https://github.com/simamojtahedi)
112 |
--------------------------------------------------------------------------------
/jestconfig.json:
--------------------------------------------------------------------------------
1 | {
2 | "moduleNameMapper": {
3 | "\\.(css)$": "/styleMock.tsx"
4 | },
5 | "transform": {
6 | "^.+\\.(t|j)sx?$": "ts-jest"
7 | },
8 | "testRegex": "(/tests/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$",
9 | "moduleFileExtensions": ["ts", "tsx", "js", "jsx", "json", "node"],
10 | "testEnvironment": "jsdom"
11 | }
12 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "react-iran-map",
3 | "version": "0.1.3",
4 | "description": "An interactive and responsive SVG map of Iran",
5 | "main": "./dist/cjs/index.js",
6 | "module": "./dist/esm/index.js",
7 | "types": "./dist/esm/index.d.ts",
8 | "repository": {
9 | "type": "git",
10 | "url": "git+https://github.com/simamojtahedi/react-iran-map.git"
11 | },
12 | "scripts": {
13 | "build": "npm run build:esm && npm run build:cjs",
14 | "build:esm": "tsc",
15 | "build:cjs": "tsc --module commonjs --outDir dist/cjs",
16 | "lint": "eslint \"{**/*,*}.{js,ts,jsx,tsx}\"",
17 | "prettier": "prettier --write \"{src,tests,example/src}/**/*.{js,ts,jsx,tsx}\"",
18 | "test": "jest --config jestconfig.json",
19 | "prepare": "npm run build",
20 | "prepublishOnly": "npm test && npm run prettier && npm run lint"
21 | },
22 | "files": [
23 | "dist",
24 | "LICENSE",
25 | "README.md"
26 | ],
27 | "keywords": [
28 | "map",
29 | "iran",
30 | "iran-map",
31 | "iran-svg-map",
32 | "react-iran-map"
33 | ],
34 | "author": "Sima Mojtahedi",
35 | "license": "MIT",
36 | "peerDependencies": {
37 | "react": ">=16"
38 | },
39 | "dependencies": {
40 | "@testing-library/react": "^14.1.2",
41 | "@types/jest": "^29.5.11",
42 | "@typescript-eslint/eslint-plugin": "^6.13.2",
43 | "@typescript-eslint/parser": "^6.13.2",
44 | "eslint": "^8.55.0",
45 | "eslint-config-prettier": "^9.1.0",
46 | "eslint-plugin-prettier": "^5.0.1",
47 | "eslint-plugin-react": "^7.33.2",
48 | "eslint-plugin-react-hooks": "^4.6.0",
49 | "jest": "^29.7.0",
50 | "jest-canvas-mock": "^2.5.2",
51 | "jest-environment-jsdom": "^29.7.0",
52 | "prettier": "^3.1.0",
53 | "react-tooltip": "^5.25.0",
54 | "ts-jest": "^29.1.1",
55 | "typescript": "^5.3.3"
56 | }
57 | }
58 |
--------------------------------------------------------------------------------
/src/components/mapSvg/IranMap.tsx:
--------------------------------------------------------------------------------
1 | import React, { useEffect, useRef, useState } from 'react'
2 | import IranMapWrapper from './IranMapWrapper'
3 | import { provinces } from '../../data/provinces'
4 | import { IranMapWrapperProps, mapDataType, provinceType, selectedProvinceType } from '../../interfaces'
5 | import './iran-map.css'
6 |
7 | const IranMap: React.FC = ({
8 | data,
9 | width,
10 | colorRange,
11 | defaultSelectedProvince,
12 | textColor = '#000',
13 | deactiveProvinceColor = '#e6e6e6',
14 | selectedProvinceColor,
15 | tooltipTitle = '',
16 | selectProvinceHandler,
17 | }) => {
18 | const mapRef = useRef(null)
19 | const [provinceName, setProvinceName] = useState(null)
20 | const [selectedProvince, setSelectedProvince] = useState({
21 | name: defaultSelectedProvince ? defaultSelectedProvince : '',
22 | faName: defaultSelectedProvince
23 | ? provinces.find((province: provinceType) => province.provinceName === defaultSelectedProvince)?.provinceFaName
24 | : '',
25 | })
26 |
27 | const pathMouseOverHandler = (event: any) => {
28 | const path = event.target
29 | setProvinceName(path.dataset.name)
30 | }
31 |
32 | const pathClickedHandle = (pathName: string) => {
33 | setSelectedProvince({
34 | faName: pathName,
35 | name: provinces.find((province: provinceType) => province.provinceFaName === pathName)?.provinceName,
36 | })
37 |
38 | selectProvinceHandler &&
39 | selectProvinceHandler({
40 | faName: pathName,
41 | name: provinces.find((province: provinceType) => province.provinceFaName === pathName)?.provinceName,
42 | })
43 | }
44 |
45 | const setPathBackgrounds = (svg: Element, mapData: mapDataType) => {
46 | const polygons = svg.querySelectorAll('polygon')
47 | const paths = svg.querySelectorAll('path')
48 |
49 | const values = Object.values(mapData)
50 | //@ts-ignore
51 | const min = Math.min(...values)
52 | const max = Math.max(...values)
53 |
54 | const setColorHandler = (element: SVGPathElement) => {
55 | const title = provinces.find((item: provinceType) => item.provinceFaName === element.getAttribute('data-name'))
56 | ?.provinceName
57 |
58 | const selectedItem = provinces.find(
59 | (province: provinceType) => province.provinceFaName === selectedProvince.faName,
60 | )?.provinceFaName
61 |
62 | if (title) {
63 | const count = mapData[title.trim()]
64 | if (count === 0) {
65 | element.style.fill = deactiveProvinceColor
66 | } else {
67 | if (min !== max) {
68 | const alpha = Math.max(0.1, Math.min(1, (count - min) / (max - min)))
69 | const usageColor = `rgba(${colorRange}, ${alpha})`
70 | element.style.fill = usageColor
71 | } else {
72 | const usageColor = `rgba(${colorRange}, ${min > 0 ? 1 : 0.1})`
73 | element.style.fill = usageColor
74 | }
75 | }
76 | }
77 | if (element.getAttribute('data-name') === selectedItem && selectedProvinceColor) {
78 | element.style.fill = selectedProvinceColor
79 | }
80 | }
81 |
82 | paths.forEach((path: SVGPathElement) => {
83 | setColorHandler(path)
84 | })
85 |
86 | polygons.forEach((polygon: SVGPathElement) => {
87 | setColorHandler(polygon)
88 | })
89 | }
90 |
91 | useEffect(() => {
92 | if (mapRef.current) {
93 | setPathBackgrounds(mapRef.current, data)
94 | }
95 | }, [mapRef, selectedProvince])
96 |
97 | return (
98 |
99 |
109 |
110 | )
111 | }
112 |
113 | export default IranMap
114 |
--------------------------------------------------------------------------------
/src/components/mapSvg/IranMapWrapper.tsx:
--------------------------------------------------------------------------------
1 | import React from 'react'
2 | import { Tooltip } from 'react-tooltip'
3 | import { provinces } from '../../data/provinces'
4 | import { MapProps } from '../../interfaces'
5 |
6 | const IranMapWrapper: React.FC = ({
7 | textColor,
8 | mapRef,
9 | provinceName,
10 | pathClickedHandle,
11 | pathMouseOverHandler,
12 | data,
13 | width,
14 | tooltipTitle,
15 | }) => {
16 | const badge = `
17 |
18 |
${provinceName}
19 |
20 | ${
21 | provinceName &&
22 | data[
23 | //@ts-ignore
24 | provinces.find((item: any) => item.provinceFaName === provinceName)?.provinceName
25 | ]
26 | }
27 | ${tooltipTitle && `${tooltipTitle}`}
28 |
29 |
30 | `
31 |
32 | return (
33 | <>
34 |
1822 |
1823 | >
1824 | )
1825 | }
1826 |
1827 | export default IranMapWrapper
1828 |
--------------------------------------------------------------------------------
/src/components/mapSvg/iran-map.css:
--------------------------------------------------------------------------------
1 | .iran-map-wrapper {
2 | overflow: hidden;
3 | display: flex;
4 | align-items: center;
5 | justify-content: center;
6 | margin: auto;
7 | position: relative;
8 | direction: ltr !important;
9 | }
10 |
11 | .iran-map-wrapper path,
12 | .iran-map-wrapper polygon {
13 | outline: none !important;
14 | stroke: transparent;
15 | cursor: pointer;
16 | transition: all .3s;
17 | }
18 |
19 | .iran-map-wrapper .react-map-water {
20 | fill: #7bdaff !important;
21 | }
22 |
23 | .react-map-badge {
24 | background-color: #fff;
25 | box-shadow: 0 0 5px rgba(0, 0, 0, 0.1);
26 | border-radius: 4px;
27 | font-family: inherit;
28 | overflow: hidden;
29 | min-width: 95px;
30 | text-align: right;
31 | }
32 |
33 | .react-map-badge h2 {
34 | margin: 0;
35 | padding: 7px;
36 | font-size: 12px;
37 | background-color: #e6e6e6;
38 | }
39 |
40 | .react-map-badge h3 {
41 | font-size: 13px;
42 | margin: 0;
43 | padding: 7px 10px;
44 | display: flex;
45 | justify-content: space-between;
46 | text-align: right;
47 | }
48 |
49 | .react-map-badge h3 span {
50 | flex: 1;
51 | direction: rtl;
52 | }
53 |
54 | .iran-map-wrapper g,
55 | .iran-map-wrapper text {
56 | pointer-events: none !important;
57 | font-family: inherit;
58 | font-weight: 700;
59 | }
60 |
--------------------------------------------------------------------------------
/src/data/provinces.ts:
--------------------------------------------------------------------------------
1 | export const provinces = [
2 | {
3 | provinceName: 'ardabil',
4 | provinceFaName: 'اردبیل',
5 | },
6 | {
7 | provinceName: 'isfahan',
8 | provinceFaName: 'اصفهان',
9 | },
10 | {
11 | provinceName: 'alborz',
12 | provinceFaName: 'البرز',
13 | },
14 | {
15 | provinceName: 'ilam',
16 | provinceFaName: 'ایلام',
17 | },
18 | {
19 | provinceName: 'eastAzerbaijan',
20 | provinceFaName: 'آذربایجان شرقی',
21 | },
22 | {
23 | provinceName: 'westAzerbaijan',
24 | provinceFaName: 'آذربایجان غربی',
25 | },
26 | {
27 | provinceName: 'bushehr',
28 | provinceFaName: 'بوشهر',
29 | },
30 | {
31 | provinceName: 'tehran',
32 | provinceFaName: 'تهران',
33 | },
34 | {
35 | provinceName: 'chaharmahalandBakhtiari',
36 | provinceFaName: 'چهارمحال و بختیاری',
37 | },
38 | {
39 | provinceName: 'southKhorasan',
40 | provinceFaName: 'خراسان جنوبی',
41 | },
42 | {
43 | provinceName: 'razaviKhorasan',
44 | provinceFaName: 'خراسان رضوی',
45 | },
46 | {
47 | provinceName: 'northKhorasan',
48 | provinceFaName: 'خراسان شمالی',
49 | },
50 | {
51 | provinceName: 'khuzestan',
52 | provinceFaName: 'خوزستان',
53 | },
54 | {
55 | provinceName: 'zanjan',
56 | provinceFaName: 'زنجان',
57 | },
58 | {
59 | provinceName: 'semnan',
60 | provinceFaName: 'سمنان',
61 | },
62 | {
63 | provinceName: 'sistanAndBaluchestan',
64 | provinceFaName: 'سیستان و بلوچستان',
65 | },
66 | {
67 | provinceName: 'fars',
68 | provinceFaName: 'فارس',
69 | },
70 | {
71 | provinceName: 'qazvin',
72 | provinceFaName: 'قزوین',
73 | },
74 | {
75 | provinceName: 'qom',
76 | provinceFaName: 'قم',
77 | },
78 | {
79 | provinceName: 'kurdistan',
80 | provinceFaName: 'کردستان',
81 | },
82 | {
83 | provinceName: 'kerman',
84 | provinceFaName: 'کرمان',
85 | },
86 | {
87 | provinceName: 'kohgiluyehAndBoyerAhmad',
88 | provinceFaName: 'کهگیلویه و بویراحمد',
89 | },
90 | {
91 | provinceName: 'kermanshah',
92 | provinceFaName: 'کرمانشاه',
93 | },
94 | {
95 | provinceName: 'golestan',
96 | provinceFaName: 'گلستان',
97 | },
98 | {
99 | provinceName: 'gilan',
100 | provinceFaName: 'گیلان',
101 | },
102 | {
103 | provinceName: 'lorestan',
104 | provinceFaName: 'لرستان',
105 | },
106 | {
107 | provinceName: 'mazandaran',
108 | provinceFaName: 'مازندران',
109 | },
110 | {
111 | provinceName: 'markazi',
112 | provinceFaName: 'مرکزی',
113 | },
114 | {
115 | provinceName: 'hormozgan',
116 | provinceFaName: 'هرمزگان',
117 | },
118 | {
119 | provinceName: 'hamadan',
120 | provinceFaName: 'همدان',
121 | },
122 | {
123 | provinceName: 'yazd',
124 | provinceFaName: 'یزد',
125 | },
126 | ]
127 |
--------------------------------------------------------------------------------
/src/index.ts:
--------------------------------------------------------------------------------
1 | import IranMap from './components/mapSvg/IranMap'
2 |
3 | export { IranMap }
4 |
--------------------------------------------------------------------------------
/src/interfaces.ts:
--------------------------------------------------------------------------------
1 | /* eslint-disable @typescript-eslint/ban-types */
2 | export interface selectedProvinceType {
3 | name: string | undefined
4 | faName: string | undefined
5 | }
6 |
7 | export interface provinceType {
8 | provinceName: string
9 | provinceFaName: string
10 | }
11 |
12 | export interface mapDataType {
13 | [key: string]: number
14 | }
15 |
16 | export interface IranMapWrapperProps {
17 | data: {}
18 | width?: number | string
19 | colorRange: string
20 | textColor?: string
21 | defaultSelectedProvince?: string
22 | selectedProvinceColor?: string
23 | tooltipTitle?: string
24 | selectProvinceHandler?: Function
25 | deactiveProvinceColor?: string
26 | }
27 |
28 | export interface MapProps {
29 | textColor: string
30 | mapRef: React.RefObject
31 | provinceName: null | string
32 | pathClickedHandle: Function
33 | pathMouseOverHandler: Function
34 | data: {}
35 | tooltipTitle: undefined | string
36 | width: number | string | undefined
37 | }
38 |
--------------------------------------------------------------------------------
/styleMock.tsx:
--------------------------------------------------------------------------------
1 | module.exports = {}
2 |
--------------------------------------------------------------------------------
/tests/common.test.tsx:
--------------------------------------------------------------------------------
1 | import * as React from 'react'
2 | import { render } from '@testing-library/react'
3 | import 'jest-canvas-mock'
4 | import IranMap from '../src/components/mapSvg/IranMap'
5 |
6 | const testData = {
7 | ardabil: 0,
8 | isfahan: 20,
9 | alborz: 11,
10 | ilam: 18,
11 | eastAzerbaijan: 10,
12 | westAzerbaijan: 20,
13 | bushehr: 15,
14 | tehran: 3,
15 | chaharmahalandBakhtiari: 25,
16 | southKhorasan: 29,
17 | razaviKhorasan: 11,
18 | northKhorasan: 19,
19 | khuzestan: 12,
20 | zanjan: 18,
21 | semnan: 9,
22 | sistanAndBaluchestan: 3,
23 | fars: 7,
24 | qazvin: 35,
25 | qom: 30,
26 | kurdistan: 24,
27 | kerman: 23,
28 | kohgiluyehAndBoyerAhmad: 2,
29 | kermanshah: 7,
30 | golestan: 18,
31 | gilan: 14,
32 | lorestan: 7,
33 | mazandaran: 28,
34 | markazi: 25,
35 | hormozgan: 14,
36 | hamadan: 19,
37 | yazd: 32,
38 | }
39 |
40 | describe('Common render', () => {
41 | it('renders without crashing', () => {
42 | render()
43 | })
44 | })
45 |
--------------------------------------------------------------------------------
/tsconfig.json:
--------------------------------------------------------------------------------
1 | {
2 | "include": ["src"],
3 | "exclude": ["dist", "node_modules"],
4 | "compilerOptions": {
5 | "module": "esnext",
6 | "lib": ["dom", "esnext"],
7 | "importHelpers": true,
8 | "declaration": true,
9 | "sourceMap": true,
10 | "rootDir": "./src",
11 | "outDir": "./dist/esm",
12 | "strict": true,
13 | "noImplicitReturns": true,
14 | "noFallthroughCasesInSwitch": true,
15 | "noUnusedLocals": true,
16 | "noUnusedParameters": true,
17 | "moduleResolution": "node",
18 | "jsx": "react",
19 | "esModuleInterop": true,
20 | "skipLibCheck": true,
21 | "forceConsistentCasingInFileNames": true
22 | }
23 | }
24 |
--------------------------------------------------------------------------------