Kepler.gl is a powerful open source geospatial analysis tool for large-scale data sets.
feat: resize the map legend from the top handle (#3762)
Igor Dykhtade195bf58fa4
diff --git a/src/components/src/common/color-legend.tsx b/src/components/src/common/color-legend.tsxindex f88d4c27f..8beb69819 100644--- a/src/components/src/common/color-legend.tsx+++ b/src/components/src/common/color-legend.tsx@@ -32,6 +32,8 @@ const inputCss = css`const StyledLegend = styled.div<{$disableEdit: boolean; isExpanded?: boolean}>`${props => props.theme.sidePanelScrollBar};${props => (props.isExpanded ? '' : `max-height: 156px;`)};+ box-sizing: border-box;+ width: 100%;overflow-y: auto;overflow-x: hidden;margin-bottom: ${GAP}px;diff --git a/src/components/src/hooks/use-legend-position.ts b/src/components/src/hooks/use-legend-position.tsindex eea82631f..5deb5dff5 100644--- a/src/components/src/hooks/use-legend-position.ts+++ b/src/components/src/hooks/use-legend-position.ts@@ -15,13 +15,15 @@ type Params = {mapWidth?: number;};+export type LegendResizeEdge = 'top' | 'bottom';+type ReturnType = {positionStyles: Record<string, unknown>;updatePosition: () => void;contentHeight: number;maxContentHeight?: number;startResize: () => void;- resize: (deltaY: number) => void;+ resize: (deltaY: number, edge?: LegendResizeEdge) => void;};const MARGIN = {@@ -125,27 +127,43 @@ export default function useLegendPosition({// eslint-disable-next-line react-hooks/exhaustive-deps}, []);const resize = useCallback(- deltaY => {+ (deltaY: number, edge: LegendResizeEdge = 'bottom') => {const root = legendContentRef.current?.closest('.kepler-gl');const legendContent = legendContentRef.current;- if (root instanceof HTMLElement && legendContent) {- const mapRootBounds = root.getBoundingClientRect();- const legendRect = legendContent.getBoundingClientRect();- const remainingHeight =- mapRootBounds.bottom - (legendRect.top + MAP_CONTROL_HEADER_FULL_HEIGHT + MARGIN.bottom);- // Use maxContentHeight if available, otherwise fall back to viewport-based calculation- const maxHeight = maxContentHeight- ? Math.min(maxContentHeight, remainingHeight)- : remainingHeight;- const nextHeight = Math.min(- maxHeight,- Math.max(MIN_CONTENT_HEIGHT, startHeightRef.current + deltaY)- );- onChangeSettings({contentHeight: nextHeight});- if (contentHeight > 0 && pos.anchorY === 'bottom') {- onChangeSettings({position: {...pos, y: pos.y - (nextHeight - contentHeight)}});+ if (!(root instanceof HTMLElement) || !legendContent) {+ return;+ }+ const mapRootBounds = root.getBoundingClientRect();+ const legendRect = legendContent.getBoundingClientRect();+ // Bottom handle grows into space below the legend top; top handle grows+ // into space above the legend bottom so a cornered legend can still resize.+ const remainingHeight =+ edge === 'top'+ ? legendRect.bottom - MAP_CONTROL_HEADER_FULL_HEIGHT - mapRootBounds.top - MARGIN.top+ : mapRootBounds.bottom -+ (legendRect.top + MAP_CONTROL_HEADER_FULL_HEIGHT + MARGIN.bottom);+ // Use maxContentHeight if available, otherwise fall back to viewport-based calculation+ const maxHeight = maxContentHeight+ ? Math.min(maxContentHeight, remainingHeight)+ : remainingHeight;+ // Dragging the top handle up (negative deltaY) should grow the legend.+ const signedDelta = edge === 'top' ? -deltaY : deltaY;+ const nextHeight = Math.min(+ maxHeight,+ Math.max(MIN_CONTENT_HEIGHT, startHeightRef.current + signedDelta)+ );+ const nextSettings: Partial<MapLegendControlSettings> = {contentHeight: nextHeight};+ // Keep the opposite edge fixed when CSS anchoring would otherwise move it.+ const heightDelta = nextHeight - contentHeight;+ if (contentHeight > 0 && heightDelta !== 0) {+ const shouldShiftAnchor =+ (edge === 'bottom' && pos.anchorY === 'bottom') ||+ (edge === 'top' && pos.anchorY === 'top');+ if (shouldShiftAnchor) {+ nextSettings.position = {...pos, y: pos.y - heightDelta};}}+ onChangeSettings(nextSettings);},// eslint-disable-next-line react-hooks/exhaustive-deps[contentHeight, pos, onChangeSettings, maxContentHeight]diff --git a/src/components/src/map/map-control-panel.tsx b/src/components/src/map/map-control-panel.tsxindex a09bf361d..6ac77b042 100644--- a/src/components/src/map/map-control-panel.tsx+++ b/src/components/src/map/map-control-panel.tsx@@ -14,7 +14,9 @@ import classNames from 'classnames';const StyledMapControlPanel = styled.div`background-color: ${props => props.theme.mapPanelBackgroundColor};+ box-sizing: border-box;flex-grow: 1;+ width: ${props => props.theme.mapControl.width}px;z-index: 1;p {margin-bottom: 0;@@ -29,10 +31,12 @@ const StyledMapControlPanelContent = styled.div.attrs({className: 'map-control__panel-content'})<StyledMapControlPanelContentProps>`${props => props.theme.sidePanelScrollBar};+ box-sizing: border-box;max-height: 500px;min-height: 100px;- min-width: ${props => props.theme.mapControl.width}px;- overflow: ${props => (props.isExport ? 'hidden' : 'overlay')};+ width: 100%;+ overflow-x: hidden;+ overflow-y: ${props => (props.isExport ? 'hidden' : 'auto')};`;type MapControlPanelHeaderProps = {diff --git a/src/components/src/map/map-legend-panel.tsx b/src/components/src/map/map-legend-panel.tsxindex 238338802..81f57e185 100644--- a/src/components/src/map/map-legend-panel.tsx+++ b/src/components/src/map/map-legend-panel.tsx@@ -41,6 +41,7 @@ import MapLegendFactory from './map-legend';import {restrictToWindowEdges} from '@dnd-kit/modifiers';const DRAG_RESIZE_ID = 'map-legend-resize';+const DRAG_RESIZE_TOP_ID = 'map-legend-resize-top';const DRAG_MOVE_ID = 'map-legend-move';const StyledDraggableLegendContent = styled.div<{@@ -65,12 +66,13 @@ const StyledDraggableLegendContent = styled.div<{}&:hover,&.is-dragging {- .legend-move-handle {+ .legend-top-handles,+ .legend-move-handle,+ .legend-resize-handle {opacity: 1;- pointer-events: auto;}+ .legend-move-handle,.legend-resize-handle {- opacity: 1;pointer-events: auto;}border-color: ${props => props.theme.activeColor};@@ -87,26 +89,48 @@ const StyledDraggableLegendContent = styled.div<{}`;-const StyledMoveHandle = styled.div`+const StyledTopHandles = styled.div`pointer-events: none;opacity: 0;transition: opacity 0.2s ease-in-out;position: absolute;display: flex;- flex-direction: column;+ flex-direction: row;align-items: center;color: white;z-index: 2;top: 0;left: 50%;transform: translate(-50%, -50%);- width: 48px;height: 16px;border-radius: 4px;- cursor: move;background-color: ${props => props.theme.activeColor};`;+const StyledMoveHandle = styled.div`+ pointer-events: none;+ display: flex;+ flex-direction: column;+ align-items: center;+ justify-content: center;+ width: 48px;+ height: 16px;+ cursor: move;+ border-left: 1px solid rgba(255, 255, 255, 0.25);+`;++const StyledTopResizeHandle = styled.div`+ pointer-events: none;+ display: flex;+ flex-direction: column;+ align-items: center;+ justify-content: center;+ width: 32px;+ height: 16px;+ cursor: ns-resize;+ color: #f7f8fa;+`;+const StyledResizeHandle = styled.div`pointer-events: none;opacity: 0;@@ -160,8 +184,10 @@ const DraggableLegendContent = forwardRef((props: DraggableLegendContentProps, rconst {positionStyles, children, contentHeight, maxContentHeight} = props;const draggableMove = useDraggable({id: DRAG_MOVE_ID});const draggableResize = useDraggable({id: DRAG_RESIZE_ID});+ const draggableResizeTop = useDraggable({id: DRAG_RESIZE_TOP_ID});const refs = useMergeRefs([draggableMove.setNodeRef, ref]);- const isDragging = draggableMove.isDragging || draggableResize.isDragging;+ const isDragging =+ draggableMove.isDragging || draggableResize.isDragging || draggableResizeTop.isDragging;return (<StyledDraggableLegendContentref={refs}@@ -173,12 +199,29 @@ const DraggableLegendContent = forwardRef((props: DraggableLegendContentProps, r>{children}{isDragging ? <div className="legend-input-block" /> : null}- <StyledMoveHandle className="legend-move-handle" {...draggableMove.listeners}>- <DraggableDots height="16px" />- </StyledMoveHandle>+ <StyledTopHandles className="legend-top-handles">+ <StyledTopResizeHandle+ className="legend-resize-handle legend-resize-handle--top"+ aria-label="Resize legend"+ ref={draggableResizeTop.setNodeRef}+ {...draggableResizeTop.attributes}+ {...draggableResizeTop.listeners}+ >+ <HorizontalResizeHandle height="16px" />+ </StyledTopResizeHandle>+ <StyledMoveHandle+ className="legend-move-handle"+ aria-label="Move legend"+ {...draggableMove.listeners}+ >+ <DraggableDots height="16px" />+ </StyledMoveHandle>+ </StyledTopHandles><StyledResizeHandleclassName="legend-resize-handle"+ aria-label="Resize legend"ref={draggableResize.setNodeRef}+ {...draggableResize.attributes}{...draggableResize.listeners}><HorizontalResizeHandle height="16px" />@@ -225,6 +268,7 @@ const DraggableLegend = withTheme(event => {switch (event.active.id) {case DRAG_RESIZE_ID:+ case DRAG_RESIZE_TOP_ID:startResize();break;default:@@ -238,7 +282,10 @@ const DraggableLegend = withTheme(event => {switch (event.active.id) {case DRAG_RESIZE_ID:- resize(event.delta.y);+ resize(event.delta.y, 'bottom');+ break;+ case DRAG_RESIZE_TOP_ID:+ resize(event.delta.y, 'top');break;}},diff --git a/src/components/src/map/map-legend.tsx b/src/components/src/map/map-legend.tsxindex f40c7c4ac..1f6539005 100644--- a/src/components/src/map/map-legend.tsx+++ b/src/components/src/map/map-legend.tsx@@ -31,7 +31,9 @@ export const StyledMapControlLegend = styled.div<StyledMapControlLegendProps>`border-bottom-style: solid;border-bottom-width: ${props => (props.$last ? 0 : '1px')};width: ${props => props.width}px;+ max-width: 100%;box-sizing: border-box;+ overflow-x: hidden;.legend--layer_name {font-size: 12px;diff --git a/test/browser/components/hooks/use-legend-position.spec.js b/test/browser/components/hooks/use-legend-position.spec.jsindex da07bf8fd..cb78a2c11 100644--- a/test/browser/components/hooks/use-legend-position.spec.js+++ b/test/browser/components/hooks/use-legend-position.spec.js@@ -1,7 +1,7 @@// SPDX-License-Identifier: MIT// Copyright contributors to the kepler.gl project-import {renderHook} from '@testing-library/react';+import {renderHook, act} from '@testing-library/react';import {useLegendPosition} from '@kepler.gl/components';const THEME = {@@ -16,7 +16,9 @@ describe('useLegendPosition', () => {style="position: absolute; width: 800px; height: 600px"><div id="map-legend">- <div id="map-legend-content"/>+ <div id="map-legend-content">+ <div class="map-control__panel-content"/>+ </div></div></div>`;});@@ -133,4 +135,117 @@ describe('useLegendPosition', () => {).toBeLessThanOrEqual(526);}});++ test('top-edge resize grows a bottom-anchored legend without moving it', () => {+ const onChangeSettings = jest.fn();+ const legendContent = setupResizeFixture({+ contentHeight: 150,+ legendRect: {top: 386, left: 600, bottom: 570, right: 790},+ rootRect: {top: 0, left: 0, bottom: 600, right: 800}+ });++ const {result} = renderHook(() =>+ useLegendPosition({+ legendContentRef: {current: legendContent},+ isSidePanelShown: false,+ settings: {+ position: {x: 10, y: 30, anchorX: 'right', anchorY: 'bottom'},+ contentHeight: 150+ },+ onChangeSettings,+ theme: THEME+ })+ );++ act(() => {+ result.current.startResize();+ result.current.resize(-40, 'top');+ });++ expect(onChangeSettings).toHaveBeenCalledWith({contentHeight: 190});+ });++ test('bottom-edge resize cannot grow a legend already at the bottom', () => {+ const onChangeSettings = jest.fn();+ const legendContent = setupResizeFixture({+ contentHeight: 150,+ legendRect: {top: 386, left: 600, bottom: 570, right: 790},+ rootRect: {top: 0, left: 0, bottom: 600, right: 800}+ });++ const {result} = renderHook(() =>+ useLegendPosition({+ legendContentRef: {current: legendContent},+ isSidePanelShown: false,+ settings: {+ position: {x: 10, y: 30, anchorX: 'right', anchorY: 'bottom'},+ contentHeight: 150+ },+ onChangeSettings,+ theme: THEME+ })+ );++ act(() => {+ result.current.startResize();+ result.current.resize(40, 'bottom');+ });++ expect(onChangeSettings).toHaveBeenCalledWith({contentHeight: 150});+ });++ test('top-edge resize keeps the bottom fixed when top-anchored', () => {+ const onChangeSettings = jest.fn();+ const legendContent = setupResizeFixture({+ contentHeight: 150,+ legendRect: {top: 100, left: 20, bottom: 284, right: 210},+ rootRect: {top: 0, left: 0, bottom: 600, right: 800}+ });++ const {result} = renderHook(() =>+ useLegendPosition({+ legendContentRef: {current: legendContent},+ isSidePanelShown: false,+ settings: {+ position: {x: 20, y: 100, anchorX: 'left', anchorY: 'top'},+ contentHeight: 150+ },+ onChangeSettings,+ theme: THEME+ })+ );++ act(() => {+ result.current.startResize();+ result.current.resize(-40, 'top');+ });++ expect(onChangeSettings).toHaveBeenCalledWith({+ contentHeight: 190,+ position: {x: 20, y: 60, anchorX: 'left', anchorY: 'top'}+ });+ });});++function rect(values) {+ return {+ width: values.right - values.left,+ height: values.bottom - values.top,+ x: values.left,+ y: values.top,+ ...values,+ toJSON() {+ return this;+ }+ };+}++function setupResizeFixture({contentHeight, legendRect, rootRect}) {+ const root = document.querySelector('.kepler-gl');+ const legendContent = document.querySelector('#map-legend-content');+ const content = legendContent.querySelector('.map-control__panel-content');+ root.getBoundingClientRect = jest.fn(() => rect(rootRect));+ legendContent.getBoundingClientRect = jest.fn(() => rect(legendRect));+ Object.defineProperty(content, 'offsetHeight', {configurable: true, value: contentHeight});+ return legendContent;+}