6 changed files with 183 additions and 211 deletions
@ -4,7 +4,7 @@ specifiers: |
|||
'@floating-ui/react-dom': ^0.4.3 |
|||
'@headlessui/react': ^1.4.2 |
|||
'@meshtastic/components': ^1.0.15 |
|||
'@meshtastic/meshtasticjs': ^0.6.36 |
|||
'@meshtastic/meshtasticjs': ^0.6.37 |
|||
'@reduxjs/toolkit': ^1.7.1 |
|||
'@types/mapbox-gl': ^2.6.0 |
|||
'@types/react': ^17.0.38 |
|||
@ -62,7 +62,7 @@ dependencies: |
|||
'@floating-ui/react-dom': 0.4.3_b3482aaf5744fc7c2aeb7941b0e0a78f |
|||
'@headlessui/react': 1.4[email protected][email protected] |
|||
'@meshtastic/components': 1.0.15_@[email protected] |
|||
'@meshtastic/meshtasticjs': 0.6.36 |
|||
'@meshtastic/meshtasticjs': 0.6.37 |
|||
'@reduxjs/toolkit': 1.7[email protected][email protected] |
|||
base64-js: 1.5.1 |
|||
boring-avatars: 1.6.1 |
|||
@ -1535,8 +1535,8 @@ packages: |
|||
- '@types/react' |
|||
dev: false |
|||
|
|||
/@meshtastic/meshtasticjs/0.6.36: |
|||
resolution: {integrity: sha512-Ibd6guVm1qC1P26smIBY+95WdjYFtWIKjDSDTS2Q0xvrvWT3gtL+kuGf+E+XKHqbtxVSAyJQ6RoG1hCEYRByXw==} |
|||
/@meshtastic/meshtasticjs/0.6.37: |
|||
resolution: {integrity: sha512-HXl8/eTvZAW9b4MfNxoZa2/qrKP4Y2RlyPP8jQOO+FHy5u/BLuMwAEbA69YqsOZMF9SK5/xNwHlrBnMXwWKHDA==} |
|||
dependencies: |
|||
'@protobuf-ts/runtime': 2.1.0 |
|||
sub-events: 1.8.9 |
|||
|
|||
@ -0,0 +1,50 @@ |
|||
import React from 'react'; |
|||
import ReactDOM from 'react-dom'; |
|||
|
|||
import mapbox from 'mapbox-gl'; |
|||
import ReactDOMServer from 'react-dom/server'; |
|||
|
|||
import { useMapbox } from '@hooks/useMapbox'; |
|||
|
|||
export interface MarkerProps extends Omit<mapbox.MarkerOptions, 'element'> { |
|||
children?: React.ReactNode; |
|||
center: mapbox.LngLatLike; |
|||
popup: JSX.Element; |
|||
} |
|||
|
|||
export const Marker = ({ |
|||
children, |
|||
center, |
|||
popup, |
|||
...props |
|||
}: MarkerProps): JSX.Element => { |
|||
const { map } = useMapbox(); |
|||
const ref = React.useRef<HTMLDivElement>(document.createElement('div')); |
|||
|
|||
const addMarker = React.useCallback((): void => { |
|||
if (map) { |
|||
const marker = new mapbox.Marker(ref.current, props) |
|||
.setLngLat(center) |
|||
.setPopup( |
|||
new mapbox.Popup().setHTML(ReactDOMServer.renderToString(popup)), |
|||
); |
|||
marker.addTo(map); |
|||
} |
|||
}, [map, center, props, popup]); |
|||
|
|||
React.useEffect(() => { |
|||
map?.on('load', () => { |
|||
addMarker(); |
|||
}); |
|||
}, [addMarker, map]); |
|||
|
|||
React.useEffect(() => { |
|||
if (map?.loaded()) { |
|||
addMarker(); |
|||
} |
|||
}, [addMarker, map]); |
|||
|
|||
<div ref={ref}>{children}</div>; |
|||
|
|||
return ReactDOM.createPortal(children, ref.current); |
|||
}; |
|||
Loading…
Reference in new issue