File size: 1,747 Bytes
d2efaca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import { LeafletContext, createLeafletContext } from '@react-leaflet/core';
import { Map as LeafletMap } from 'leaflet';
import React, { forwardRef, useCallback, useEffect, useImperativeHandle, useRef, useState } from 'react';
function MapContainerComponent({ bounds, boundsOptions, center, children, className, id, placeholder, style, whenReady, zoom, ...options }, forwardedRef) {
    const [props] = useState({
        className,
        id,
        style
    });
    const [context, setContext] = useState(null);
    const mapInstanceRef = useRef(undefined);
    useImperativeHandle(forwardedRef, ()=>context?.map ?? null, [
        context
    ]);
    // biome-ignore lint/correctness/useExhaustiveDependencies: ref callback
    const mapRef = useCallback((node)=>{
        if (node !== null && !mapInstanceRef.current) {
            const map = new LeafletMap(node, options);
            mapInstanceRef.current = map;
            if (center != null && zoom != null) {
                map.setView(center, zoom);
            } else if (bounds != null) {
                map.fitBounds(bounds, boundsOptions);
            }
            if (whenReady != null) {
                map.whenReady(whenReady);
            }
            setContext(createLeafletContext(map));
        }
    }, []);
    useEffect(()=>{
        return ()=>{
            context?.map.remove();
        };
    }, [
        context
    ]);
    const contents = context ? /*#__PURE__*/ React.createElement(LeafletContext, {
        value: context
    }, children) : placeholder ?? null;
    return /*#__PURE__*/ React.createElement("div", {
        ...props,
        ref: mapRef
    }, contents);
}
export const MapContainer = /*#__PURE__*/ forwardRef(MapContainerComponent);