Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fallback striped area size when resizing grid cols/rows #6543

Merged
merged 2 commits into from
Oct 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading