Skip to content

Commit

Permalink
Centralize map cursor management
Browse files Browse the repository at this point in the history
  • Loading branch information
nerik committed Oct 10, 2023
1 parent 88e0ef2 commit 2f470a6
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

import { Feature } from 'geojson';
import { useEffect } from 'react';
import useMaps from './use-maps';
import useMaps, { useMapsContext } from './use-maps';

interface LayerInteractionHookOptions {
layerId: string;
Expand All @@ -12,6 +12,7 @@ export default function useLayerInteraction({
onClick
}: LayerInteractionHookOptions) {
const { current: mapInstance } = useMaps();
const { setCursor } = useMapsContext();
useEffect(() => {
if (!mapInstance) return;
const onPointsClick = (e) => {
Expand All @@ -20,11 +21,11 @@ export default function useLayerInteraction({
};

const onPointsEnter = () => {
mapInstance.getCanvas().style.cursor = 'pointer';
setCursor('pointer');
};

const onPointsLeave = () => {
mapInstance.getCanvas().style.cursor = '';
setCursor('auto');
};

mapInstance.on('click', layerId, onPointsClick);
Expand All @@ -36,5 +37,5 @@ export default function useLayerInteraction({
mapInstance.off('mouseenter', layerId, onPointsEnter);
mapInstance.off('mouseleave', layerId, onPointsLeave);
};
}, [layerId, mapInstance, onClick]);
}, [layerId, mapInstance, onClick, setCursor]);
}
3 changes: 2 additions & 1 deletion app/scripts/components/common/map/map-component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export default function MapComponent({
isCompared?: boolean;
projection?: ProjectionOptions;
}) {
const { initialViewState, setInitialViewState, mainId, comparedId } =
const { initialViewState, setInitialViewState, mainId, comparedId, cursor } =
useMapsContext();

const id = isCompared ? comparedId : mainId;
Expand Down Expand Up @@ -52,6 +52,7 @@ export default function MapComponent({
mapStyle={style as any}
onMove={onMove}
projection={mapboxProjection}
cursor={cursor}
>
{controls}
</ReactMapGlMap>
Expand Down
13 changes: 11 additions & 2 deletions app/scripts/components/common/map/maps.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { Styles } from './styles';
import useMapCompare from './hooks/use-map-compare';
import MapComponent from './map-component';
import useMaps, { useMapsContext } from './hooks/use-maps';
import { Cursor } from './types';

const chevronRightURI = () =>
iconDataURI(CollecticonChevronRightSmall, {
Expand Down Expand Up @@ -156,14 +157,18 @@ export default function MapsContextWrapper(props: MapsContextWrapperProps) {
zoom: 1
});

const [cursor, setCursor] = useState<Cursor>('auto');

return (
<MapsContext.Provider
value={{
initialViewState,
setInitialViewState,
mainId,
comparedId,
containerId
containerId,
cursor,
setCursor
}}
>
<Maps {...props}>{props.children}</Maps>
Expand All @@ -177,12 +182,16 @@ interface MapsContextType {
mainId: string;
comparedId: string;
containerId: string;
cursor: Cursor;
setCursor: (cursor: Cursor) => void;
}

export const MapsContext = createContext<MapsContextType>({
initialViewState: {},
setInitialViewState: () => undefined,
mainId: '',
comparedId: '',
containerId: ''
containerId: '',
cursor: 'auto',
setCursor: () => undefined
});
2 changes: 2 additions & 0 deletions app/scripts/components/common/map/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,5 @@ export type AoIFeature = Feature<Polygon> & {
selected: boolean;
id: string;
};

export type Cursor = 'auto' | 'default' | 'pointer' | 'grab' | ' move';
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ export function ExplorationMap(props: { comparing: boolean }) {
polygon: true,
trash: true
} as any}
defaultMode='draw_polygon'
onCreate={onUpdate}
onUpdate={onUpdate}
onDelete={onDelete}
Expand Down

0 comments on commit 2f470a6

Please sign in to comment.