1 | import { createElementHook, createElementObject, createLayerHook, updateMediaOverlay } from '@react-leaflet/core';
|
2 | import { SVGOverlay as LeafletSVGOverlay } from 'leaflet';
|
3 | import { forwardRef, useImperativeHandle } from 'react';
|
4 | import { createPortal } from 'react-dom';
|
5 | export const useSVGOverlayElement = createElementHook(function createSVGOverlay(props, context) {
|
6 | const { attributes , bounds , ...options } = props;
|
7 | const container = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
|
8 | container.setAttribute('xmlns', 'http://www.w3.org/2000/svg');
|
9 | if (attributes != null) {
|
10 | Object.keys(attributes).forEach((name)=>{
|
11 | container.setAttribute(name, attributes[name]);
|
12 | });
|
13 | }
|
14 | const overlay = new LeafletSVGOverlay(container, bounds, options);
|
15 | return createElementObject(overlay, context, container);
|
16 | }, updateMediaOverlay);
|
17 | export const useSVGOverlay = createLayerHook(useSVGOverlayElement);
|
18 | function SVGOverlayComponent({ children , ...options }, forwardedRef) {
|
19 | const { instance , container } = useSVGOverlay(options).current;
|
20 | useImperativeHandle(forwardedRef, ()=>instance);
|
21 | return container == null || children == null ? null : createPortal(children, container);
|
22 | }
|
23 | export const SVGOverlay = forwardRef(SVGOverlayComponent);
|