EnxDev commented on code in PR #31452: URL: https://github.com/apache/superset/pull/31452#discussion_r1907283144
########## superset-frontend/src/dashboard/components/resizable/ResizableContainer.tsx: ########## @@ -0,0 +1,289 @@ +/** + * 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 { useState } from 'react'; +import { ResizeCallback, ResizeStartCallback, Resizable } from 're-resizable'; +import cx from 'classnames'; +import { css, styled } from '@superset-ui/core'; + +import { + RightResizeHandle, + BottomResizeHandle, + BottomRightResizeHandle, +} from './ResizableHandle'; +import resizableConfig from '../../util/resizableConfig'; +import { GRID_BASE_UNIT, GRID_GUTTER_SIZE } from '../../util/constants'; + +const proxyToInfinity = Number.MAX_VALUE; + +export interface ResizableContainerProps { + id: string; + children?: object; + adjustableWidth?: boolean; + adjustableHeight?: boolean; + gutterWidth?: number; + widthStep?: number; + heightStep?: number; + widthMultiple: number; + heightMultiple: number; + minWidthMultiple?: number; + maxWidthMultiple?: number; + minHeightMultiple?: number; + maxHeightMultiple?: number; + staticHeight?: number; + staticHeightMultiple?: number; + staticWidth?: number; + staticWidthMultiple?: number; + onResizeStart?: ResizeStartCallback; + onResize?: ResizeCallback; + onResizeStop?: ResizeCallback; + editMode: boolean; +} + +// because columns are not multiples of a single variable (width = n*cols + (n-1) * gutters) +// we snap to the base unit and then snap to _actual_ column multiples on stop +const SNAP_TO_GRID: [number, number] = [GRID_BASE_UNIT, GRID_BASE_UNIT]; +const HANDLE_CLASSES = { + right: 'resizable-container-handle--right', + bottom: 'resizable-container-handle--bottom', +}; +// @ts-ignore +const StyledResizable = styled(Resizable)` + ${({ theme }) => css` + &.resizable-container { + background-color: transparent; + position: relative; + + /* re-resizable sets an empty div to 100% width and height, which doesn't + play well with many 100% height containers we need */ + + & ~ div { + width: auto !important; + height: auto !important; + } + } + + &.resizable-container--resizing { + /* after ensures border visibility on top of any children */ + + &:after { + content: ''; + position: absolute; + top: 0; + left: 0; + width: 100%; + height: 100%; + box-shadow: inset 0 0 0 2px ${theme.colors.primary.base}; + } + + & > span .resize-handle { + border-color: ${theme.colors.primary.base}; + } + } + + .resize-handle { + opacity: 0; + z-index: 10; + + &--bottom-right { + position: absolute; + border-right: 1px solid ${theme.colors.text.label}; + border-bottom: 1px solid ${theme.colors.text.label}; + right: ${theme.gridUnit * 4}px; + bottom: ${theme.gridUnit * 4}px; + width: ${theme.gridUnit * 2}px; + height: ${theme.gridUnit * 2}px; + } + + &--right { + width: ${theme.gridUnit / 2}px; + height: ${theme.gridUnit * 5}px; + right: ${theme.gridUnit}px; + top: 50%; + transform: translate(0, -50%); + position: absolute; + border-left: 1px solid ${theme.colors.text.label}; + border-right: 1px solid ${theme.colors.text.label}; + } + + &--bottom { + height: ${theme.gridUnit / 2}px; + width: ${theme.gridUnit * 5}px; + bottom: ${theme.gridUnit}px; + left: 50%; + transform: translate(-50%); + position: absolute; + border-top: 1px solid ${theme.colors.text.label}; + border-bottom: 1px solid ${theme.colors.text.label}; + } + } + `} + + &.resizable-container:hover .resize-handle, + &.resizable-container--resizing .resize-handle { + opacity: 1; + } + + .dragdroppable-column & .resizable-container-handle--right { + /* override the default because the inner column's handle's mouse target is very small */ + right: 0 !important; + } + + & .resizable-container-handle--bottom { + bottom: 0 !important; + } +`; + +export default function ResizableContainer({ + id, + children, + widthMultiple, + heightMultiple, + staticHeight, + staticHeightMultiple, + staticWidth, + staticWidthMultiple, + onResizeStop, + onResize, + onResizeStart, + editMode, + adjustableWidth = true, + adjustableHeight = true, + gutterWidth = GRID_GUTTER_SIZE, + widthStep = GRID_BASE_UNIT, + heightStep = GRID_BASE_UNIT, + minWidthMultiple = 1, + maxWidthMultiple = proxyToInfinity, + minHeightMultiple = 1, + maxHeightMultiple = proxyToInfinity, +}: ResizableContainerProps) { + const [isResizing, setIsResizing] = useState<boolean>(false); + + const handleResize: ResizeCallback = ( + event, + direction, + elementRef, + delta: { width: number; height: number }, + ) => { + if (onResize) onResize(event, direction, elementRef, delta); + }; + + const handleResizeStart: ResizeStartCallback = (e, dir, elementRef) => { + if (onResizeStart) onResizeStart(e, dir, elementRef); + setIsResizing(true); + }; + + const handleResizeStop: ResizeCallback = ( + event, + direction, + elementRef, + delta: { width: number; height: number }, + ) => { + if (onResizeStop) { + const nextWidthMultiple = + widthMultiple + Math.round(delta.width / (widthStep + gutterWidth)); + const nextHeightMultiple = + heightMultiple + Math.round(delta.height / heightStep); + + onResizeStop( + event, + direction, + elementRef, + { + width: adjustableWidth ? nextWidthMultiple : 0, + height: adjustableHeight ? nextHeightMultiple : 0, + }, + // @ts-ignore Review Comment: ResizeCallback does not expect the id, but in our case it is required until it is modified in handleResizeStop. Instead of using ResizeCallback, I can create a custom type if we think it might be useful. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
