Kepler.gl is a powerful open source geospatial analysis tool for large-scale data sets.
[Feat] support pin map legend in map control (#1614)
Shan He612e18a9988b
diff --git a/src/components/bottom-widget.d.ts b/src/components/bottom-widget.d.tsindex 06855afa9..c936aff70 100644--- a/src/components/bottom-widget.d.ts+++ b/src/components/bottom-widget.d.ts@@ -16,6 +16,7 @@ export type BottomWidgetProps = {containerW: number;uiState: UiState;sidePanelWidth: number;+ legendWidth: number;toggleModal: typeof UIStateActions.toggleModal;children?: React.ReactNode;};diff --git a/src/components/bottom-widget.js b/src/components/bottom-widget.jsindex d03adbc50..fbad2a474 100644--- a/src/components/bottom-widget.js+++ b/src/components/bottom-widget.js@@ -23,7 +23,7 @@ import styled from 'styled-components';import TimeWidgetFactory from './filters/time-widget';import AnimationControlFactory from './common/animation-control/animation-control';import AnimationControllerFactory from './common/animation-control/animation-controller';-import {ANIMATION_WINDOW, FILTER_TYPES} from 'constants/default-settings';+import {ANIMATION_WINDOW, DIMENSIONS, FILTER_TYPES} from 'constants/default-settings';import {getIntervalBins} from 'utils/filter-utils';import {media} from 'styles/media-breakpoints';@@ -146,7 +146,14 @@ export default function BottomWidgetFactory(const animatedFilterIdx = useMemo(() => filters.findIndex(f => f.isAnimating), [filters]);const animatedFilter = animatedFilterIdx > -1 ? filters[animatedFilterIdx] : null;- const enlargedFilterWidth = isOpen ? containerW - sidePanelWidth : containerW;+ const isLegendPinned =+ uiState.mapControls?.mapLegend?.show && uiState.mapControls?.mapLegend?.active;+ const spaceForLegendWidth = isLegendPinned+ ? DIMENSIONS.mapControl.width + DIMENSIONS.mapControl.mapLegend.pinned.right+ : 0;++ const enlargedFilterWidth =+ (isOpen ? containerW - sidePanelWidth : containerW) - spaceForLegendWidth;// show playback control if layers contain trip layer & at least one trip layer is visibleconst animatableLayer = useMemo(@@ -170,6 +177,7 @@ export default function BottomWidgetFactory(return (<BottomWidgetContainerwidth={Math.min(maxWidth, enlargedFilterWidth)}+ style={{marginRight: spaceForLegendWidth}}className="bottom-widget--container"hasPadding={showAnimationControl || showTimeWidget}>diff --git a/src/components/common/icons/pin.js b/src/components/common/icons/pin.jsindex fe155a9a1..84f101e11 100644--- a/src/components/common/icons/pin.js+++ b/src/components/common/icons/pin.js@@ -21,6 +21,11 @@import React, {Component} from 'react';import PropTypes from 'prop-types';import Base from './base';+import styled from 'styled-components';++const StyledBase = styled(Base)`+ transform: rotate(30deg);+`;export default class Pin extends Component {static propTypes = {@@ -35,9 +40,9 @@ export default class Pin extends Component {render() {return (- <Base {...this.props}>+ <StyledBase {...this.props}><path d="M36 35.476V59a1 1 0 0 1-1 1h-6a1 1 0 0 1-1-1V35.476C21.103 33.696 16 27.453 16 20c0-8.836 7.163-16 16-16s16 7.164 16 16c0 7.453-5.103 13.697-12 15.476z" />- </Base>+ </StyledBase>);}}diff --git a/src/components/map-container.js b/src/components/map-container.jsindex 8e7a48bbb..ba5d441b2 100644--- a/src/components/map-container.js+++ b/src/components/map-container.js@@ -557,6 +557,7 @@ export default function MapContainerFactory(MapPopover, MapControl, Editor) {onSetEditorMode={visStateActions.setEditorMode}onSetLocale={uiStateActions.setLocale}onToggleEditorVisibility={visStateActions.toggleEditorVisibility}+ mapHeight={mapState.height}/><MapComponent{...mapProps}diff --git a/src/components/map/map-control-panel.d.ts b/src/components/map/map-control-panel.d.tsindex 91854f519..346c8fc1e 100644--- a/src/components/map/map-control-panel.d.ts+++ b/src/components/map/map-control-panel.d.ts@@ -4,6 +4,9 @@ export type MapControlPanelProps = {header?: string;scale?: number;onClick: () => void;+ onPinClick?: () => void;+ pinnable?: boolean;+ disableClose?: boolean;isExport?: boolean;logoComponent?: Element;};diff --git a/src/components/map/map-control-panel.js b/src/components/map/map-control-panel.jsindex f907e5b7a..5a4c57962 100644--- a/src/components/map/map-control-panel.js+++ b/src/components/map/map-control-panel.js@@ -22,7 +22,7 @@ import React from 'react';import styled from 'styled-components';import {FormattedMessage} from 'localization';import {IconRoundSmall} from 'components/common/styled-components';-import {Close} from 'components/common/icons';+import {Close, Pin} from 'components/common/icons';const StyledMapControlPanel = styled.div`background-color: ${props => props.theme.mapPanelBackgroundColor};@@ -51,9 +51,11 @@ const StyledMapControlPanelHeader = styled.div.attrs({background-color: ${props => props.theme.mapPanelHeaderBackgroundColor};height: 32px;padding: 6px 12px;+ font-family: ${props => props.theme.fontFamily};font-size: 11px;color: ${props => props.theme.titleTextColor};position: relative;+ box-sizing: border-box;button {width: 18px;@@ -61,10 +63,31 @@ const StyledMapControlPanelHeader = styled.div.attrs({}`;+const StyledIcon = styled(IconRoundSmall)`+ color: ${props => props.theme.activeColor};+ background-color: transparent;++ :hover {+ cursor: pointer;+ background-color: transparent;+ color: ${props => props.theme.linkBtnColor};+ }+`;+function MapControlPanelFactory() {/** @type {import('./map-control-panel').MapControlPanelComponent} */const MapControlPanel = React.memo(- ({children, header, onClick, scale = 1, isExport, logoComponent}) => (+ ({+ children,+ header,+ pinnable,+ disableClose,+ onPinClick,+ onClick,+ scale = 1,+ isExport,+ logoComponent+ }) => (<StyledMapControlPanelstyle={{transform: `scale(${scale})`,@@ -80,9 +103,18 @@ function MapControlPanelFactory() {</span>) : null}{isExport ? null : (- <IconRoundSmall className="close-map-control-item" onClick={onClick}>- <Close height="16px" />- </IconRoundSmall>+ <>+ {pinnable && (+ <StyledIcon className="pin-map-control-item" onClick={onPinClick}>+ <Pin height="16px" />+ </StyledIcon>+ )}+ {disableClose ? null : (+ <StyledIcon className="close-map-control-item" onClick={onClick}>+ <Close height="16px" />+ </StyledIcon>+ )}+ </>)}</StyledMapControlPanelHeader><StyledMapControlPanelContent>{children}</StyledMapControlPanelContent>diff --git a/src/components/map/map-control.d.ts b/src/components/map/map-control.d.tsindex a9f9c1708..552dfef5e 100644--- a/src/components/map/map-control.d.ts+++ b/src/components/map/map-control.d.ts@@ -16,21 +16,23 @@ export type MapControlProps = {layersToRender: {[key: string]: boolean};mapIndex: number;mapControls: MapControls;- readOnly?: boolean;- scale: number;- top: number;- editor: Editor;- locale: string;-onTogglePerspective: () => void;- onToggleGlobe: () => void;onToggleSplitMap: () => void;onToggleMapControl: (control: string) => void;onSetEditorMode: (mode: string) => void;- onSetLocale: () => void;onToggleEditorVisibility: () => void;+ top: number;+ onSetLocale: () => void;+ locale: string;logoComponent: React.FC | React.ReactNode;++ // optional+ readOnly?: boolean;+ scale?: number;+ mapLayers?: {[key: string]: boolean};+ editor: Editor;actionComponents: React.FC[] | React.Component[];+ mapHeight?: number;};export type MapControl = React.FC<MapControlProps>;diff --git a/src/components/map/map-control.js b/src/components/map/map-control.jsindex 37245e5a6..6393cf791 100644--- a/src/components/map/map-control.js+++ b/src/components/map/map-control.js@@ -69,9 +69,9 @@ function MapControlFactory(SplitMapButton,LayerSelectorPanel,Toggle3dButton,- MapLegendPanel,MapDrawPanel,- LocalePanel+ LocalePanel,+ MapLegendPanel];/** @type {import('./map-control').MapControl} */diff --git a/src/components/map/map-legend-panel.d.ts b/src/components/map/map-legend-panel.d.tsindex 00cab679c..b6699eb35 100644--- a/src/components/map/map-legend-panel.d.ts+++ b/src/components/map/map-legend-panel.d.ts@@ -3,7 +3,7 @@ import {Layer} from 'layers';import {MapControls} from '../../reducers';interface MapLegendPanelIcons {- legend: ComponentType<any>+ legend: ComponentType<any>;}export type MapLegendPanelProps = {@@ -14,6 +14,7 @@ export type MapLegendPanelProps = {logoComponent: Element;actionIcons: MapLegendPanelIcons;mapControls: MapControls;+ mapHeight?: number;};export type MapLegendPanelComponent = React.FunctionComponent<MapLegendPanelProps>;diff --git a/src/components/map/map-legend-panel.js b/src/components/map/map-legend-panel.jsindex 930f840c2..e81814b5b 100644--- a/src/components/map/map-legend-panel.js+++ b/src/components/map/map-legend-panel.js@@ -18,17 +18,27 @@// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN// THE SOFTWARE.-import React, {useCallback} from 'react';-import classnames from 'classnames';+import React, {useState} from 'react';+import styled from 'styled-components';+import {Legend} from 'components/common/icons';import {FormattedMessage} from 'localization';import {MapControlButton} from 'components/common/styled-components';import MapControlPanelFactory from './map-control-panel';import MapLegendFactory from './map-legend';+import Tippy from '@tippyjs/react/headless';import TippyTooltip from 'components/common/tippy-tooltip';+import {createPortal} from 'react-dom';+import {DIMENSIONS} from 'constants/default-settings';MapLegendPanelFactory.deps = [MapControlPanelFactory, MapLegendFactory];+const PinToBottom = styled.div`+ position: absolute;+ bottom: ${DIMENSIONS.mapControl.mapLegend.pinned.bottom}px;+ right: ${DIMENSIONS.mapControl.mapLegend.pinned.right}px;+`;+function MapLegendPanelFactory(MapControlPanel, MapLegend) {const defaultActionIcons = {legend: Legend@@ -42,55 +52,94 @@ function MapLegendPanelFactory(MapControlPanel, MapLegend) {onToggleMapControl,isExport,logoComponent,- actionIcons = defaultActionIcons+ actionIcons = defaultActionIcons,+ mapHeight}) => {const mapLegend = mapControls?.mapLegend || {};- const {active: isActive, disableClose} = mapLegend || {};- const onToggleMenuPanel = useCallback(() => onToggleMapControl('mapLegend'), [- onToggleMapControl- ]);+ const {active: isPinned} = mapLegend || {};- const onClick = useCallback(- e => {- e.preventDefault();- if (mapControls?.mapDraw?.active) {- onToggleMapControl('mapDraw');- }- onToggleMenuPanel();- },- [onToggleMenuPanel, onToggleMapControl, mapControls?.mapDraw?.active]- );+ const onClick = () => {+ if (mapControls?.mapDraw?.active) {+ onToggleMapControl('mapDraw');+ }+ };+ const [tippyInstance, setTippyInstance] = useState(null);+ const onCloseClick = e => {+ e.preventDefault();+ onToggleMapControl('mapLegend');+ };+ const onPinClick = e => {+ e.preventDefault();+ if (tippyInstance) {+ // @ts-ignore+ tippyInstance.hide();+ }+ onToggleMapControl('mapLegend');+ };if (!mapLegend.show) {return null;}- return !isActive ? (- (<TippyTooltip- placement="left"- render={() => (- <div id="show-legend">- <FormattedMessage id="tooltip.showLegend" />- </div>- )}- >- <MapControlButton- onClick={onClick}- className={classnames('map-control-button', 'show-legend', {isActive})}- >- <actionIcons.legend height="22px" />- </MapControlButton>- </TippyTooltip>)- ) : (- (<MapControlPanel+ const mapControlPanel = (+ <MapControlPanelscale={scale}header={'header.layerLegend'}- onClick={onToggleMenuPanel}+ isPinned={true}+ {...(isPinned+ ? {+ onClick: onCloseClick,+ pinnable: false,+ disableClose: false+ }+ : {+ onPinClick,+ pinnable: true,+ disableClose: true+ })}isExport={isExport}- disableClose={disableClose}logoComponent={logoComponent}>- <MapLegend layers={layers} />- </MapControlPanel>)+ <MapLegend layers={layers} mapHeight={mapHeight} />+ </MapControlPanel>+ );++ if (isPinned) {+ // Pinned panel is not supported in export mode+ if (isExport) {+ return mapControlPanel;+ }+ const pinnedPanel = <PinToBottom>{mapControlPanel}</PinToBottom>;+ return createPortal(pinnedPanel, document.body);+ }++ return (+ // The outer div is to prevent an accessibility warning from Tippy+ <div>+ <Tippy+ interactive={true}+ trigger="click"+ placement="left-start"+ // @ts-ignore+ onCreate={setTippyInstance}+ render={attrs => <div {...attrs}>{mapControlPanel}</div>}+ appendTo={document.body}+ >+ <div>+ <TippyTooltip+ placement="left"+ render={() => (+ <div id="show-legend">+ <FormattedMessage id="tooltip.showLegend" />+ </div>+ )}+ >+ <MapControlButton className="map-control-button show-legend" onClick={onClick}>+ <actionIcons.legend height="22px" />+ </MapControlButton>+ </TippyTooltip>+ </div>+ </Tippy>+ </div>);};diff --git a/src/components/map/map-legend.d.ts b/src/components/map/map-legend.d.tsindex 75afa2e66..31e41920a 100644--- a/src/components/map/map-legend.d.ts+++ b/src/components/map/map-legend.d.ts@@ -3,33 +3,33 @@ import {Layer, LayerConfig, VisualChannel, VisualChannelDescription} from 'layerimport {MapLegendPanelComponent} from './map-legend-panel';export type LayerSizeLegendProps = {- label: string,- name: string+ label: string;+ name: string;};export const LayerSizeLegend: FunctionComponent<LayerSizeLegendProps>;export type SingleColorLegendProps = {- width: number,- color: string+ width: number;+ color: string;};export const SingleColorLegend: FunctionComponent<SingleColorLegendProps>;export type LayerColorLegendProps = {- description: VisualChannelDescription,- config: LayerConfig,- width: number,- colorChannel: VisualChannel+ description: VisualChannelDescription;+ config: LayerConfig;+ width: number;+ colorChannel: VisualChannel;};export const LayerColorLegend: FunctionComponent<LayerColorLegendProps>;export type LayerLegendHeaderProps = {- layer: Layer,+ layer: Layer;options?: {- showLayerName?: boolean- }+ showLayerName?: boolean;+ };};export const LayerLegendHeader: FunctionComponent<LayerLegendHeaderProps>;@@ -37,8 +37,8 @@ export const LayerLegendHeader: FunctionComponent<LayerLegendHeaderProps>;export function LayerLegendHeaderFactory(): LayerLegendHeader;export type LayerLegendContentProps = {- layer: Layer,- containerW: number,+ layer: Layer;+ containerW: number;};export const LayerLegendContent: FunctionComponent<LayerLegendContentProps>;@@ -46,11 +46,12 @@ export const LayerLegendContent: FunctionComponent<LayerLegendContentProps>;export function LayerLegendContentFactory(): LayerLegendContent;export type MapLegendProps = {- layers?: ReadonlyArray<Layer>,- width?: number,+ layers?: ReadonlyArray<Layer>;+ width?: number;+ mapHeight?: number;options?: {- showLayerName?: boolean- }+ showLayerName?: boolean;+ };};export const MapLegend: FunctionComponent<MapLegendProps>;diff --git a/src/components/map/map-legend.js b/src/components/map/map-legend.jsindex c8cf3d3f9..119a426a7 100644--- a/src/components/map/map-legend.js+++ b/src/components/map/map-legend.js@@ -29,10 +29,12 @@ export const StyledMapControlLegend = styled.div`padding: 10px ${props => props.theme.mapControl.padding}px 10px${props => props.theme.mapControl.padding}px;font-size: 11px;+ font-family: ${props => props.theme.fontFamily};border-bottom-color: ${props => props.theme.panelBorderColor};border-bottom-style: solid;border-bottom-width: ${props => (props.last ? 0 : '1px')};width: ${props => props.width}px;+ box-sizing: border-box;.legend--layer_name {font-size: 12px;@@ -197,8 +199,16 @@ export function LayerLegendContentFactory() {MapLegendFactory.deps = [LayerLegendHeaderFactory, LayerLegendContentFactory];function MapLegendFactory(LayerLegendHeader, LayerLegendContent) {/** @type {typeof import('./map-legend').MapLegend }> */- const MapLegend = ({layers = [], width, options}) => (- <div className="map-legend">+ const MapLegend = ({layers = [], width, mapHeight, options}) => (+ <div+ className="map-legend"+ {...(mapHeight && {+ style: {+ /* subtracting rough size of 4 map control buttons and padding */+ maxHeight: mapHeight - 250+ }+ })}+ >{layers.map((layer, index) => {if (!layer.isValidToSave() || layer.config.hidden) {return null;diff --git a/src/components/map/map-popover.js b/src/components/map/map-popover.jsindex 5057e8b77..b377c02dc 100644--- a/src/components/map/map-popover.js+++ b/src/components/map/map-popover.js@@ -118,10 +118,6 @@ const PopoverContent = styled.div`const StyledIcon = styled.div`color: ${props => props.theme.activeColor};- &.popover-pin {- transform: rotate(30deg);- }-:hover {cursor: pointer;color: ${props => props.theme.linkBtnColor};diff --git a/src/constants/default-settings.js b/src/constants/default-settings.jsindex b73a3aee7..5ed320d8c 100644--- a/src/constants/default-settings.js+++ b/src/constants/default-settings.js@@ -139,7 +139,13 @@ export const DIMENSIONS = {},mapControl: {width: 184,- padding: 12+ padding: 12,+ mapLegend: {+ pinned: {+ bottom: 22,+ right: 12+ }+ }}};diff --git a/test/browser/components/map/map-control-test.js b/test/browser/components/map/map-control-test.jsindex 2bffdf931..87b9fead7 100644--- a/test/browser/components/map/map-control-test.js+++ b/test/browser/components/map/map-control-test.js@@ -164,21 +164,20 @@ test('MapControlFactory - click options', t => {.find('.map-control-button.show-legend').at(0).simulate('click');- t.ok(onToggleMapControl.calledOnce, 'should call onToggleMapControl');- t.deepEqual(- onToggleMapControl.args[0],- ['mapLegend', 0],- 'should call onToggleMapControl with mapLegend'- );++ t.equal(wrapper.find(MapLegend).length, 1, 'should render MapLegend');// click map drawwrapper.find('.map-control-button.map-draw').at(0).simulate('click');- t.ok(onToggleMapControl.calledTwice, 'should call onToggleMapControl');++ t.equal(wrapper.find(MapLegend).length, 1, 'should render MapLegend');++ t.ok(onToggleMapControl.calledOnce, 'should call onToggleMapControl');t.deepEqual(- onToggleMapControl.args[1],+ onToggleMapControl.args[0],['mapDraw', 0],'should call onToggleMapControl with mapDraw');@@ -188,9 +187,9 @@ test('MapControlFactory - click options', t => {.find('.map-control-button.map-locale').at(0).simulate('click');- t.ok(onToggleMapControl.calledThrice, 'should call onToggleMapControl');+ t.ok(onToggleMapControl.calledTwice, 'should call onToggleMapControl');t.deepEqual(- onToggleMapControl.args[2],+ onToggleMapControl.args[1],['mapLocale', 0],'should call onToggleMapControl with mapLocale');@@ -200,9 +199,9 @@ test('MapControlFactory - click options', t => {.find('.map-control-button.toggle-layer').at(0).simulate('click');- t.equal(onToggleMapControl.callCount, 4, 'should call onToggleMapControl');+ t.ok(onToggleMapControl.calledThrice, 'should call onToggleMapControl');t.deepEqual(- onToggleMapControl.args[3],+ onToggleMapControl.args[2],['visibleLayers', 0],'should call onToggleMapControl with visibleLayers');