Skip to content

Kepler.gl is a powerful open source geospatial analysis tool for large-scale data sets.

[Enhancement] Render map control tooltip with TippyTooltip (#1612)

Shan He0931a55c79d9
diff --git a/src/components/common/tippy-tooltip.d.ts b/src/components/common/tippy-tooltip.d.ts
new file mode 100644
index 000000000..bdb3dac7b
--- /dev/null
+++ b/src/components/common/tippy-tooltip.d.ts
@@ -0,0 +1,26 @@
+// Copyright (c) 2021 Uber Technologies, Inc.
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
+
+import {TippyProps} from '@tippyjs/react';
+import {FC} from 'react';
+
+const TippyTooltip: FC<TippyProps>;
+
+export default TippyTooltip;
diff --git a/src/components/common/tippy-tooltip.js b/src/components/common/tippy-tooltip.js
new file mode 100644
index 000000000..aa73320a5
--- /dev/null
+++ b/src/components/common/tippy-tooltip.js
@@ -0,0 +1,130 @@
+// Copyright (c) 2021 Uber Technologies, Inc.
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
+
+import styled from 'styled-components';
+import React, {useState} from 'react';
+import Tippy from '@tippyjs/react';
+
+const TippyArrow = styled.div`
+ position: absolute;
+ width: 15px;
+ height: 15px;
+ fill: ${props => props.theme.tooltipBg};
+ text-align: initial;
+
+ > svg {
+ position: absolute;
+ }
+`;
+
+const TippyTooltipContent = styled(({children, ...props}) => (
+ <div {...props}>
+ {children}
+ <TippyArrow className="svg-arrow" data-popper-arrow="">
+ <svg width={15} height={15}>
+ <path d="M2,7.5 7.5,2 13,7.5Z" />
+ </svg>
+ </TippyArrow>
+ </div>
+))`
+ font-family: ${props => props.theme.fontFamily};
+ font-size: ${props => props.theme.tooltipFontSize};
+ font-weight: 400;
+ padding: 7px 18px;
+ box-shadow: ${props => props.theme.tooltipBoxShadow};
+ background-color: ${props => props.theme.tooltipBg};
+ color: ${props => props.theme.tooltipColor};
+ border-radius: ${props => props.theme.primaryBtnRadius};
+ &[data-placement^='top'] > .svg-arrow {
+ bottom: 0;
+ &::after,
+ > svg {
+ top: 7px;
+ transform: rotate(180deg);
+ }
+ }
+
+ &[data-placement^='bottom'] > .svg-arrow {
+ top: 0;
+ > svg {
+ bottom: 7px;
+ }
+ }
+
+ &[data-placement^='left'] > .svg-arrow {
+ right: 0;
+ &::after,
+ > svg {
+ transform: rotate(90deg);
+ left: 7px;
+ }
+ }
+
+ &[data-placement^='right'] > .svg-arrow {
+ left: 0;
+ &::after,
+ > svg {
+ transform: rotate(-90deg);
+ right: 7px;
+ }
+ }
+`;
+
+const TippyTooltip = ({children, render, duration = 200, ...rest}) => {
+ const [opacity, setOpacity] = useState(0);
+ const [timer, setTimer] = useState(null);
+ function onMount() {
+ setOpacity(1);
+ if (timer) {
+ // @ts-ignore
+ clearTimeout(timer);
+ }
+ }
+
+ function onHide(instance) {
+ const {unmount} = instance;
+ const timeout = setTimeout(() => {
+ if (!instance.state?.isDestroyed) {
+ unmount();
+ }
+ }, duration);
+ // @ts-ignore
+ setTimer(timeout);
+ setOpacity(0);
+ }
+
+ return (
+ <Tippy
+ {...rest}
+ animation={true}
+ render={attrs => (
+ <TippyTooltipContent {...attrs} style={{opacity, transition: `opacity ${duration}ms`}}>
+ {render()}
+ </TippyTooltipContent>
+ )}
+ onMount={onMount}
+ onHide={onHide}
+ >
+ {children}
+ </Tippy>
+ );
+};
+
+export default TippyTooltip;
diff --git a/src/components/index.js b/src/components/index.js
index 5b2e76c3a..a661e3fc1 100644
--- a/src/components/index.js
+++ b/src/components/index.js
@@ -174,6 +174,7 @@ export {default as CanvasHack} from './common/data-table/canvas';
export {default as MapLayerSelector} from './common/map-layer-selector';
export {default as VerticalToolbar} from './common/vertical-toolbar';
export {default as ToolbarItem} from './common/toolbar-item';
+export {default as TippyTooltip} from './common/tippy-tooltip';
// side pane components
export {default as LayerTypeSelectorFactory} from './side-panel/layer-panel/layer-type-selector';
diff --git a/src/components/map/locale-panel.js b/src/components/map/locale-panel.js
index 2a451914e..d4ec001b4 100644
--- a/src/components/map/locale-panel.js
+++ b/src/components/map/locale-panel.js
@@ -25,6 +25,8 @@ import ToolbarItem from 'components/common/toolbar-item';
import {MapControlButton} from 'components/common/styled-components';
import MapControlTooltipFactory from './map-control-tooltip';
import MapControlToolbarFactory from './map-control-toolbar';
+import {FormattedMessage} from '../../localization';
+import TippyTooltip from 'components/common/tippy-tooltip';
LocalePanelFactory.deps = [MapControlTooltipFactory, MapControlToolbarFactory];
@@ -67,17 +69,23 @@ function LocalePanelFactory(MapControlTooltip, MapControlToolbar) {
))}
</MapControlToolbar>
) : null}
- <MapControlButton
- onClick={onClickButton}
- className={classnames('map-control-button', 'map-locale', {isActive})}
- active={isActive}
- data-tip
- data-for="locale"
- disableClose={disableClose}
+ <TippyTooltip
+ placement="left"
+ render={() => (
+ <div id="locale">
+ <FormattedMessage id="tooltip.selectLocale" />
+ </div>
+ )}
>
- <span className="map-control-button__locale">{currentLocal.toUpperCase()}</span>
- <MapControlTooltip id="locale" message="tooltip.selectLocale" />
- </MapControlButton>
+ <MapControlButton
+ className={classnames('map-control-button', 'map-locale', {isActive})}
+ onClick={onClickButton}
+ active={isActive}
+ disableClose={disableClose}
+ >
+ <span className="map-control-button__locale">{currentLocal.toUpperCase()}</span>
+ </MapControlButton>
+ </TippyTooltip>
</div>
);
}
diff --git a/src/components/map/map-draw-panel.js b/src/components/map/map-draw-panel.js
index 7e58079f1..d8af17593 100644
--- a/src/components/map/map-draw-panel.js
+++ b/src/components/map/map-draw-panel.js
@@ -35,6 +35,8 @@ import ToolbarItem from 'components/common/toolbar-item';
import MapControlTooltipFactory from './map-control-tooltip';
import MapControlPanelFactory from './map-control-panel';
import MapControlToolbarFactory from './map-control-toolbar';
+import {FormattedMessage} from '../../localization';
+import TippyTooltip from 'components/common/tippy-tooltip';
MapDrawPanelFactory.deps = [
MapControlTooltipFactory,
@@ -100,19 +102,25 @@ function MapDrawPanelFactory(MapControlTooltip, MapControlPanel, MapControlToolb
/>
</MapControlToolbar>
) : null}
- <MapControlButton
- onClick={e => {
- e.preventDefault();
- onToggleMenuPanel();
- }}
- active={isActive}
- className={classnames('map-control-button', 'map-draw', {isActive})}
- data-tip
- data-for="map-draw"
+ <TippyTooltip
+ placement="left"
+ render={() => (
+ <div id="map-draw">
+ <FormattedMessage id="tooltip.DrawOnMap" />
+ </div>
+ )}
>
- <actionIcons.polygon height="22px" />
- <MapControlTooltip id="map-draw" message="tooltip.DrawOnMap" />
- </MapControlButton>
+ <MapControlButton
+ className={classnames('map-control-button', 'map-draw', {isActive})}
+ onClick={e => {
+ e.preventDefault();
+ onToggleMenuPanel();
+ }}
+ active={isActive}
+ >
+ <actionIcons.polygon height="22px" />
+ </MapControlButton>
+ </TippyTooltip>
</div>
);
}
diff --git a/src/components/map/map-legend-panel.js b/src/components/map/map-legend-panel.js
index adc3d1ce3..930f840c2 100644
--- a/src/components/map/map-legend-panel.js
+++ b/src/components/map/map-legend-panel.js
@@ -22,22 +22,14 @@ import React, {useCallback} from 'react';
import classnames from 'classnames';
import {Legend} from 'components/common/icons';
import {FormattedMessage} from 'localization';
-import {MapControlButton, Tooltip} from 'components/common/styled-components';
-import MapControlTooltipFactory from './map-control-tooltip';
+import {MapControlButton} from 'components/common/styled-components';
import MapControlPanelFactory from './map-control-panel';
import MapLegendFactory from './map-legend';
+import TippyTooltip from 'components/common/tippy-tooltip';
-const MapLegendTooltip = ({id, message}) => (
- <Tooltip id={id} place="left" effect="solid">
- <span>
- <FormattedMessage id={message} />
- </span>
- </Tooltip>
-);
+MapLegendPanelFactory.deps = [MapControlPanelFactory, MapLegendFactory];
-MapLegendPanelFactory.deps = [MapControlTooltipFactory, MapControlPanelFactory, MapLegendFactory];
-
-function MapLegendPanelFactory(MapControlTooltip, MapControlPanel, MapLegend) {
+function MapLegendPanelFactory(MapControlPanel, MapLegend) {
const defaultActionIcons = {
legend: Legend
};
@@ -61,24 +53,33 @@ function MapLegendPanelFactory(MapControlTooltip, MapControlPanel, MapLegend) {
const onClick = useCallback(
e => {
e.preventDefault();
+ if (mapControls?.mapDraw?.active) {
+ onToggleMapControl('mapDraw');
+ }
onToggleMenuPanel();
},
- [onToggleMenuPanel]
+ [onToggleMenuPanel, onToggleMapControl, mapControls?.mapDraw?.active]
);
if (!mapLegend.show) {
return null;
}
return !isActive ? (
- (<MapControlButton
- data-tip
- data-for="show-legend"
- className={classnames('map-control-button', 'show-legend', {isActive})}
- onClick={onClick}
+ (<TippyTooltip
+ placement="left"
+ render={() => (
+ <div id="show-legend">
+ <FormattedMessage id="tooltip.showLegend" />
+ </div>
+ )}
>
- <actionIcons.legend height="22px" />
- <MapLegendTooltip id="show-legend" message={'tooltip.showLegend'} />
- </MapControlButton>)
+ <MapControlButton
+ onClick={onClick}
+ className={classnames('map-control-button', 'show-legend', {isActive})}
+ >
+ <actionIcons.legend height="22px" />
+ </MapControlButton>
+ </TippyTooltip>)
) : (
(<MapControlPanel
scale={scale}
diff --git a/src/components/map/split-map-button.js b/src/components/map/split-map-button.js
index 450dea5d9..791f38449 100644
--- a/src/components/map/split-map-button.js
+++ b/src/components/map/split-map-button.js
@@ -24,6 +24,8 @@ import {MapControlButton} from 'components/common/styled-components';
import {Delete, Split} from 'components/common/icons';
import MapControlTooltipFactory from './map-control-tooltip';
import MapControlPanelFactory from './map-control-panel';
+import {FormattedMessage} from '../../localization';
+import TippyTooltip from '../common/tippy-tooltip';
SplitMapButtonFactory.deps = [MapControlTooltipFactory, MapControlPanelFactory];
@@ -56,22 +58,23 @@ function SplitMapButtonFactory(MapControlTooltip) {
if (!splitMap.show) {
return null;
}
-
return isVisible ? (
- (<MapControlButton
- active={isSplit}
- onClick={onClick}
- key={`split-${isSplit}`}
- className={classnames('map-control-button', 'split-map', {'close-map': isSplit})}
- data-tip
- data-for="action-toggle"
+ (<TippyTooltip
+ placement="left"
+ render={() => (
+ <div id="action-toggle">
+ <FormattedMessage id={isSplit ? 'tooltip.closePanel' : 'tooltip.switchToDualView'} />
+ </div>
+ )}
>
- {isSplit ? <actionIcons.delete height="18px" /> : <actionIcons.split height="18px" />}
- <MapControlTooltip
- id="action-toggle"
- message={isSplit ? 'tooltip.closePanel' : 'tooltip.switchToDualView'}
- />
- </MapControlButton>)
+ <MapControlButton
+ active={isSplit}
+ onClick={onClick}
+ className={classnames('map-control-button', 'split-map', {'close-map': isSplit})}
+ >
+ {isSplit ? <actionIcons.delete height="18px" /> : <actionIcons.split height="18px" />}
+ </MapControlButton>
+ </TippyTooltip>)
) : null;
};
diff --git a/src/components/map/toggle-3d-button.js b/src/components/map/toggle-3d-button.js
index 123ad0718..ce82b0645 100644
--- a/src/components/map/toggle-3d-button.js
+++ b/src/components/map/toggle-3d-button.js
@@ -25,6 +25,8 @@ import {Cube3d} from 'components/common/icons';
import {MapControlButton} from 'components/common/styled-components';
import MapControlTooltipFactory from './map-control-tooltip';
import MapControlPanelFactory from './map-control-panel';
+import {FormattedMessage} from '../../localization';
+import TippyTooltip from 'components/common/tippy-tooltip';
Toggle3dButtonFactory.deps = [MapControlTooltipFactory, MapControlPanelFactory];
@@ -52,19 +54,22 @@ function Toggle3dButtonFactory(MapControlTooltip) {
}, [mapControls]);
return isVisible ? (
- (<MapControlButton
- onClick={onClick}
- className={classnames('map-control-button', 'toggle-3d', {map3d: dragRotate})}
- active={dragRotate}
- data-tip
- data-for="action-3d"
+ (<TippyTooltip
+ placement="left"
+ render={() => (
+ <div id="action-3d">
+ <FormattedMessage id={dragRotate ? 'tooltip.disable3DMap' : 'tooltip.3DMap'} />
+ </div>
+ )}
>
- <actionIcons.cube height="22px" />
- <MapControlTooltip
- id="action-3d"
- message={dragRotate ? 'tooltip.disable3DMap' : 'tooltip.3DMap'}
- />
- </MapControlButton>)
+ <MapControlButton
+ onClick={onClick}
+ active={dragRotate}
+ className={classnames('map-control-button', 'toggle-3d', {map3d: dragRotate})}
+ >
+ <actionIcons.cube height="22px" />
+ </MapControlButton>
+ </TippyTooltip>)
) : null;
};
diff --git a/src/components/side-panel/interaction-panel/tooltip-config/tooltip-chicklet.js b/src/components/side-panel/interaction-panel/tooltip-config/tooltip-chicklet.js
index 607f42147..a84289472 100644
--- a/src/components/side-panel/interaction-panel/tooltip-config/tooltip-chicklet.js
+++ b/src/components/side-panel/interaction-panel/tooltip-config/tooltip-chicklet.js
@@ -23,12 +23,12 @@ import styled from 'styled-components';
import {ChickletButton, ChickletTag} from 'components/common/item-selector/chickleted-input';
import {Hash, Delete} from 'components/common/icons';
import DropdownList from 'components/common/item-selector/dropdown-list';
-import {Tooltip} from 'components/common/styled-components';
import {FormattedMessage} from 'localization';
import onClickOutside from 'react-onclickoutside';
import {FIELD_OPTS} from 'constants/default-settings';
import {TOOLTIP_FORMATS, TOOLTIP_FORMAT_TYPES, TOOLTIP_KEY} from 'constants/tooltip';
import {getFormatter} from 'utils/data-utils';
+import TippyTooltip from 'components/common/tippy-tooltip';
const TIME_DISPLAY = '2020-05-11 14:00';
const getValue = fmt => fmt[TOOLTIP_KEY];
@@ -133,17 +133,9 @@ function TooltipChickletFactory(dataId, config, onChange, fields) {
<ChickletTag>{displayOption(item)}</ChickletTag>
{formatLabels.length > 1 && (
<ChickletAddonWrapper>
- <ChickletAddon data-tip data-for={`addon-${tooltipField.name}`}>
- <IconDiv status={hashStyle}>
- <Hash
- height="8px"
- onClick={e => {
- e.stopPropagation();
- this.setState({show: Boolean(!show)});
- }}
- />
- </IconDiv>
- <Tooltip id={`addon-${tooltipField.name}`} effect="solid">
+ <TippyTooltip
+ placement="top"
+ render={() => (
<span>
{hasFormat ? (
getFormatTooltip(formatLabels, tooltipField.format)
@@ -151,8 +143,20 @@ function TooltipChickletFactory(dataId, config, onChange, fields) {
<FormattedMessage id={'fieldSelector.formatting'} />
)}
</span>
- </Tooltip>
- </ChickletAddon>
+ )}
+ >
+ <ChickletAddon>
+ <IconDiv status={hashStyle}>
+ <Hash
+ height="8px"
+ onClick={e => {
+ e.stopPropagation();
+ this.setState({show: Boolean(!show)});
+ }}
+ />
+ </IconDiv>
+ </ChickletAddon>
+ </TippyTooltip>
{show && (
<StyledPopover>
<DropdownList
diff --git a/src/localization/en.js b/src/localization/en.js
index 71d9e0e4d..7dcd3b9f7 100644
--- a/src/localization/en.js
+++ b/src/localization/en.js
@@ -198,7 +198,7 @@ export default {
layerSettings: 'Layer settings',
closePanel: 'Close current panel',
switchToDualView: 'Switch to dual map view',
- showLegend: 'show legend',
+ showLegend: 'Show legend',
disable3DMap: 'Disable 3D Map',
DrawOnMap: 'Draw on map',
selectLocale: 'Select locale',
diff --git a/test/setup-browser-env.js b/test/setup-browser-env.js
index 7487c053d..105e45e24 100644
--- a/test/setup-browser-env.js
+++ b/test/setup-browser-env.js
@@ -69,7 +69,13 @@ Object.defineProperty(window, 'clipboardData', {
});
// These do not seem to be present under jsdom v16, even though the documentation suggests that should be the case
-['addEventListener', 'removeEventListener', 'dispatchEvent'].forEach(prop => {
+[
+ 'addEventListener',
+ 'removeEventListener',
+ 'dispatchEvent',
+ 'requestAnimationFrame',
+ 'cancelAnimationFrame'
+].forEach(prop => {
window[prop] = () => {};
});