Skip to content

Commit

Permalink
Clamp minimum gap for flex/grid elements (#6441)
Browse files Browse the repository at this point in the history
**Problem:**

The grid and flex gap inspector controls can go negative, either by
scrubbing as well as direct input.

**Fix:**

1. Add `minimum={0}` to those number inputs so the scrubbing cannot go
lower than 0
2. Clamp the value calculated in the number input so it stays between
the min/max bounds.

Fixes #6440
  • Loading branch information
ruggi authored Oct 1, 2024
1 parent ddcec3d commit 161179c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
6 changes: 6 additions & 0 deletions editor/src/components/inspector/flex-section.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -887,6 +887,8 @@ const GapRowColumnControl = React.memo(() => {
<NumberInput
value={columnGap.value}
numberType={'Length'}
minimum={0}
clampOnSubmitValue={true}
onSubmitValue={onSubmitUnifiedValue}
onTransientSubmitValue={onSubmitUnifiedValue}
onForcedSubmitValue={onSubmitUnifiedValue}
Expand All @@ -903,6 +905,8 @@ const GapRowColumnControl = React.memo(() => {
<NumberInput
value={columnGap.value}
numberType={'Length'}
minimum={0}
clampOnSubmitValue={true}
onSubmitValue={onSubmitSplitValue('columnGap')}
onTransientSubmitValue={onSubmitSplitValue('columnGap')}
onForcedSubmitValue={onSubmitSplitValue('columnGap')}
Expand All @@ -916,6 +920,8 @@ const GapRowColumnControl = React.memo(() => {
<NumberInput
value={rowGap.value}
numberType={'Length'}
minimum={0}
clampOnSubmitValue={true}
onSubmitValue={onSubmitSplitValue('rowGap')}
onTransientSubmitValue={onSubmitSplitValue('rowGap')}
onForcedSubmitValue={onSubmitSplitValue('rowGap')}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,8 @@ export const FlexGapControl = React.memo(() => {
testId='flex.container.gap'
key='flex.container.gap'
value={value}
minimum={0}
clampOnSubmitValue={true}
onSubmitValue={wrappedOnSubmitValue}
onTransientSubmitValue={wrappedOnTransientSubmitValue}
onForcedSubmitValue={wrappedOnSubmitValue}
Expand Down
15 changes: 12 additions & 3 deletions editor/src/uuiui/inputs/number-input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,10 @@ import type {
} from '../../components/inspector/controls/control'
import type { Either } from '../../core/shared/either'
import { isLeft, mapEither } from '../../core/shared/either'
import { clampValue, point } from '../../core/shared/math-utils'
import { clampValue } from '../../core/shared/math-utils'
import { memoize } from '../../core/shared/memoize'
import type { ControlStyles } from '../../uuiui-deps'
import { getControlStyles, CSSCursor } from '../../uuiui-deps'
import type { IcnProps } from '../icn'
import { Icn } from '../icn'
import { useColorTheme, UtopiaTheme } from '../styles/theme'
import { FlexRow } from '../widgets/layout/flex-row'
Expand Down Expand Up @@ -141,6 +140,7 @@ export interface NumberInputOptions {
pasteHandler?: boolean
descriptionLabel?: string
disableScrubbing?: boolean
clampOnSubmitValue?: boolean
}

export interface AbstractNumberInputProps<T extends CSSNumber | number>
Expand Down Expand Up @@ -190,6 +190,7 @@ export const NumberInput = React.memo<NumberInputProps>(
invalid,
pasteHandler,
disableScrubbing = false,
clampOnSubmitValue,
}) => {
const ref = React.useRef<HTMLInputElement>(null)
const colorTheme = useColorTheme()
Expand Down Expand Up @@ -511,7 +512,12 @@ export const NumberInput = React.memo<NumberInputProps>(
if (isLeft(parsed)) {
return unknownInputValue(displayValue)
}
return parsed.value
return clampOnSubmitValue
? {
...parsed.value,
value: clampValue(parsed.value.value, minimum, maximum),
}
: parsed.value
}

const newValue = getNewValue()
Expand Down Expand Up @@ -540,6 +546,9 @@ export const NumberInput = React.memo<NumberInputProps>(
updateValue,
value,
displayValue,
minimum,
maximum,
clampOnSubmitValue,
],
)

Expand Down

0 comments on commit 161179c

Please sign in to comment.