Small preact-based (like React.js) project
https://inga-lovinde.github.io/static/komoot-demo/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
23 lines
660 B
23 lines
660 B
import leaflet from 'leaflet';
|
|
import { type Ref, useEffect } from 'preact/hooks';
|
|
import type { 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]);
|
|
|