This is an automated email from the ASF dual-hosted git repository. villebro pushed a commit to branch 7.0 in repository https://gitbox.apache.org/repos/asf/superset.git
commit 9fb3b1a1ba8463d2c9df9cca7b378f4a34687b3a Author: Mayank Joshi <[email protected]> AuthorDate: Thu Nov 12 23:06:45 2026 +0530 fix(chart): correct resizable modal resize direction for top/left handles (#43333) Co-authored-by: Evan Rusackas <[email protected]> Co-authored-by: Evan Rusackas <[email protected]> (cherry picked from commit e591edc9810434ef576eb624745ca5fcdd441856) --- .../src/components/Modal/Modal.resize.test.tsx | 413 +++++++++++++++++++++ .../src/components/Modal/Modal.tsx | 93 ++++- 2 files changed, 497 insertions(+), 9 deletions(-) diff --git a/superset-frontend/packages/superset-ui-core/src/components/Modal/Modal.resize.test.tsx b/superset-frontend/packages/superset-ui-core/src/components/Modal/Modal.resize.test.tsx new file mode 100644 index 00000000000..7252b4a93f3 --- /dev/null +++ b/superset-frontend/packages/superset-ui-core/src/components/Modal/Modal.resize.test.tsx @@ -0,0 +1,413 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +import { render, screen, act } from '@superset-ui/core/spec'; +import { Modal } from './Modal'; + +// Track what props each mock receives +let lastDraggableProps: any = null; +let lastResizableProps: any = null; + +jest.mock('react-draggable', () => { + const MockDraggable = (props: any) => { + lastDraggableProps = props; + const { children, disabled, position, handle } = props; + return ( + <div + data-test="mock-draggable" + data-position-x={position?.x ?? 0} + data-position-y={position?.y ?? 0} + data-disabled={disabled} + data-handle={handle} + > + {children} + </div> + ); + }; + MockDraggable.displayName = 'MockDraggable'; + return { __esModule: true, default: MockDraggable }; +}); + +jest.mock('re-resizable', () => { + const MockResizable = (props: any) => { + lastResizableProps = props; + const { children, className, enable } = props; + return ( + <div + className={className} + data-test="mock-resizable" + data-enable={JSON.stringify(enable)} + > + {children} + </div> + ); + }; + MockResizable.displayName = 'MockResizable'; + return { Resizable: MockResizable }; +}); + +// jsdom has no layout engine: the viewport reports 0x0 and +// getBoundingClientRect returns all zeros, which would make every viewport +// bound degenerate (left === right). Stub a realistic viewport and modal rect +// so the bounds math is meaningful. +const VIEWPORT_WIDTH = 1920; +const VIEWPORT_HEIGHT = 1080; +const MODAL_RECT = { left: 660, top: 240, width: 600, height: 540 }; + +const makeRect = (overrides: Partial<DOMRect> = {}): DOMRect => + ({ + x: MODAL_RECT.left, + y: MODAL_RECT.top, + left: MODAL_RECT.left, + top: MODAL_RECT.top, + width: MODAL_RECT.width, + height: MODAL_RECT.height, + right: MODAL_RECT.left + MODAL_RECT.width, + bottom: MODAL_RECT.top + MODAL_RECT.height, + ...overrides, + toJSON: () => ({}), + }) as DOMRect; + +beforeEach(() => { + lastDraggableProps = null; + lastResizableProps = null; + jest + .spyOn(document.documentElement, 'clientWidth', 'get') + .mockReturnValue(VIEWPORT_WIDTH); + jest + .spyOn(document.documentElement, 'clientHeight', 'get') + .mockReturnValue(VIEWPORT_HEIGHT); + jest + .spyOn(Element.prototype, 'getBoundingClientRect') + .mockReturnValue(makeRect()); +}); + +afterEach(() => { + jest.restoreAllMocks(); +}); + +const renderModal = (props: Record<string, any> = {}) => + render( + <Modal + show + onHide={jest.fn()} + title="Test Modal" + resizable + draggable + {...props} + > + <div>Modal content</div> + </Modal>, + ); + +describe('Modal resizable config', () => { + test('preserves default enable handles when resizableConfig has no enable key', () => { + renderModal({ + resizableConfig: { + minHeight: 500, + minWidth: 400, + defaultSize: { width: 'auto', height: '75vh' }, + }, + }); + + const enable = JSON.parse( + screen.getByTestId('mock-resizable').getAttribute('data-enable') || '{}', + ); + + expect(enable).toEqual({ + bottom: true, + bottomLeft: false, + bottomRight: true, + left: false, + top: false, + topLeft: false, + topRight: false, + right: true, + }); + }); + + test('uses default enable handles when no resizableConfig is provided', () => { + renderModal(); + + const enable = JSON.parse( + screen.getByTestId('mock-resizable').getAttribute('data-enable') || '{}', + ); + + expect(enable).toEqual({ + bottom: true, + bottomLeft: false, + bottomRight: true, + left: false, + top: false, + topLeft: false, + topRight: false, + right: true, + }); + }); + + test('uses caller-provided enable handles when explicitly set', () => { + renderModal({ + resizableConfig: { + enable: { + bottom: true, + bottomLeft: true, + bottomRight: true, + left: true, + top: true, + topLeft: true, + topRight: true, + right: true, + }, + }, + }); + + const enable = JSON.parse( + screen.getByTestId('mock-resizable').getAttribute('data-enable') || '{}', + ); + + expect(enable.top).toBe(true); + expect(enable.left).toBe(true); + expect(enable.topLeft).toBe(true); + }); + + test('merges resizableConfig properties with defaults', () => { + renderModal({ + resizableConfig: { + minHeight: 600, + }, + }); + + const enable = JSON.parse( + screen.getByTestId('mock-resizable').getAttribute('data-enable') || '{}', + ); + + // enable should remain restricted to the defaults since caller omitted it + expect(enable.bottom).toBe(true); + expect(enable.right).toBe(true); + expect(enable.top).toBe(false); + }); +}); + +describe('Modal controlled Draggable', () => { + test('passes position to Draggable with initial {x:0, y:0}', () => { + renderModal(); + + expect( + screen.getByTestId('mock-draggable').getAttribute('data-position-x'), + ).toBe('0'); + expect( + screen.getByTestId('mock-draggable').getAttribute('data-position-y'), + ).toBe('0'); + }); + + test('Draggable is enabled and gated on the title bar trigger', () => { + renderModal(); + + const draggable = screen.getByTestId('mock-draggable'); + expect(draggable.getAttribute('data-disabled')).toBe('false'); + expect(draggable.getAttribute('data-handle')).toBe('.draggable-trigger'); + }); + + test('onResize callback is passed to Resizable', () => { + renderModal(); + + expect(lastResizableProps).not.toBeNull(); + expect(typeof lastResizableProps.onResize).toBe('function'); + }); + + test('onDrag callback is passed to Draggable', () => { + renderModal(); + + expect(lastDraggableProps).not.toBeNull(); + expect(typeof lastDraggableProps.onDrag).toBe('function'); + }); + + test('position prop is passed to Draggable', () => { + renderModal(); + + expect(lastDraggableProps).not.toBeNull(); + expect(lastDraggableProps.position).toEqual({ x: 0, y: 0 }); + }); + + test('position resets to {0,0} after close and reopen', () => { + const onHide = jest.fn(); + const { rerender } = render( + <Modal show onHide={onHide} title="Test" resizable draggable> + <div>Content</div> + </Modal>, + ); + + // Simulate a drag by calling onDrag + act(() => { + lastDraggableProps.onDrag(null, { x: 100, y: 200 }); + }); + expect(lastDraggableProps.position).toEqual({ x: 100, y: 200 }); + + // Close the modal + act(() => { + rerender( + <Modal show={false} onHide={onHide} title="Test" resizable draggable> + <div>Content</div> + </Modal>, + ); + }); + + // Reopen the modal — position should have been reset + act(() => { + rerender( + <Modal show onHide={onHide} title="Test" resizable draggable> + <div>Content</div> + </Modal>, + ); + }); + + expect(lastDraggableProps.position).toEqual({ x: 0, y: 0 }); + }); + + test('shifts Draggable left when resizing from the left edge', () => { + renderModal(); + + act(() => { + lastResizableProps.onResizeStart(); + lastResizableProps.onResize({}, 'left', {}, { width: 50, height: 0 }); + }); + + // re-resizable grows the box away from its layout origin and never moves + // it, so anchoring the right edge means moving the modal left by the + // growth delta. + expect(lastDraggableProps.position).toEqual({ x: -50, y: 0 }); + }); + + test('shifts Draggable up when resizing from the top edge', () => { + renderModal(); + + act(() => { + lastResizableProps.onResizeStart(); + lastResizableProps.onResize({}, 'top', {}, { width: 0, height: 40 }); + }); + + expect(lastDraggableProps.position).toEqual({ x: 0, y: -40 }); + }); + + test('anchors both axes when resizing from the topLeft corner', () => { + renderModal(); + + act(() => { + lastResizableProps.onResizeStart(); + lastResizableProps.onResize({}, 'topLeft', {}, { width: 50, height: 40 }); + }); + + // Corner directions are camelCase; the left/top match must be + // case-insensitive or the x axis is never synced. + expect(lastDraggableProps.position).toEqual({ x: -50, y: -40 }); + }); + + test('handles cumulative onResize deltas idempotently', () => { + renderModal(); + + act(() => { + lastDraggableProps.onDrag(null, { x: 20, y: 30 }); + }); + act(() => { + lastResizableProps.onResizeStart(); + // re-resizable reports the total delta since the gesture started, + // once per mouse-move event. + lastResizableProps.onResize({}, 'left', {}, { width: 25, height: 0 }); + lastResizableProps.onResize({}, 'left', {}, { width: 50, height: 0 }); + lastResizableProps.onResize({}, 'left', {}, { width: 75, height: 0 }); + }); + + // Position is base - delta, not an accumulation of every event. + expect(lastDraggableProps.position).toEqual({ x: -55, y: 30 }); + }); + + test('does not move Draggable when resizing from the bottom-right corner', () => { + renderModal(); + + act(() => { + lastResizableProps.onResizeStart(); + lastResizableProps.onResize( + {}, + 'bottomRight', + {}, + { width: 30, height: 20 }, + ); + }); + + // Bottom/right handles grow away from the anchored top-left; position + // must stay untouched (no drift). + expect(lastDraggableProps.position).toEqual({ x: 0, y: 0 }); + }); + + test('allows resize shifts up to the exact viewport edge and clamps beyond it', () => { + renderModal(); + + act(() => { + lastResizableProps.onResizeStart(); + // MODAL_RECT.left is 660, so a 660px leftward shift lands the modal + // exactly on the viewport's left edge — the bound itself. + lastResizableProps.onResize({}, 'left', {}, { width: 660, height: 0 }); + }); + expect(lastDraggableProps.position).toEqual({ x: -660, y: 0 }); + + act(() => { + // One pixel beyond the edge is clamped back to the bound. + lastResizableProps.onResize({}, 'left', {}, { width: 661, height: 0 }); + }); + expect(lastDraggableProps.position).toEqual({ x: -660, y: 0 }); + }); + + test('clamps resize shifts that would push the modal out of the viewport', () => { + // Modal flush against the left edge of the screen. + jest + .spyOn(Element.prototype, 'getBoundingClientRect') + .mockReturnValue(makeRect({ left: 0, x: 0, right: MODAL_RECT.width })); + + renderModal(); + + act(() => { + lastResizableProps.onResizeStart(); + lastResizableProps.onResize({}, 'left', {}, { width: 400, height: 0 }); + lastResizableProps.onResize({}, 'top', {}, { width: 0, height: 800 }); + }); + + // The raw shifts would be (-400, -800); each axis is clamped at the + // corresponding viewport edge instead. + expect(lastDraggableProps.position).toEqual({ + x: 0, + y: -MODAL_RECT.top, + }); + }); + + test('repositions on resize even when dragging is disabled', () => { + renderModal({ draggable: false }); + + expect( + screen.getByTestId('mock-draggable').getAttribute('data-disabled'), + ).toBe('true'); + + act(() => { + lastResizableProps.onResizeStart(); + lastResizableProps.onResize({}, 'topLeft', {}, { width: 80, height: 60 }); + }); + + // Anchoring works without a drag handle, and the shift stays within + // the captured viewport bounds. + expect(lastDraggableProps.position).toEqual({ x: -80, y: -60 }); + }); +}); diff --git a/superset-frontend/packages/superset-ui-core/src/components/Modal/Modal.tsx b/superset-frontend/packages/superset-ui-core/src/components/Modal/Modal.tsx index 3e6bfb76379..d761beab308 100644 --- a/superset-frontend/packages/superset-ui-core/src/components/Modal/Modal.tsx +++ b/superset-frontend/packages/superset-ui-core/src/components/Modal/Modal.tsx @@ -19,6 +19,7 @@ import { isValidElement, cloneElement, + useEffect, useMemo, useRef, useState, @@ -291,7 +292,25 @@ const CustomModal = ({ [bodyStyle, stylesProp], ); const draggableRef = useRef<HTMLDivElement>(null); + // Modal position at the start of a resize gesture; see onResize below. + const resizeBasePositionRef = useRef<{ x: number; y: number } | null>(null); + // Viewport bounds captured at resize start; see onResize below. + const resizeBoundsRef = useRef<DraggableBounds>({}); const [bounds, setBounds] = useState<DraggableBounds>({}); + // Controlled position for react-draggable. Keeping Draggable in controlled + // mode lets us sync position with re-resizable's onResize so that resizing + // from top/left edges correctly repositions the modal (anchoring the + // opposite corner) instead of fighting over position. + const [position, setPosition] = useState({ x: 0, y: 0 }); + + // Reset drag position when modal closes so it doesn't retain the + // previous offset on next open. + useEffect(() => { + if (!show) { + setPosition({ x: 0, y: 0 }); + } + }, [show]); + const theme = useTheme(); const handleOnHide = () => { @@ -339,18 +358,27 @@ const CustomModal = ({ const modalWidth = width || (responsive ? '100vw' : '600px'); const shouldShowMask = !(resizable || draggable); - const onDragStart = (_: DraggableEvent, uiData: DraggableData) => { + // Computes DraggableBounds confining the modal to the viewport, expressed + // relative to the wrapper's current position (x, y). Shared by drag and + // resize so both gestures respect the same on-screen limits. + const getViewportBounds = (x: number, y: number): DraggableBounds => { const { clientWidth, clientHeight } = document.documentElement; const targetRect = draggableRef?.current?.getBoundingClientRect(); - if (targetRect) { - setBounds({ - left: -targetRect?.left + uiData?.x, - right: clientWidth - (targetRect?.right - uiData?.x), - top: -targetRect?.top + uiData?.y, - bottom: clientHeight - (targetRect?.bottom - uiData?.y), - }); + if (!targetRect) { + return {}; } + + return { + left: -targetRect.left + x, + right: clientWidth - (targetRect.right - x), + top: -targetRect.top + y, + bottom: clientHeight - (targetRect.bottom - y), + }; + }; + + const onDragStart = (_: DraggableEvent, uiData: DraggableData) => { + setBounds(getViewportBounds(uiData?.x ?? 0, uiData?.y ?? 0)); }; const getResizableConfig = useMemo( @@ -396,12 +424,59 @@ const CustomModal = ({ // `draggableConfig.disabled` is still honored. disabled={!draggable || !!draggableConfig?.disabled} handle={draggable ? '.draggable-trigger' : undefined} + // Controlled position and its drag sync are applied after the + // spread too: the resize anchoring below depends on owning both. + position={position} + onDrag={(_, data) => setPosition({ x: data.x, y: data.y })} // Pass nodeRef so react-draggable does not fall back to // ReactDOM.findDOMNode (deprecated in React 18+ Strict Mode). nodeRef={draggableRef} > {resizable ? ( - <Resizable className="resizable" {...getResizableConfig}> + <Resizable + className="resizable" + {...getResizableConfig} + onResizeStart={() => { + resizeBasePositionRef.current = position; + // Capture bounds here: react-draggable only clamps during + // drag gestures, so onResize must clamp its own setPosition + // calls or a large resize from the top/left could push the + // modal off-screen with no way to drag it back (e.g. on a + // resizable modal with draggable disabled). + resizeBoundsRef.current = getViewportBounds( + position.x, + position.y, + ); + }} + onResize={(_e, direction, _ref, delta) => { + // When resizing from the top or left, the opposite corner + // should stay anchored. re-resizable adjusts size but + // cannot move the Draggable wrapper, so we sync position. + // delta is cumulative from gesture start, so the target + // position must be derived from the position captured at + // resize start, not accumulated per event. Direction + // strings are camelCase ("topLeft"), so match + // case-insensitively. + const base = resizeBasePositionRef.current ?? position; + const dir = direction.toLowerCase(); + const { + left = -Infinity, + right = Infinity, + top = -Infinity, + bottom = Infinity, + } = resizeBoundsRef.current; + setPosition(prev => ({ + // Only the axis matching the resized edge moves; clamp it + // so the modal cannot be pushed outside the viewport. + x: dir.includes('left') + ? Math.min(Math.max(base.x - delta.width, left), right) + : prev.x, + y: dir.includes('top') + ? Math.min(Math.max(base.y - delta.height, top), bottom) + : prev.y, + })); + }} + > <div className="resizable-wrapper" ref={draggableRef}> {modal} </div>
