Skip to content

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

[chore] pass props directly to Draggable legend, not withState (#3055)

Igor Dykhta111b3180d2bb
diff --git a/src/components/src/hooks/use-legend-position.ts b/src/components/src/hooks/use-legend-position.ts
index 996151bee..215ffad62 100644
--- a/src/components/src/hooks/use-legend-position.ts
+++ b/src/components/src/hooks/use-legend-position.ts
@@ -3,20 +3,12 @@
import {useMemo, useRef, useCallback, useEffect} from 'react';
-export type MapLegendControlSettings = {
- position: {
- x: number;
- y: number;
- anchorX: 'left' | 'right';
- anchorY: 'top' | 'bottom';
- };
- contentHeight: number;
-};
+import {MapLegendControlSettings} from '@kepler.gl/types';
type Params = {
legendContentRef: React.MutableRefObject<HTMLElement | null>;
isSidePanelShown: boolean;
- settings: MapLegendControlSettings;
+ settings?: MapLegendControlSettings;
onChangeSettings: (settings: Partial<MapLegendControlSettings>) => void;
theme: Record<string, any>;
};
diff --git a/src/components/src/map-container.tsx b/src/components/src/map-container.tsx
index b9abb3aa1..297445024 100644
--- a/src/components/src/map-container.tsx
+++ b/src/components/src/map-container.tsx
@@ -1111,6 +1111,8 @@ export default function MapContainerFactory(
onToggleEditorVisibility={visStateActions.toggleEditorVisibility}
onLayerVisConfigChange={visStateActions.layerVisConfigChange}
mapHeight={mapState.height}
+ setMapControlSettings={uiStateActions.setMapControlSettings}
+ activeSidePanel={activeSidePanel}
/>
{isSplitSelector(this.props) && <Droppable containerId={containerId} />}
diff --git a/src/components/src/map/map-control.tsx b/src/components/src/map/map-control.tsx
index b35264659..69f332c25 100644
--- a/src/components/src/map/map-control.tsx
+++ b/src/components/src/map/map-control.tsx
@@ -69,6 +69,9 @@ export type MapControlProps = {
logoComponent?: React.FC | React.ReactNode;
isExport?: boolean;
+ setMapControlSettings: typeof UIStateActions.setMapControlSettings;
+ activeSidePanel: string | null;
+
// optional
mapState?: MapState;
readOnly?: boolean;
diff --git a/src/components/src/map/map-legend-panel.tsx b/src/components/src/map/map-legend-panel.tsx
index f644b9a9a..78f9db498 100644
--- a/src/components/src/map/map-legend-panel.tsx
+++ b/src/components/src/map/map-legend-panel.tsx
@@ -3,7 +3,15 @@
import classnames from 'classnames';
import {createPortal} from 'react-dom';
-import React, {ComponentType, FC, forwardRef, useCallback, useContext, useRef} from 'react';
+import React, {
+ ComponentType,
+ FC,
+ forwardRef,
+ useCallback,
+ useContext,
+ useRef,
+ PropsWithChildren
+} from 'react';
import styled, {withTheme} from 'styled-components';
import {DndContext, useDraggable} from '@dnd-kit/core';
@@ -12,16 +20,15 @@ import {useMergeRefs} from '@floating-ui/react';
import {ActionHandler, setMapControlSettings, toggleSplitMapViewport} from '@kepler.gl/actions';
import {Layer} from '@kepler.gl/layers';
-import {uiStateLens} from '@kepler.gl/reducers';
import {breakPointValues} from '@kepler.gl/styles';
import {LayerVisConfig, MapControlMapLegend, MapControls, MapState} from '@kepler.gl/types';
import {hasPortableWidth} from '@kepler.gl/utils';
+import {MapLegendControlSettings} from '@kepler.gl/types';
import {Legend, DraggableDots, HorizontalResizeHandle} from '../common/icons';
import {MapControlButton} from '../common/styled-components';
import {RootContext} from '../context';
-import useLegendPosition, {MapLegendControlSettings} from '../hooks/use-legend-position';
-import {withState} from '../injector';
+import useLegendPosition from '../hooks/use-legend-position';
import MapControlPanelFactory from './map-control-panel';
import MapControlTooltipFactory from './map-control-tooltip';
import MapLegendFactory from './map-legend';
@@ -151,10 +158,21 @@ const DraggableLegendContent = forwardRef((props: DraggableLegendContentProps, r
);
});
-const DraggableLegend = withState([uiStateLens], state => state, {setMapControlSettings})(
- withTheme(({uiState, theme, setMapControlSettings, children}) => {
- const isSidePanelShown = uiState.activeSidePanel;
- const settings = uiState.mapControls?.mapLegend?.settings;
+type DraggableLegendProps = PropsWithChildren<{
+ isSidePanelShown: boolean;
+ mapControls: MapControls;
+ setMapControlSettings: typeof setMapControlSettings;
+}>;
+
+const DraggableLegend = withTheme(
+ ({
+ isSidePanelShown,
+ children,
+ mapControls,
+ setMapControlSettings,
+ theme
+ }: DraggableLegendProps & {theme: any}) => {
+ const settings = mapControls?.mapLegend?.settings;
const legendContentRef = useRef<HTMLElement>(null);
const onChangeSettings = useCallback(
@@ -209,11 +227,11 @@ const DraggableLegend = withState([uiStateLens], state => state, {setMapControlS
</DraggableLegendContent>
</DndContext>
);
- })
-) as FC<{children: React.ReactNode}>;
+ }
+) as FC<DraggableLegendProps>;
type ImageExportLegendProps = {
- settings: MapLegendControlSettings;
+ settings?: MapLegendControlSettings;
isSidePanelShown: boolean;
children: React.ReactNode;
};
@@ -277,6 +295,14 @@ export type MapLegendPanelProps = {
className: string;
settings: MapLegendControlSettings;
isSidePanelShown: boolean;
+ activeSidePanel: string | null;
+ setMapControlSettings: any;
+};
+
+type MapLegendPanelComponents = {
+ MapControlTooltip: ReturnType<typeof MapControlTooltipFactory>;
+ MapControlPanel: ReturnType<typeof MapControlPanelFactory>;
+ MapLegend: ReturnType<typeof MapLegendFactory>;
};
type MapLegendPanelComponentType = React.FC<MapLegendPanelProps>;
@@ -294,18 +320,19 @@ const MapLegendPanelComponent = ({
logoComponent,
actionIcons = defaultActionIcons,
mapState,
- uiState,
onLayerVisConfigChange,
onToggleSplitMapViewport,
onClickControlBtn,
+ activeSidePanel,
+ setMapControlSettings,
isViewportUnsyncAllowed = true,
className,
MapControlTooltip,
MapControlPanel,
MapLegend
-}) => {
- const isSidePanelShown = uiState.activeSidePanel;
- const settings = uiState.mapControls?.mapLegend?.settings;
+}: MapLegendPanelProps & MapLegendPanelComponents) => {
+ const isSidePanelShown = Boolean(activeSidePanel);
+ const settings = mapControls?.mapLegend?.settings;
const mapLegend = mapControls?.mapLegend || ({} as MapControlMapLegend);
const {active, disableEdit} = mapLegend || {};
@@ -363,7 +390,13 @@ const MapLegendPanelComponent = ({
</ImageExportLegend>
) : (
createPortal(
- <DraggableLegend>{legendPanel}</DraggableLegend>,
+ <DraggableLegend
+ isSidePanelShown={isSidePanelShown}
+ mapControls={mapControls}
+ setMapControlSettings={setMapControlSettings}
+ >
+ {legendPanel}
+ </DraggableLegend>,
rootContext?.current ?? document.body
)
)
@@ -384,20 +417,14 @@ function MapLegendPanelFactory(
MapControlPanel: ReturnType<typeof MapControlPanelFactory>,
MapLegend: ReturnType<typeof MapLegendFactory>
): MapLegendPanelComponentType {
- const MapLegendPanel = withState(
- [uiStateLens],
- _state => {
- return {
- MapControlTooltip,
- MapControlPanel,
- MapLegend
- };
- },
- {setMapControlSettings}
- )(MapLegendPanelComponent);
-
- MapLegendPanel.displayName = 'MapLegendPanel';
- return withTheme(MapLegendPanel as MapLegendPanelComponentType) as MapLegendPanelComponentType;
+ return (props: MapLegendPanelProps) => (
+ <MapLegendPanelComponent
+ {...props}
+ MapControlTooltip={MapControlTooltip}
+ MapControlPanel={MapControlPanel}
+ MapLegend={MapLegend}
+ />
+ );
}
export default MapLegendPanelFactory;
diff --git a/src/types/reducers.d.ts b/src/types/reducers.d.ts
index fcfd5e9ed..b674538b3 100644
--- a/src/types/reducers.d.ts
+++ b/src/types/reducers.d.ts
@@ -415,8 +415,20 @@ export type MapControlItem = {
disableClose?: boolean;
activeMapIndex?: number;
};
+
+export type MapLegendControlSettings = {
+ position: {
+ x: number;
+ y: number;
+ anchorX: 'left' | 'right';
+ anchorY: 'top' | 'bottom';
+ };
+ contentHeight: number;
+};
+
export type MapControlMapLegend = MapControlItem & {
disableEdit?: boolean;
+ settings?: MapLegendControlSettings;
};
export type MapControls = {
visibleLayers?: MapControlItem;