UNPKG

2.08 kBJavaScriptView Raw
1function _extends() {
2 _extends = Object.assign || function(target) {
3 for(var i = 1; i < arguments.length; i++){
4 var source = arguments[i];
5 for(var key in source){
6 if (Object.prototype.hasOwnProperty.call(source, key)) {
7 target[key] = source[key];
8 }
9 }
10 }
11 return target;
12 };
13 return _extends.apply(this, arguments);
14}
15import { LeafletProvider, createLeafletContext } from '@react-leaflet/core';
16import { Map as LeafletMap } from 'leaflet';
17import React, { forwardRef, useCallback, useEffect, useImperativeHandle, useState } from 'react';
18function MapContainerComponent({ bounds , boundsOptions , center , children , className , id , placeholder , style , whenReady , zoom , ...options }, forwardedRef) {
19 const [props] = useState({
20 className,
21 id,
22 style
23 });
24 const [context, setContext] = useState(null);
25 useImperativeHandle(forwardedRef, ()=>context?.map ?? null, [
26 context
27 ]);
28 const mapRef = useCallback((node)=>{
29 if (node !== null && context === null) {
30 const map = new LeafletMap(node, options);
31 if (center != null && zoom != null) {
32 map.setView(center, zoom);
33 } else if (bounds != null) {
34 map.fitBounds(bounds, boundsOptions);
35 }
36 if (whenReady != null) {
37 map.whenReady(whenReady);
38 }
39 setContext(createLeafletContext(map));
40 }
41 // eslint-disable-next-line react-hooks/exhaustive-deps
42 }, []);
43 useEffect(()=>{
44 return ()=>{
45 context?.map.remove();
46 };
47 }, [
48 context
49 ]);
50 const contents = context ? /*#__PURE__*/ React.createElement(LeafletProvider, {
51 value: context
52 }, children) : placeholder ?? null;
53 return /*#__PURE__*/ React.createElement("div", _extends({}, props, {
54 ref: mapRef
55 }), contents);
56}
57export const MapContainer = /*#__PURE__*/ forwardRef(MapContainerComponent);