Skip to content

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

fix(components): expose basemap background color (#3855)

Igor Dykhta546f1046ff31
diff --git a/src/components/src/map-container.tsx b/src/components/src/map-container.tsx
index 6227430fa..5ff05c8ee 100644
--- a/src/components/src/map-container.tsx
+++ b/src/components/src/map-container.tsx
@@ -456,23 +456,21 @@ export default function MapContainerFactory(
generateMapboxLayerMethod(layer, layerData, layerOrder, layersToRender)
);
- // merge in a background-color style if the basemap choice is NO_MAP_ID
- // used by <StyledMap> inline style prop
- mapStyleTypeSelector = props => props.mapStyle.styleType;
+ // Paint the configured flat-map background behind the renderer surfaces.
+ // Mapbox and MapLibre clear their WebGL canvases to transparent, so this is
+ // the color exposed beyond the projected map when a pitched view zooms out.
mapStyleBackgroundColorSelector = props => props.mapStyle.backgroundColor;
globeModeSelector = props => Boolean(props.mapState?.globe?.enabled);
globeBackgroundColorSelector = props => props.mapState?.globe?.config?.backgroundColor;
globeStarsSelector = props => Boolean(props.mapState?.globe?.config?.stars);
transparentBackgroundSelector = props => Boolean(props.transparentBackground);
styleSelector = createSelector(
- this.mapStyleTypeSelector,
this.mapStyleBackgroundColorSelector,
this.globeModeSelector,
this.globeBackgroundColorSelector,
this.globeStarsSelector,
this.transparentBackgroundSelector,
(
- styleType,
backgroundColor,
isGlobeMode,
globeBackgroundColor,
@@ -480,7 +478,7 @@ export default function MapContainerFactory(
transparentBackground
) => ({
...MAP_STYLE.container,
- ...(!transparentBackground && styleType === NO_MAP_ID
+ ...(!transparentBackground && !isGlobeMode && backgroundColor
? {backgroundColor: rgbToHex(backgroundColor)}
: {}),
// In globe mode the deck.gl canvas is transparent (the globe View uses
diff --git a/src/components/src/side-panel/map-manager.spec.tsx b/src/components/src/side-panel/map-manager.spec.tsx
new file mode 100644
index 000000000..f486bab31
--- /dev/null
+++ b/src/components/src/side-panel/map-manager.spec.tsx
@@ -0,0 +1,66 @@
+// SPDX-License-Identifier: MIT
+// Copyright contributors to the kepler.gl project
+
+import React from 'react';
+
+import {MapStateActions, MapStyleActions} from '@kepler.gl/actions';
+import {appInjector} from '@kepler.gl/components';
+import {NO_MAP_ID} from '@kepler.gl/constants';
+import {INITIAL_MAP_STATE, INITIAL_MAP_STYLE} from '@kepler.gl/reducers';
+import {renderWithTheme} from 'test/helpers/component-jest-utils';
+
+import MapManagerFactory from './map-manager';
+
+const MapManager = appInjector.get(MapManagerFactory);
+
+const panelMetadata = {
+ id: 'map',
+ label: 'sidebar.panels.basemap',
+ iconComponent: () => null,
+ onClick: null
+};
+
+const defaultProps = {
+ mapStyle: INITIAL_MAP_STYLE,
+ mapState: INITIAL_MAP_STATE,
+ mapStyleActions: MapStyleActions,
+ mapStateActions: MapStateActions,
+ showAddMapStyleModal: jest.fn(),
+ panelMetadata
+};
+
+describe('MapManager', () => {
+ test('shows one background color picker for a selected basemap style', () => {
+ const {container} = renderWithTheme(<MapManager {...defaultProps} />);
+
+ expect(
+ container.querySelectorAll('[data-for="update-color-Background"]')
+ ).toHaveLength(1);
+ });
+
+ test('preserves the existing No Basemap background color picker', () => {
+ const mapStyle = {...INITIAL_MAP_STYLE, styleType: NO_MAP_ID};
+ const {container} = renderWithTheme(
+ <MapManager {...defaultProps} mapStyle={mapStyle} />
+ );
+
+ expect(
+ container.querySelectorAll('[data-for="update-color-Background"]')
+ ).toHaveLength(1);
+ });
+
+ test('preserves the separate globe background color picker', () => {
+ const mapState = {
+ ...INITIAL_MAP_STATE,
+ globe: {...INITIAL_MAP_STATE.globe, enabled: true}
+ };
+ const {container} = renderWithTheme(
+ <MapManager {...defaultProps} mapState={mapState} />
+ );
+
+ expect(container.querySelector('[data-for="update-color-Background"]')).toBeNull();
+ expect(
+ container.querySelectorAll('[data-for="update-color-globe-background"]')
+ ).toHaveLength(1);
+ });
+});
diff --git a/src/components/src/side-panel/map-manager.tsx b/src/components/src/side-panel/map-manager.tsx
index eb710196f..9c1fb00b4 100644
--- a/src/components/src/side-panel/map-manager.tsx
+++ b/src/components/src/side-panel/map-manager.tsx
@@ -16,6 +16,7 @@ import {FormattedMessage} from '@kepler.gl/localization';
import {MapStyle} from '@kepler.gl/reducers';
import {MapStyleActions, MapStateActions} from '@kepler.gl/actions';
import {MapState} from '@kepler.gl/types';
+import {BACKGROUND_LAYER_GROUP, BACKGROUND_LAYER_GROUP_SLUG} from '@kepler.gl/constants';
export type MapManagerProps = {
mapStyle: MapStyle;
@@ -59,7 +60,12 @@ function MapManagerFactory(
const [isSelecting, setIsSelecting] = useState(false);
const {mapStyleChange, removeCustomMapStyle} = mapStyleActions;
const currentStyle = mapStyle.mapStyles[mapStyle.styleType] || {};
- const editableLayers = currentStyle.layerGroups || [];
+ const configuredLayerGroups = currentStyle.layerGroups || [];
+ const editableLayers = configuredLayerGroups.some(
+ ({slug}) => slug === BACKGROUND_LAYER_GROUP_SLUG
+ )
+ ? configuredLayerGroups
+ : [...configuredLayerGroups, BACKGROUND_LAYER_GROUP];
const toggleSelecting = useCallback(() => {
setIsSelecting(prev => !prev);
diff --git a/src/sqlrooms/src/components/CustomMapManager.spec.tsx b/src/sqlrooms/src/components/CustomMapManager.spec.tsx
new file mode 100644
index 000000000..b264c8ac4
--- /dev/null
+++ b/src/sqlrooms/src/components/CustomMapManager.spec.tsx
@@ -0,0 +1,80 @@
+// SPDX-License-Identifier: MIT
+// Copyright contributors to the kepler.gl project
+
+import React from 'react';
+
+import {NO_MAP_ID} from '@kepler.gl/constants';
+import {INITIAL_MAP_STATE, INITIAL_MAP_STYLE} from '@kepler.gl/reducers';
+import {IntlWrapper, renderWithTheme} from 'test/helpers/rtl-utils';
+
+import {useKeplerStateActions} from '../hooks/useKeplerStateActions';
+import {CustomMapManager} from './CustomMapManager';
+
+jest.mock('../hooks/useKeplerStateActions', () => ({
+ useKeplerStateActions: jest.fn()
+}));
+jest.mock('./KeplerInjector', () => {
+ const {appInjector} = jest.requireActual('@kepler.gl/components');
+ return {getKeplerFactory: factory => appInjector.get(factory)};
+});
+
+const mockUseKeplerStateActions = jest.mocked(useKeplerStateActions);
+
+const keplerActions = {
+ uiStateActions: {toggleModal: jest.fn()},
+ mapStyleActions: {
+ mapStyleChange: jest.fn(),
+ removeCustomMapStyle: jest.fn(),
+ mapConfigChange: jest.fn(),
+ set3dBuildingColor: jest.fn(),
+ setBackgroundColor: jest.fn()
+ }
+};
+
+describe('CustomMapManager', () => {
+ beforeEach(() => {
+ mockUseKeplerStateActions.mockReturnValue({
+ keplerActions: keplerActions as any,
+ keplerState: {
+ mapStyle: INITIAL_MAP_STYLE,
+ mapState: INITIAL_MAP_STATE
+ } as any
+ });
+ });
+
+ test('shows an independent background picker for each flat-map instance', () => {
+ const {container} = renderWithTheme(
+ <IntlWrapper>
+ <>
+ <CustomMapManager mapId="first-map" />
+ <CustomMapManager mapId="second-map" />
+ </>
+ </IntlWrapper>
+ );
+
+ expect(container.querySelectorAll('[data-for="update-color-Background"]')).toHaveLength(2);
+ expect(mockUseKeplerStateActions).toHaveBeenCalledWith({mapId: 'first-map'});
+ expect(mockUseKeplerStateActions).toHaveBeenCalledWith({mapId: 'second-map'});
+ });
+
+ test('does not add the flat-map background picker in globe mode', () => {
+ mockUseKeplerStateActions.mockReturnValue({
+ keplerActions: keplerActions as any,
+ keplerState: {
+ mapStyle: {...INITIAL_MAP_STYLE, styleType: NO_MAP_ID},
+ mapState: {
+ ...INITIAL_MAP_STATE,
+ globe: {...INITIAL_MAP_STATE.globe, enabled: true}
+ }
+ } as any
+ });
+
+ const {container} = renderWithTheme(
+ <IntlWrapper>
+ <CustomMapManager mapId="globe-map" />
+ </IntlWrapper>
+ );
+
+ expect(container.querySelector('[data-for="update-color-Background"]')).toBeNull();
+ });
+});
diff --git a/src/sqlrooms/src/components/CustomMapManager.tsx b/src/sqlrooms/src/components/CustomMapManager.tsx
index 388b19f69..83b286a9a 100644
--- a/src/sqlrooms/src/components/CustomMapManager.tsx
+++ b/src/sqlrooms/src/components/CustomMapManager.tsx
@@ -15,7 +15,12 @@ import {
Button,
Icons
} from '@kepler.gl/components';
-import {SIDEBAR_PANELS, ADD_MAP_STYLE_ID} from '@kepler.gl/constants';
+import {
+ SIDEBAR_PANELS,
+ ADD_MAP_STYLE_ID,
+ BACKGROUND_LAYER_GROUP,
+ BACKGROUND_LAYER_GROUP_SLUG
+} from '@kepler.gl/constants';
import {MapStyle} from '@kepler.gl/reducers';
import {getKeplerFactory} from './KeplerInjector';
@@ -190,7 +195,12 @@ export const CustomMapManager: React.FC<CustomMapManagerProps> = ({mapId}) => {
const {mapStyle} = keplerState;
const currentStyle = mapStyle.mapStyles[mapStyle.styleType] || {};
- const editableLayers = (currentStyle as any).layerGroups || [];
+ const configuredLayerGroups = (currentStyle as any).layerGroups || [];
+ const editableLayers = keplerState.mapState.globe?.enabled
+ ? configuredLayerGroups.filter(({slug}) => slug !== BACKGROUND_LAYER_GROUP_SLUG)
+ : configuredLayerGroups.some(({slug}) => slug === BACKGROUND_LAYER_GROUP_SLUG)
+ ? configuredLayerGroups
+ : [...configuredLayerGroups, BACKGROUND_LAYER_GROUP];
return (
<CustomMapManagerContainer>
diff --git a/test/browser-headless/component/map-container-test.js b/test/browser-headless/component/map-container-test.js
index 4f5571ff9..55663fdb8 100644
--- a/test/browser-headless/component/map-container-test.js
+++ b/test/browser-headless/component/map-container-test.js
@@ -21,6 +21,7 @@ import {gl, InteractionTestRunner} from '@deck.gl/test-utils';
import {Provider} from 'react-redux';
import configureStore from 'redux-mock-store';
+import {NO_MAP_ID} from '@kepler.gl/constants';
import {mockKeplerProps, expectedLayerHoverProp} from '../../helpers/mock-state';
@@ -94,6 +95,97 @@ test('MapContainerFactory - display all options', t => {
t.end();
});
+test('MapContainerFactory - selected basemap background color', t => {
+ const store = mockStore(initialState);
+ const containerRef = createRef();
+ const props = {
+ ...initialProps,
+ mapStyle: {
+ ...initialProps.mapStyle,
+ styleType: 'dark-matter',
+ backgroundColor: [12, 34, 56]
+ }
+ };
+
+ const {unmount} = renderWithTheme(
+ <Provider store={store}>
+ <IntlWrapper>
+ <MapViewStateContextProvider mapState={props.mapState}>
+ <MapContainer ref={containerRef} {...props} />
+ </MapViewStateContextProvider>
+ </IntlWrapper>
+ </Provider>
+ );
+
+ t.equal(
+ containerRef.current.styleSelector(containerRef.current.props).backgroundColor,
+ '#0C2238',
+ 'should expose the configured background behind a selected basemap render surface'
+ );
+
+ const noBasemapStyle = containerRef.current.styleSelector({
+ ...containerRef.current.props,
+ mapStyle: {...props.mapStyle, styleType: NO_MAP_ID}
+ });
+ t.equal(
+ noBasemapStyle.backgroundColor,
+ '#0C2238',
+ 'should preserve the No Basemap background color'
+ );
+
+ const transparentStyle = containerRef.current.styleSelector({
+ ...containerRef.current.props,
+ transparentBackground: true
+ });
+ t.notOk(
+ Object.prototype.hasOwnProperty.call(transparentStyle, 'backgroundColor'),
+ 'should preserve transparent exports'
+ );
+
+ const globeStyle = containerRef.current.styleSelector({
+ ...containerRef.current.props,
+ mapState: {
+ ...props.mapState,
+ globe: {
+ ...props.mapState.globe,
+ enabled: true,
+ config: {...props.mapState.globe.config, backgroundColor: [9, 10, 11], stars: false}
+ }
+ }
+ });
+ t.equal(globeStyle.backgroundColor, '#090A0B', 'should preserve the globe background color');
+
+ unmount();
+ t.end();
+});
+
+test('MapContainerFactory - missing background color', t => {
+ const store = mockStore(initialState);
+ const mapStyle = {...initialProps.mapStyle, styleType: 'dark-matter'};
+ delete mapStyle.backgroundColor;
+ const props = {...initialProps, mapStyle};
+
+ const {container, unmount} = renderWithTheme(
+ <Provider store={store}>
+ <IntlWrapper>
+ <MapViewStateContextProvider mapState={props.mapState}>
+ <MapContainer {...props} />
+ </MapViewStateContextProvider>
+ </IntlWrapper>
+ </Provider>
+ );
+
+ t.ok(container.firstElementChild, 'should mount when the background color is missing');
+ t.equal(
+ container.firstElementChild.style.backgroundColor,
+ '',
+ 'should leave the container background unset when no color is configured'
+ );
+
+ unmount();
+ t.end();
+});
+
test('MapContainerFactory - _renderDeckOverlay', t => {
const props = {
...initialProps,