Skip to content

Commit

Permalink
Fallback striped area size when resizing grid cols/rows (#6543)
Browse files Browse the repository at this point in the history
**Problem:**

When resizing a grid col/row if the counterpart has no size, the striped
area collapses to `0` pixels.

**Fix:**

Fallback to the frame size if the counterpart axis has no size. Also,
disable the mouse event if the dimension is not set.

| Before | After |
|------|--------|
| ![Kapture 2024-10-15 at 21 19
53](https://github.com/user-attachments/assets/e9bdcceb-07a5-4b70-9f31-7e6d8dc3b6fe)
| ![Kapture 2024-10-15 at 21 19
37](https://github.com/user-attachments/assets/dfa0b547-e7f7-48a5-a2ae-b3c173640ba8)
|


Fixes #6542
  • Loading branch information
ruggi authored Oct 16, 2024
1 parent bb8596f commit 9c831a1
Showing 1 changed file with 34 additions and 14 deletions.
48 changes: 34 additions & 14 deletions editor/src/components/canvas/controls/grid-controls.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,9 @@ import { windowToCanvasCoordinates } from '../dom-lookup'
import type { Axis } from '../gap-utils'
import { useCanvasAnimation } from '../ui-jsx-canvas-renderer/animation-context'
import { CanvasOffsetWrapper } from './canvas-offset-wrapper'
import type {
GridControlsProps,
GridData,
GridMeasurementHelperData,
} from './grid-controls-for-strategies'
import type { GridControlsProps, GridData } from './grid-controls-for-strategies'
import {
edgePositionToGridResizeEdge,
getNullableAutoOrTemplateBaseString,
GridCellTestId,
GridControlKey,
gridEdgeToEdgePosition,
Expand Down Expand Up @@ -148,8 +143,22 @@ const GridResizingControl = React.memo((props: GridResizingControlProps) => {
const dispatch = useDispatch()
const colorTheme = useColorTheme()

const canResize = React.useMemo(() => {
return (
props.fromPropsAxisValues?.type === 'DIMENSIONS' &&
props.fromPropsAxisValues.dimensions.length > 0
)
}, [props.fromPropsAxisValues])

const mouseDownHandler = React.useCallback(
(event: React.MouseEvent): void => {
event.stopPropagation()
event.preventDefault()

if (!canResize) {
return
}

function mouseUpHandler() {
setResizingIndex(null)
window.removeEventListener('mouseup', mouseUpHandler)
Expand All @@ -173,10 +182,8 @@ const GridResizingControl = React.memo((props: GridResizingControlProps) => {
),
),
])
event.stopPropagation()
event.preventDefault()
},
[canvasOffset, dispatch, props.axis, props.dimensionIndex, scale, setResizingIndex],
[canvasOffset, dispatch, props.axis, props.dimensionIndex, scale, setResizingIndex, canResize],
)

const { maybeClearHighlightsOnHoverEnd } = useMaybeHighlightElement()
Expand Down Expand Up @@ -229,7 +236,9 @@ const GridResizingControl = React.memo((props: GridResizingControlProps) => {
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
cursor: gridEdgeToCSSCursor(props.axis === 'column' ? 'column-start' : 'row-start'),
cursor: canResize
? gridEdgeToCSSCursor(props.axis === 'column' ? 'column-start' : 'row-start')
: 'default',
pointerEvents: 'initial',
}}
onMouseDown={mouseDownHandler}
Expand Down Expand Up @@ -452,16 +461,23 @@ export const GridRowColumnResizingControlsComponent = ({
}: GridRowColumnResizingControlsProps) => {
const grids = useGridData([target])

function getStripedAreaLength(template: GridAutoOrTemplateBase | null, gap: number) {
function getStripedAreaLength(
template: GridAutoOrTemplateBase | null,
gap: number,
): number | null {
if (template?.type !== 'DIMENSIONS') {
return null
}
return template.dimensions.reduce((acc, curr, index) => {
const fromDimensions = template.dimensions.reduce((acc, curr, index) => {
if (curr.type === 'NUMBER') {
return acc + curr.value.value + (index > 0 ? gap : 0)
}
return acc
}, 0)
if (fromDimensions <= 0) {
return null
}
return fromDimensions
}

const scale = useEditorState(
Expand Down Expand Up @@ -514,7 +530,9 @@ export const GridRowColumnResizingControlsComponent = ({
axis={'column'}
gap={grid.columnGap ?? grid.gap}
padding={grid.padding}
stripedAreaLength={getStripedAreaLength(grid.gridTemplateRows, grid.gap ?? 0)}
stripedAreaLength={
getStripedAreaLength(grid.gridTemplateRows, grid.gap ?? 0) ?? grid.frame.height
}
alignContent={grid.justifyContent}
justifyContent={grid.alignContent}
/>
Expand All @@ -530,7 +548,9 @@ export const GridRowColumnResizingControlsComponent = ({
axis={'row'}
gap={grid.rowGap ?? grid.gap}
padding={grid.padding}
stripedAreaLength={getStripedAreaLength(grid.gridTemplateColumns, grid.gap ?? 0)}
stripedAreaLength={
getStripedAreaLength(grid.gridTemplateColumns, grid.gap ?? 0) ?? grid.frame.width
}
alignContent={grid.alignContent}
justifyContent={grid.justifyContent}
/>
Expand Down

0 comments on commit 9c831a1

Please sign in to comment.