UNPKG

1.34 kBJavaScriptView Raw
1import { createElementHook, createElementObject, createLayerHook, updateMediaOverlay } from '@react-leaflet/core';
2import { SVGOverlay as LeafletSVGOverlay } from 'leaflet';
3import { forwardRef, useImperativeHandle } from 'react';
4import { createPortal } from 'react-dom';
5export 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);
17export const useSVGOverlay = createLayerHook(useSVGOverlayElement);
18function SVGOverlayComponent({ children , ...options }, forwardedRef) {
19 const { instance , container } = useSVGOverlay(options).current;
20 useImperativeHandle(forwardedRef, ()=>instance);
21 return container == null || children == null ? null : /*#__PURE__*/ createPortal(children, container);
22}
23export const SVGOverlay = /*#__PURE__*/ forwardRef(SVGOverlayComponent);