parent
a4f642d232
commit
99ce0d97e1
@ -0,0 +1,26 @@ |
||||
import leaflet from 'leaflet'; |
||||
import { Ref, useEffect } from 'preact/hooks'; |
||||
|
||||
import 'leaflet/dist/leaflet.css'; |
||||
import { Coordinates } from '../../../shared/types'; |
||||
|
||||
export const useMapClick = ( |
||||
mapRef: Readonly<Ref<leaflet.Map | undefined>>, |
||||
onMapClick: (leaflet: Coordinates) => void, |
||||
) => { |
||||
useEffect(() => { |
||||
const map = mapRef.current; |
||||
if (!map) { |
||||
return; |
||||
} |
||||
|
||||
const handler: leaflet.LeafletMouseEventHandlerFn = ({ latlng }) => { |
||||
onMapClick(latlng); |
||||
}; |
||||
map.on('click', handler); |
||||
|
||||
return () => { |
||||
map.removeEventListener('click', handler); |
||||
}; |
||||
}, [mapRef, onMapClick]); |
||||
}; |
Loading…
Reference in new issue