Skip to content

Commit

Permalink
Don't allow negative values when resizing grids (#6407)
Browse files Browse the repository at this point in the history
**Problem:**

Resizing a grid col/row can lead to setting negative values.

**Fix:**

Clamp post-drag values to be ≥ 0.

Fixes #6406
  • Loading branch information
ruggi authored Sep 24, 2024
1 parent 342d61c commit c949425
Showing 1 changed file with 10 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -157,24 +157,21 @@ export const resizeGridStrategy: CanvasStrategyFactory = (
const precision = modifiers.cmd ? 'coarse' : 'precise'
const areaName = mergedValues.dimensions[control.columnOrRow]?.areaName ?? null

const newValue = gridCSSNumber(
cssNumber(
newResizedValue(
mergedValue.value,
getNewDragValue(dragAmount, isFractional, calculatedValue, mergedValue),
precision,
isFractional,
),
mergedUnit.value,
const newValue = Math.max(
0,
newResizedValue(
mergedValue.value,
getNewDragValue(dragAmount, isFractional, calculatedValue, mergedValue),
precision,
isFractional,
),
areaName,
)

const newDimensions = replaceGridTemplateDimensionAtIndex(
originalValues.dimensions,
expandedOriginalValues,
control.columnOrRow,
newValue,
gridCSSNumber(cssNumber(newValue, mergedUnit.value), areaName),
)

const propertyValueAsString = printArrayGridDimensions(newDimensions)
Expand Down Expand Up @@ -202,7 +199,7 @@ function getNewDragValue(
isFractional: boolean,
possibleCalculatedValue: Either<string, number>,
mergedValue: Either<string, number>,
) {
): number {
if (!isFractional) {
return dragAmount
}
Expand All @@ -219,8 +216,7 @@ function getNewDragValue(
const calculatedValue = possibleCalculatedValue.value
const perPointOne =
mergedFractionalValue == 0 ? 10 : (calculatedValue / mergedFractionalValue) * 0.1
const newValue = roundToNearestWhole((dragAmount / perPointOne) * 10) / 10
return newValue
return roundToNearestWhole((dragAmount / perPointOne) * 10) / 10
}

function newResizedValue(
Expand Down

0 comments on commit c949425

Please sign in to comment.