Skip to content

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

[Enhancement] (Map Control) use lazy tippy to improve map legend rendering perf (#1924)

Igor Dykhta0889d0d1fc88
diff --git a/src/components/map/lazy-tippy.tsx b/src/components/map/lazy-tippy.tsx
new file mode 100644
index 000000000..ecc694eb3
--- /dev/null
+++ b/src/components/map/lazy-tippy.tsx
@@ -0,0 +1,36 @@
+// @ts-nocheck
+import React, {useState, forwardRef} from 'react';
+import Tippy from '@tippyjs/react/headless';
+import {isTest} from 'utils/utils';
+
+const isTestEnv = isTest();
+
+/**
+ * Lazy mounting tippy content
+ * https://gist.github.com/atomiks/520f4b0c7b537202a23a3059d4eec908
+ */
+// eslint-disable-next-line react/display-name
+const LazyTippy = forwardRef((props, ref) => {
+ const [mounted, setMounted] = useState(isTestEnv);
+
+ const lazyPlugin = {
+ fn: () => ({
+ onMount: () => setMounted(true),
+ onHidden: () => setMounted(false)
+ })
+ };
+
+ const computedProps = {...props};
+
+ computedProps.plugins = [lazyPlugin, ...(props.plugins || [])];
+
+ if (props.render) {
+ computedProps.render = (...args) => (mounted ? props.render(...args) : '');
+ } else {
+ computedProps.content = mounted ? props.content : '';
+ }
+
+ return <Tippy {...computedProps} ref={ref} />;
+});
+
+export default LazyTippy;
diff --git a/src/components/map/map-legend-panel.tsx b/src/components/map/map-legend-panel.tsx
index 2ed44c663..ea28eb513 100644
--- a/src/components/map/map-legend-panel.tsx
+++ b/src/components/map/map-legend-panel.tsx
@@ -26,7 +26,7 @@ import {MapControlButton} from 'components/common/styled-components';
import MapControlTooltipFactory from './map-control-tooltip';
import MapControlPanelFactory from './map-control-panel';
import MapLegendFactory from './map-legend';
-import Tippy from '@tippyjs/react/headless';
+import LazyTippy from './lazy-tippy';
import {createPortal} from 'react-dom';
import {DIMENSIONS} from '@kepler.gl/constants';
import {MapControl, MapControls} from 'reducers';
@@ -131,11 +131,12 @@ function MapLegendPanelFactory(MapControlTooltip, MapControlPanel, MapLegend) {
return (
// The outer div is to prevent an accessibility warning from Tippy
<div>
- <Tippy
+ {/*
+ // @ts-ignore */}
+ <LazyTippy
interactive={true}
trigger="click"
placement="left-start"
- // @ts-ignore
onCreate={setTippyInstance}
render={attrs => <div {...attrs}>{mapControlPanel}</div>}
appendTo={document.body}
@@ -147,7 +148,7 @@ function MapLegendPanelFactory(MapControlTooltip, MapControlPanel, MapLegend) {
</MapControlButton>
</MapControlTooltip>
</div>
- </Tippy>
+ </LazyTippy>
</div>
);
};
diff --git a/test/browser/components/map/map-control-test.js b/test/browser/components/map/map-control-test.js
index 818d74167..0fc406164 100644
--- a/test/browser/components/map/map-control-test.js
+++ b/test/browser/components/map/map-control-test.js
@@ -138,7 +138,7 @@ test('MapControlFactory - click options', t => {
<MapContainer {...mapContainerProps} />
</IntlWrapper>
);
- }, 'MapCnotainer should not fail without props');
+ }, 'MapContainer should not fail without props');
// layer selector is not active
t.equal(wrapper.find(MapControlButton).length, 6, 'Should show 5 MapControlButton');
@@ -221,7 +221,7 @@ test('MapControlFactory - show panels', t => {
<MapContainer {...mapContainerProps} />
</IntlWrapper>
);
- }, 'MapCnotainer should not fail without props');
+ }, 'MapContainer should not fail without props');
// show legend
t.equal(wrapper.find(MapLegend).length, 1, 'should render 1 MapLegend');