1.2.0 • Published 4 months ago

@vishalvoid/react-india-map v1.2.0

Weekly downloads
-
License
MIT
Repository
github
Last release
4 months ago

React India Map Component

An interactive SVG India map component for React applications with TypeScript support. This component allows you to create clickable and hoverable state regions with custom data and styling.

1.00

Installation

npm install @vishalvoid/react-india-map

Usage

import { IndiaMap } from "@vishalvoid/react-india-map";
import type { StateData } from "@vishalvoid/react-india-map";

const App = () => {
  const mapStyle = {
    backgroundColor: "#ffffff",
    hoverColor: "#dddddd",
    tooltipConfig: {
      backgroundColor: "rgba(0, 0, 0, 0.8)",
      textColor: "#ffffff",
    },
  };

  const stateData: StateData[] = [
    {
      id: "IN-MH",
      customData: {
        population: "123.2M",
        capital: "Mumbai",
      },
    },
  ];

  const handleStateHover = (stateId: string, stateInfo?: StateData) => {
    console.log(`Hovering over ${stateId}`, stateInfo);
  };

  const handleStateClick = (stateId: string, stateInfo?: StateData) => {
    console.log(`Clicked on ${stateId}`, stateInfo);
  };

  return (
    <IndiaMap
      mapStyle={mapStyle}
      stateData={stateData}
      onStateHover={handleStateHover}
      onStateClick={handleStateClick}
    />
  );
};

Props

interface TooltipConfig {
  backgroundColor?: string; // Default: "rgba(0, 0, 0, 0.8)"
  textColor?: string; // Default: "#ffffff"
}

interface MapStyle {
  backgroundColor?: string; // Default: "#ffffff"
  hoverColor?: string; // Default: "#dddddd"
  tooltipConfig?: TooltipConfig;
  stroke?: string; // Default: "#000000"
  strokeWidth?: number; // Default: 1
}

interface StateData {
  id: string; // State ID (e.g., "IN-MH")
  customData?: {
    [key: string]: any; // Custom state data
  };
}

interface IndiaMapProps {
  mapStyle?: MapStyle;
  stateData?: StateData[];
  onStateHover?: (stateId: string, stateInfo?: StateData) => void;
  onStateClick?: (stateId: string, stateInfo?: StateData) => void;
}

State IDs Reference

State/UTIDCapital
Andhra PradeshIN-APAmaravati
Arunachal PradeshIN-ARItanagar
AssamIN-ASDispur
BiharIN-BRPatna
ChhattisgarhIN-CTRaipur
GoaIN-GAPanaji
GujaratIN-GJGandhinagar
HaryanaIN-HRChandigarh
Himachal PradeshIN-HPShimla
JharkhandIN-JHRanchi
KarnatakaIN-KABengaluru
KeralaIN-KLThiruvananthapuram
Madhya PradeshIN-MPBhopal
MaharashtraIN-MHMumbai
ManipurIN-MNImphal
MeghalayaIN-MLShillong
MizoramIN-MZAizawl
NagalandIN-NLKohima
OdishaIN-ORBhubaneswar
PunjabIN-PBChandigarh
RajasthanIN-RJJaipur
SikkimIN-SKGangtok
Tamil NaduIN-TNChennai
TelanganaIN-TGHyderabad
TripuraIN-TRAgartala
Uttar PradeshIN-UPLucknow
UttarakhandIN-UTDehradun
West BengalIN-WBKolkata

Union Territories

Union TerritoryIDCapital
Andaman and NicobarIN-ANPort Blair
ChandigarhIN-CHChandigarh
Dadra and Nagar Haveli and Daman and DiuIN-DNDaman
DelhiIN-DLNew Delhi
Jammu and KashmirIN-JKSrinagar/Jammu
LadakhIN-LALeh
LakshadweepIN-LDKavaratti
PuducherryIN-PYPuducherry

Styling

You can customize the map appearance using CSS:

.india-map-container {
  width: 100%;
  max-width: 800px;
  margin: 0 auto;
}

.india-map-container path {
  transition: fill 0.3s ease;
  stroke: #ffffff;
  stroke-width: 1;
}

.india-map-container path:hover {
  cursor: pointer;
  opacity: 0.8;
}

.state-tooltip {
  transition: opacity 0.2s;
}

Examples

Basic Usage with Hover Effect

import React, { useState } from "react";
import { IndiaMap } from "@vishalvoid/react-india-map";

const App = () => {
  const [activeState, setActiveState] = useState("");

  return (
    <IndiaMap
      mapStyle={{
        backgroundColor: "#f0f0f0",
        hoverColor: "#b3e0ff",
      }}
      onStateHover={(stateId) => setActiveState(stateId)}
    />
  );
};

With Custom State Data and Click Handler

import React from "react";
import { IndiaMap } from "@vishalvoid/react-india-map";
import type { StateData } from "@vishalvoid/react-india-map";

const App = () => {
  const stateData: StateData[] = [
    {
      id: "IN-MH",
      customData: {
        population: "123.2M",
        capital: "Mumbai",
        gdp: "$400B",
      },
    },
    {
      id: "IN-DL",
      customData: {
        population: "20.5M",
        capital: "New Delhi",
        gdp: "$110B",
      },
    },
  ];

  const handleStateClick = (stateId: string, data?: StateData) => {
    console.log(`Clicked ${stateId}:`, data?.customData);
    // You can show modal, navigate to state page, etc.
  };

  return (
    <IndiaMap
      stateData={stateData}
      onStateClick={handleStateClick}
      mapStyle={{
        backgroundColor: "#f8f9fa",
        hoverColor: "#acd5f2",
        stroke: "#ffffff",
        strokeWidth: 1,
        tooltipConfig: {
          backgroundColor: "rgba(0, 0, 0, 0.8)",
          textColor: "#ffffff",
        },
      }}
    />
  );
};

Publishing Updates

To publish a new version to NPM:

# Update version in package.json
npm version patch # or minor or major

# Build the package
npm run build

# Publish to NPM
npm publish

Development

# Install dependencies
npm install

# Run development server
npm run dev

# Build for production
npm run build

License

MIT License

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

1.2.0

4 months ago

1.1.9

4 months ago

1.1.8

4 months ago

1.1.7

4 months ago

1.1.6

5 months ago

1.1.5

5 months ago

1.1.4

5 months ago

1.0.0

5 months ago