Saltar al contenido principal

API reference

All components are client components that also render on the server (pass topology explicitly for SSR). Data values are joined by official zero-padded codes: regions "01"-"16", provincias 3-digit, comunas 5-digit CUT.

Shared props (BaseMapProps<TFeature>)

Accepted by ChileMap, RegionComunasMap, and RegionProvinciasMap. TFeature is the level-specific feature type passed to callbacks.

PropTypeDefaultDescription
dataRecord<string, number>noneValues keyed by official code. Missing codes render with fillDefault; non-finite values are treated as missing. Every finite value participates in the scale domain.
colorsstring[]["#dbeafe", "#1e40af"]Color ramp (2+ colors), sampled into one color per bin. A diverging scale is expressed by a three-stop ramp.
binsnumber5Bin count for linear/quantile scales.
scaleType"linear" | "quantile" | "threshold""linear"Bin strategy. linear = equal-width, quantile = equal-count, threshold = explicit boundaries.
thresholdsnumber[]noneAscending inner boundaries; required when scaleType is "threshold", ignored otherwise.
colorFor(feature: TFeature) => string | undefinednonePer-feature override. Return undefined to fall back to the automatic scale.
featureLabel(feature: TFeature) => stringfeature.namePresentation label for the default tooltip and each path's aria-label. Callback features keep the canonical BCN name.
fillDefaultstring"#e2e8f0"Fill for units without a value.
strokestring"#ffffff"Border color between units.
strokeWidthnumber0.5Border width.
selectedstring | nullnullControlled selected code, highlighted per highlightMode and exposed as aria-pressed.
highlightStrokestring"#0f172a"Hover/selection border color.
highlightFillstring"rgba(15, 23, 42, 0.18)"Hover/selection overlay fill.
highlightMode"stroke" | "fill" | "both""stroke"Whether highlights use a border, a fill overlay, or both.
oceanicIslandsbooleantrueRender oceanic islands (west of longitude -76.5) in side insets. false draws mainland geometry only; fully oceanic units keep their data but render no shape.
onClick(feature: TFeature) => voidnoneClick/Enter/Space activation. Providing it makes features focusable buttons.
onHover(feature: TFeature | null) => voidnoneHover and keyboard-focus callback; null when the pointer/focus leaves the map.
tooltip((feature: TFeature) => ReactNode) | falsebuilt-inCustom tooltip render prop, or false to disable. The default shows the label and formatted value.
formatValue(value: number) => stringtoLocaleStringFormats the value in the default tooltip.
width, heightnumber340, 800viewBox size; the SVG scales responsively up to width.
classNamestringnoneClass on the wrapping <div>.
ariaLabelstringper componentaria-label of the <svg>.

Keyboard interaction (when onClick is set)

An interactive SVG is exposed as a labeled group. Each unit is a role="button" path with a roving tab index (one Tab stop per map). Arrow keys move between units, Home/End jump to the first/last, Enter/Space activate, and focus shows the tooltip at the unit's centroid. aria-pressed reflects selected. Without onClick, the SVG is one labeled img and its internal paths are hidden from the accessibility tree.

<ChileMap> (ChileMapProps)

PropTypeDescription
topologyTopologyRequired. National regions TopoJSON. Import react-chile-map/data/regiones.json. Kept explicit so applications control when the national geometry loads.

Plus every shared prop with TFeature = RegionFeature.

<RegionComunasMap> / <RegionProvinciasMap>

Shared props plus (BaseRegionalMapProps<TFeature>):

PropTypeDefaultDescription
regionRegionCoderequiredRegion whose subdivisions render; drives the bundled lazy import.
topologyTopologynoneOverride that bypasses the lazy loader (SSR, tests, preloading, custom cartography).
loadingFallbackReactNodebuilt-inShown while the regional chunk loads.
errorFallbackReactNode | (error) => ReactNodebuilt-inShown when loading or parsing fails.
onLoadError(error: Error) => voidnoneLoading/parsing error callback.
outlineStrokestring"#64748b"Outer region boundary color.
outlineStrokeWidthnumberstrokeWidth * 2Outer boundary width.

Successful topology imports are cached per region; stale in-flight results are ignored after region changes. Callback features: ComunaFeature adds region, province, provinceName; ProvinciaFeature adds region.

Optional regional data-key types

The public data prop remains Record<string, number> so dynamically loaded and custom topologies stay supported. Applications that use the bundled cartography can opt into exact key checking with the generated types:

import type { ComunaData, ProvinciaData } from "react-chile-map";

const comunaData = {
"13101": 50,
"13113": 82,
} satisfies ComunaData<"13">;

const provinciaData = {
"131": 100,
"132": 78,
} satisfies ProvinciaData<"13">;

<RegionComunasMap region="13" data={comunaData} />;
<RegionProvinciasMap region="13" data={provinciaData} />;

ComunaCode<R> and ProvinciaCode<R> expose the corresponding code unions directly. These four types cover the bundled codes for all 16 regions and are generated from the committed TopoJSON files.

SSR note: the bundled lazy loaders use dynamic JSON imports, which bundlers (Vite, webpack, Next) handle but raw Node ESM does not. On the server, pass topology explicitly.

React support: React 17, 18, and 19. With React 17, use a bundler or the CJS build; React 17 lacks the exports map raw Node ESM needs to resolve react/jsx-runtime.

Scales and <Legend>

createScale(options: CreateScaleOptions): MapScale
scaleFromData(data, options): MapScale // domain = every finite value in data

MapScale exposes type, palette (one color per bin), thresholds (inner boundaries), fillDefault, and colorOf(value | undefined). The map components build their fills through scaleFromData with their own props, so a <Legend> created from the same inputs cannot diverge:

const scale = scaleFromData(data, { colors, bins: 6, type: "quantile" });
<ChileMap topology={regiones} data={data} colors={colors} bins={6} scaleType="quantile" />
<Legend scale={scale} formatValue={(v) => v.toLocaleString("es-CL")} showMissing />

<Legend> (LegendProps)

PropTypeDefaultDescription
scaleMapScalerequiredDescriptor from createScale/scaleFromData.
formatValue(value: number) => stringtoLocaleStringBoundary label formatter.
titleReactNodenoneOptional heading.
showMissingbooleanfalseAdds a swatch for missing values.
missingLabelstring"No data"Missing swatch label.
orientation"column" | "row""column"Layout direction.
className, ariaLabelstringnoneStyling and accessibility hooks.

Low-level helpers makePalette, quantize, and quantile remain exported.

Feature types

type RegionCode = "01" || "16";
interface MapFeature { code: string; name: string; value?: number }
type RegionFeature = MapFeature;
interface ProvinciaFeature extends MapFeature { region: RegionCode }
interface ComunaFeature extends MapFeature {
region: RegionCode;
province: string; // first three CUT digits
provinceName: string;
}

type Region13ComunaCode = ComunaCode<"13">;
type Region13ProvinciaCode = ProvinciaCode<"13">;
type Region13ComunaData = ComunaData<"13">;
type Region13ProvinciaData = ProvinciaData<"13">;

Development warnings

In development builds the package emits deduplicated, Strict Mode-safe console.warn diagnostics for: data codes absent from the topology, non-finite values, invalid colors/bins, scaleType: "threshold" without thresholds, duplicate feature codes, multiple topology objects, features with unexpected parent regions, and a controlled selected code missing from the topology. Production builds emit nothing. Ordinary missing values never warn.