Skip to content

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

[Fix] legend wasn't interactive in shadow DOM (#2630)

Igor Dykhtaad94d703449c
diff --git a/src/components/src/map/map-legend-panel.tsx b/src/components/src/map/map-legend-panel.tsx
index 85f1d3295..4ecaf7b37 100644
--- a/src/components/src/map/map-legend-panel.tsx
+++ b/src/components/src/map/map-legend-panel.tsx
@@ -9,7 +9,6 @@ import {MapControlButton} from '../common/styled-components';
import MapControlTooltipFactory from './map-control-tooltip';
import MapControlPanelFactory from './map-control-panel';
import MapLegendFactory from './map-legend';
-import LazyTippy from './lazy-tippy';
import {createPortal} from 'react-dom';
import {DIMENSIONS} from '@kepler.gl/constants';
import {MapControlItem, MapControls, MapState} from '@kepler.gl/types';
@@ -29,6 +28,14 @@ export type MapLegendPanelFactoryDeps = [
interface PinToBottomProps {
offsetRight?: number;
}
+import {
+ FloatingPortal,
+ offset,
+ useClick,
+ useDismiss,
+ useFloating,
+ useInteractions
+} from '@floating-ui/react';
const PinToBottom = styled.div<PinToBottomProps>`
position: absolute;
@@ -87,6 +94,19 @@ function MapLegendPanelFactory(MapControlTooltip, MapControlPanel, MapLegend) {
}) => {
const mapLegend = mapControls?.mapLegend || ({} as MapControlItem);
const {active: isPinned} = mapLegend || {};
+ const rootContext = useContext(RootContext);
+ const [isOpened, setIsOpened] = useState(false);
+
+ const {refs, context, floatingStyles} = useFloating({
+ placement: 'left-start',
+ open: isOpened,
+ onOpenChange: setIsOpened,
+ middleware: [offset(10)]
+ });
+
+ const click = useClick(context);
+ const dismiss = useDismiss(context);
+ const {getFloatingProps, getReferenceProps} = useInteractions([click, dismiss]);
const onClick = useCallback(() => {
onClickControlBtn?.();
@@ -94,7 +114,6 @@ function MapLegendPanelFactory(MapControlTooltip, MapControlPanel, MapLegend) {
onToggleMapControl('mapDraw');
}
}, [onClickControlBtn, onToggleMapControl, mapControls]);
- const [tippyInstance, setTippyInstance] = useState(null);
const onCloseClick = useCallback(
e => {
e.preventDefault();
@@ -105,15 +124,11 @@ function MapLegendPanelFactory(MapControlTooltip, MapControlPanel, MapLegend) {
const onPinClick = useCallback(
e => {
e.preventDefault();
- if (tippyInstance) {
- // @ts-ignore
- tippyInstance.hide();
- }
+ setIsOpened(false);
onToggleMapControl('mapLegend');
},
- [tippyInstance, onToggleMapControl]
+ [setIsOpened, onToggleMapControl]
);
- const rootContext = useContext(RootContext);
if (!mapLegend.show) {
return null;
@@ -154,27 +169,22 @@ function MapLegendPanelFactory(MapControlTooltip, MapControlPanel, MapLegend) {
}
return (
- // The outer div is to prevent an accessibility warning from Tippy
- <div>
- {/*
- // @ts-ignore */}
- <LazyTippy
- interactive={true}
- trigger="click"
- placement="left-start"
- onCreate={setTippyInstance}
- render={attrs => <div {...attrs}>{mapControlPanel}</div>}
- appendTo="parent"
- >
- <div>
- <MapControlTooltip id="show-legend" message="tooltip.showLegend">
- <MapControlButton className="map-control-button show-legend" onClick={onClick}>
- <actionIcons.legend height="22px" />
- </MapControlButton>
- </MapControlTooltip>
- </div>
- </LazyTippy>
- </div>
+ <>
+ {isOpened ? (
+ <FloatingPortal root={rootContext?.current}>
+ <div ref={refs.setFloating} style={floatingStyles} {...getFloatingProps()}>
+ {mapControlPanel}
+ </div>
+ </FloatingPortal>
+ ) : null}
+ <div ref={refs.setReference} {...getReferenceProps()}>
+ <MapControlTooltip id="show-legend" message="tooltip.showLegend">
+ <MapControlButton className="map-control-button show-legend" onClick={onClick}>
+ <actionIcons.legend height="22px" />
+ </MapControlButton>
+ </MapControlTooltip>
+ </div>
+ </>
);
};
diff --git a/src/components/src/map/map-popover.tsx b/src/components/src/map/map-popover.tsx
index c3090c5f2..adf7cad1c 100644
--- a/src/components/src/map/map-popover.tsx
+++ b/src/components/src/map/map-popover.tsx
@@ -1,19 +1,26 @@
// SPDX-License-Identifier: MIT
// Copyright contributors to the kepler.gl project
-import React, {useState, useCallback} from 'react';
+import React, {useState, useCallback, useContext} from 'react';
import styled from 'styled-components';
import MapPopoverContentFactory from './map-popover-content';
import {Pin, ArrowLeft, ArrowRight, CursorPoint} from '../common/icons';
import {injectIntl, IntlShape} from 'react-intl';
import {FormattedMessage} from '@kepler.gl/localization';
-import Tippy from '@tippyjs/react/headless';
import {RootContext} from '../';
import {parseGeoJsonRawFeature} from '@kepler.gl/layers';
import {idToPolygonGeo, generateHashId} from '@kepler.gl/utils';
import {LAYER_TYPES} from '@kepler.gl/constants';
import {LayerHoverProp} from '@kepler.gl/reducers';
import {Feature, FeatureSelectionContext} from '@kepler.gl/types';
+import {
+ FloatingPortal,
+ flip,
+ offset,
+ useClientPoint,
+ useFloating,
+ useInteractions
+} from '@floating-ui/react';
const SELECTABLE_LAYERS: string[] = [LAYER_TYPES.hexagonId, LAYER_TYPES.geojson];
const MAX_WIDTH = 500;
@@ -22,6 +29,7 @@ const MAX_HEIGHT = 600;
const StyledMapPopover = styled.div`
display: flex;
flex-direction: column;
+
max-width: ${MAX_WIDTH}px;
max-height: ${MAX_HEIGHT}px;
padding: 14px;
@@ -34,7 +42,7 @@ const StyledMapPopover = styled.div`
font-weight: 500;
background-color: ${props => props.theme.panelBackground};
color: ${props => props.theme.textColor};
- z-index: 1000;
+ z-index: 98; /* should be below 99 which is side pane */
overflow-x: auto;
box-shadow: ${props => props.theme.panelBoxShadow};
@@ -129,53 +137,6 @@ const StyledSelectGeometry = styled.div`
MapPopoverFactory.deps = [MapPopoverContentFactory];
-function createVirtualReference(container, x, y, size = 0) {
- const bounds =
- container && container.getBoundingClientRect ? container.getBoundingClientRect() : {};
- const left = (bounds.left || 0) + x - size / 2;
- const top = (bounds.top || 0) + y - size / 2;
- return {
- left,
- top,
- right: left + size,
- bottom: top + size,
- width: size,
- height: size,
- // These properties are present to meet the DOMRect interface
- y: top,
- x: left,
- toJSON() {
- return this;
- }
- };
-}
-
-function getOffsetForPlacement({placement}, gap = 20) {
- switch (placement) {
- case 'top-start':
- case 'bottom-start':
- return [gap, gap];
- case 'top-end':
- case 'bottom-end':
- return [-gap, gap];
- default:
- return [0, 0];
- }
-}
-
-function getPopperOptions(container) {
- return {
- modifiers: [
- {
- name: 'preventOverflow',
- options: {
- boundary: container
- }
- }
- ]
- };
-}
-
export function getSelectedFeature(layerHoverProp: LayerHoverProp | null): Feature | null {
const layer = layerHoverProp?.layer;
let fieldIdx;
@@ -242,7 +203,11 @@ export default function MapPopoverFactory(
const [horizontalPlacement, setHorizontalPlacement] = useState('start');
const moveLeft = () => setHorizontalPlacement('end');
const moveRight = () => setHorizontalPlacement('start');
-
+ const rootContext = useContext(RootContext);
+ const {refs, context, floatingStyles} = useFloating({
+ placement: `${horizontalPlacement == 'end' ? 'left' : 'right'}-start`,
+ middleware: [offset({mainAxis: 20, alignmentAxis: 20}), flip()]
+ });
const onSetSelectedFeature = useCallback(() => {
const clickContext = {
mapIndex: 0,
@@ -260,65 +225,60 @@ export default function MapPopoverFactory(
onClose();
}, [onClose, onSetFeatures, x, y, setSelectedFeature, layerHoverProp, featureCollection]);
+ const containerBounds = container?.getBoundingClientRect();
+ const clientPoint = useClientPoint(context, {
+ x: (containerBounds?.left || 0) + x,
+ y: (containerBounds?.top || 0) + y
+ });
+ const {getFloatingProps} = useInteractions([clientPoint]);
+
return (
- <RootContext.Consumer>
- {context => (
- <Tippy
- popperOptions={getPopperOptions(container)}
- zIndex={98} /* should be below side panel's z-index of 99 */
- visible={true}
- interactive={true}
- // @ts-ignore
- getReferenceClientRect={() => createVirtualReference(container, x, y)}
- // @ts-ignore
- placement={`bottom-${horizontalPlacement}`}
- // @ts-ignore
- offset={getOffsetForPlacement}
- appendTo={context?.current || document.body}
- render={attrs => (
- <StyledMapPopover {...attrs} className="map-popover">
- {frozen ? (
- <PinnedButtons>
- {horizontalPlacement === 'start' && (
- <StyledIcon className="popover-arrow-left" onClick={moveLeft}>
- <ArrowLeft />
- </StyledIcon>
- )}
- <StyledIcon className="popover-pin" onClick={onClose}>
- <Pin height="16px" />
- </StyledIcon>
- {horizontalPlacement === 'end' && (
- <StyledIcon className="popover-arrow-right" onClick={moveRight}>
- <ArrowRight />
- </StyledIcon>
- )}
- {isBase && (
- <div className="primary-label">
- <FormattedMessage id="mapPopover.primary" />
- </div>
- )}
- </PinnedButtons>
- ) : null}
- <PopoverContent>
- <MapPopoverContent
- coordinate={coordinate}
- zoom={zoom}
- layerHoverProp={layerHoverProp}
- />
- </PopoverContent>
- {layerHoverProp?.layer?.type &&
- SELECTABLE_LAYERS.includes(layerHoverProp?.layer?.type) &&
- frozen ? (
- <StyledSelectGeometry className="select-geometry" onClick={onSetSelectedFeature}>
- <CursorPoint />
- Select Geometry
- </StyledSelectGeometry>
- ) : null}
- </StyledMapPopover>
- )}
- />
- )}
- </RootContext.Consumer>
+ <FloatingPortal root={rootContext?.current}>
+ <StyledMapPopover
+ className="map-popover"
+ ref={refs.setFloating}
+ style={floatingStyles}
+ {...getFloatingProps()}
+ >
+ {frozen ? (
+ <PinnedButtons>
+ {horizontalPlacement === 'start' && (
+ <StyledIcon className="popover-arrow-left" onClick={moveLeft}>
+ <ArrowLeft />
+ </StyledIcon>
+ )}
+ <StyledIcon className="popover-pin" onClick={onClose}>
+ <Pin height="16px" />
+ </StyledIcon>
+ {horizontalPlacement === 'end' && (
+ <StyledIcon className="popover-arrow-right" onClick={moveRight}>
+ <ArrowRight />
+ </StyledIcon>
+ )}
+ {isBase && (
+ <div className="primary-label">
+ <FormattedMessage id="mapPopover.primary" />
+ </div>
+ )}
+ </PinnedButtons>
+ ) : null}
+ <PopoverContent>
+ <MapPopoverContent
+ coordinate={coordinate}
+ zoom={zoom}
+ layerHoverProp={layerHoverProp}
+ />
+ </PopoverContent>
+ {layerHoverProp?.layer?.type &&
+ SELECTABLE_LAYERS.includes(layerHoverProp?.layer?.type) &&
+ frozen ? (
+ <StyledSelectGeometry className="select-geometry" onClick={onSetSelectedFeature}>
+ <CursorPoint />
+ Select Geometry
+ </StyledSelectGeometry>
+ ) : null}
+ </StyledMapPopover>
+ </FloatingPortal>
);
};
return injectIntl(MapPopover);
diff --git a/test/browser-headless/component/map-container-test.js b/test/browser-headless/component/map-container-test.js
index 4fdceaf6a..4e09965bf 100644
--- a/test/browser-headless/component/map-container-test.js
+++ b/test/browser-headless/component/map-container-test.js
@@ -15,7 +15,6 @@ import {
MapViewStateContextProvider
} from '@kepler.gl/components';
// import {Map} from 'react-map-gl'; // see other TODO below
-import Tippy from '@tippyjs/react/headless';
import {gl, InteractionTestRunner} from '@deck.gl/test-utils';
import {mockKeplerProps, expectedLayerHoverProp} from '../../helpers/mock-state';
@@ -208,34 +207,12 @@ test('MapContainerFactory - _renderDeckOverlay', t => {
// test MapPopoverProp
testMapPopoverProp(t, mapPopoverProps);
- // map control and map popover both uses Tippy
- t.equal(wrapper.find(Tippy).length, 2, 'should render Tippy');
+ t.equal(wrapper.find('.map-popover').length, 3, 'should render .map-popover');
t.equal(wrapper.find('table').length, 1, 'should render 1 table');
const table = wrapper.find('table').at(0);
const rows = table.find('.layer-hover-info__row');
t.equal(rows.length, 5, 'should render 5 rows');
- const tippyProps = wrapper.find(Tippy).at(1).props();
-
- const expectedClientRect = {
- bottom: 200,
- height: 0,
- left: 200,
- right: 200,
- top: 200,
- width: 0,
- y: 200,
- x: 200
- };
-
- const rect = tippyProps.getReferenceClientRect();
- delete rect.toJSON;
-
- t.deepEqual(
- rect,
- expectedClientRect,
- 'getReferenceClientRect should return correct rect'
- );
const expectedTooltips = [
['gps_data.utc_timestamp', '2016-09-17 00:24:24'],
diff --git a/test/setup-browser-env.js b/test/setup-browser-env.js
index aea735741..bb5dc5018 100644
--- a/test/setup-browser-env.js
+++ b/test/setup-browser-env.js
@@ -163,3 +163,6 @@ global.IntersectionObserver = class IntersectionObserver {
return null;
}
};
+
+// Undefined once bumped to node v18 in @floating-ui
+global.Node = global.Node || (() => {});