-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tooltip to draw shape filter (#330)
* Add tooltip to draw filter shape 1. Changes cursor 2. Add instruction on how to proceed. 3. Revert once cancel is selected. Signed-off-by: Vijayan Balasubramanian <[email protected]> (cherry picked from commit 3a20e81)
- Loading branch information
1 parent
f3906b7
commit 17b9391
Showing
3 changed files
with
91 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import maplibregl, { Map as Maplibre, MapEventType, Popup } from 'maplibre-gl'; | ||
import React, { Fragment, useEffect, useRef } from 'react'; | ||
import { i18n } from '@osd/i18n'; | ||
import { FILTER_DRAW_MODE } from '../../../../common'; | ||
|
||
interface Props { | ||
map: Maplibre; | ||
mode: FILTER_DRAW_MODE; | ||
} | ||
|
||
const X_AXIS_GAP_BETWEEN_CURSOR_AND_POPUP = -12; | ||
const Y_AXIS_GAP_BETWEEN_CURSOR_AND_POPUP = 0; | ||
|
||
const getTooltipContent = (mode: FILTER_DRAW_MODE): string => { | ||
switch (mode) { | ||
case FILTER_DRAW_MODE.POLYGON: | ||
return i18n.translate('maps.drawFilterPolygon.tooltipContent', { | ||
defaultMessage: 'Click to start shape. Click for vertex. Double click to finish.', | ||
}); | ||
default: | ||
return i18n.translate('maps.drawFilterDefault.tooltipContent', { | ||
defaultMessage: 'Click to start shape. Double click to finish.', | ||
}); | ||
} | ||
}; | ||
|
||
export const DrawTooltip = ({ map, mode }: Props) => { | ||
const hoverPopupRef = useRef<Popup>( | ||
new maplibregl.Popup({ | ||
closeButton: false, | ||
closeOnClick: false, | ||
anchor: 'right', | ||
maxWidth: 'max-content', | ||
}) | ||
); | ||
|
||
useEffect(() => { | ||
// remove previous popup | ||
function onMouseMoveMap(e: MapEventType['mousemove']) { | ||
map.getCanvas().style.cursor = 'crosshair'; // Changes cursor to '+' | ||
hoverPopupRef.current | ||
.setLngLat(e.lngLat) | ||
.setOffset([X_AXIS_GAP_BETWEEN_CURSOR_AND_POPUP, Y_AXIS_GAP_BETWEEN_CURSOR_AND_POPUP]) // add some gap between cursor and pop up | ||
.setText(getTooltipContent(mode)) | ||
.addTo(map); | ||
} | ||
|
||
function onMouseMoveOut() { | ||
hoverPopupRef.current.remove(); | ||
} | ||
|
||
function resetAction() { | ||
map.getCanvas().style.cursor = ''; | ||
hoverPopupRef.current.remove(); | ||
// remove tooltip when users mouse move over a point | ||
map.off('mousemove', onMouseMoveMap); | ||
map.off('mouseout', onMouseMoveOut); | ||
} | ||
|
||
if (map && mode === FILTER_DRAW_MODE.NONE) { | ||
resetAction(); | ||
} else { | ||
// add tooltip when users mouse move over a point | ||
map.on('mousemove', onMouseMoveMap); | ||
map.on('mouseout', onMouseMoveOut); | ||
} | ||
return () => { | ||
// remove tooltip when users mouse move over a point | ||
// when component is unmounted | ||
resetAction(); | ||
}; | ||
}, [mode]); | ||
|
||
return <Fragment />; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters