پرش به مطلب اصلی

💻 How to Render It

Every map in svg-world-maps is generated as a plain SVG string via the createMap() function. Because of this, the rendering pattern is identical across all frameworks:

  1. Generate the SVG string with createMap(mapId, options).
  2. Inject it into a container element (usually via innerHTML).
  3. Listen for events on the container to build interactivity.
Works everywhere

Since createMap() returns a pure string with no framework dependencies, you can render it in React, Vue, Angular, Svelte, vanilla JS, or even server-rendered environments.


🧩 The Core Concept

Every <path> element in the generated SVG automatically receives two data attributes you can use for interactivity:

AttributeExampleDescription
data-code"USCA"The unique ISO-style region code
data-name"California"The human-readable region name

You listen for clicks on the parent container and read these attributes from the event target. This one pattern powers drill-downs, filtering, and detail views in every framework below.


⚙️ Rendering by Framework

Use a ref for the container and dangerouslySetInnerHTML to inject the SVG. Attach the click listener inside useEffect.

import { createMap } from "svg-world-maps";
import { useRef, useEffect } from "react";

export default function WorldMap() {
const containerRef = useRef(null);

// Generate the SVG string
const svg = createMap("usa", {
background: "transparent",
borders: "#1e293b",
hoverColor: "rgba(179, 25, 46, 0.35)",
size: "xl",
});

useEffect(() => {
const el = containerRef.current;
const onClick = (e) => {
const { code, name } = e.target.dataset;
if (code && name) console.log(`Clicked: ${name} (${code})`);
};

el.addEventListener("click", onClick);
return () => el.removeEventListener("click", onClick);
}, []);

return (
<div
ref={containerRef}
style={{ background: "#0f172a", padding: "2rem", borderRadius: "1rem" }}
dangerouslySetInnerHTML={{ __html: svg }}
/>
);
}

🎛️ Map Options Reference

The second argument to createMap() controls the visual theme. These options are identical in every framework.

createMap("usa", {
background: "transparent", // Container background
borders: "#1e293b", // Region border color
hoverColor: "rgba(0, 153, 51, 0.35)", // Fill color on hover
showTooltip: true, // Enable built-in tooltips
size: "xl", // Render size preset
});
OptionTypeDefaultDescription
backgroundstring"transparent"Background behind the SVG
bordersstring"#1e293b"Color of region border strokes
hoverColorstringnullTranslucent fill applied on hover
showTooltipbooleantrueShow region name tooltip on hover
sizestring"md"Size preset (sm, md, lg, xl)

🗂️ Registering Custom Map Data

If you are shipping a map that isn't bundled by default (like the optional country packs), register it once before calling createMap(). This step is framework-agnostic.

import { registerMapData, createMap } from "svg-world-maps";
import argentinaData from "svg-world-maps/maps/ARGENTINA";

// Register once (e.g., in your app bootstrap)
registerMapData("argentina", argentinaData);

// Now you can create it anywhere
const svg = createMap("argentina", { hoverColor: "rgba(117, 186, 222, 0.35)" });
Server-Side Rendering

Because createMap() is a pure string generator with no window or document access, it is safe to call during SSR (Next.js, Nuxt, SvelteKit, Angular Universal). Only the click-listener setup needs to run on the client.