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

Increment/Decrement Fractional Grid Tracks #6588

Merged
merged 2 commits into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 4 additions & 0 deletions editor/src/components/inspector/common/css-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,10 @@ const CSSNumberUnits: Array<CSSNumberUnit> = [
'%',
]

export function isFR(unit: CSSNumberUnit): unit is 'fr' {
return unit === 'fr'
}

export interface CSSNumber {
value: number
unit: CSSNumberUnit | null
Expand Down
76 changes: 51 additions & 25 deletions editor/src/uuiui/inputs/grid-expression-input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
import { jsx } from '@emotion/react'
import type { CSSProperties } from 'react'
import React from 'react'
import type { CSSNumberUnit } from '../../components/inspector/common/css-utils'
import {
cssKeyword,
gridDimensionsAreEqual,
isFR,
isGridCSSNumber,
isValidGridDimensionKeyword,
parseCSSNumber,
Expand All @@ -28,6 +30,9 @@ import { Icons, SmallerIcons } from '../icons'
import { NO_OP } from '../../core/shared/utils'
import { unless } from '../../utils/react-conditionals'
import { useColorTheme, UtopiaTheme } from '../styles/theme'
import type { Optic } from '../../core/shared/optics/optics'
import { fromField, fromTypeGuard, notNull } from '../../core/shared/optics/optic-creators'
import { exists, modify } from '../../core/shared/optics/optic-utilities'

interface GridExpressionInputProps {
testId: string
Expand Down Expand Up @@ -66,32 +71,53 @@ export const GridExpressionInput = React.memo(

const onKeyDown = React.useCallback(
(e: React.KeyboardEvent) => {
if (e.key !== 'Enter') {
return
switch (e.key) {
case 'Enter':
if (isValidGridDimensionKeyword(printValue)) {
return onUpdateNumberOrKeyword(cssKeyword(printValue))
}

const defaultUnit = isGridCSSNumber(value) ? value.value.unit : 'px'
const maybeNumber = parseCSSNumber(printValue, 'AnyValid', defaultUnit)
if (isRight(maybeNumber)) {
return onUpdateNumberOrKeyword(maybeNumber.value)
}

const maybeMinmax = parseGridCSSMinmaxOrRepeat(printValue)
if (maybeMinmax != null) {
return onUpdateDimension({
...maybeMinmax,
lineName: value.lineName,
} as GridDimension)
seanparsons marked this conversation as resolved.
Show resolved Hide resolved
}

if (printValue === '') {
return onUpdateNumberOrKeyword(cssKeyword('auto'))
}

setPrintValue(stringifyGridDimension(value))
break
case 'ArrowUp':
case 'ArrowDown':
e.preventDefault()
const gridNumberValueOptic: Optic<GridDimension, CSSNumber> = fromTypeGuard(
isGridCSSNumber,
).compose(fromField('value'))
const valueUnitOptic: Optic<GridDimension, 'fr'> = gridNumberValueOptic
.compose(fromField('unit'))
.compose(notNull())
.compose(fromTypeGuard(isFR))
const gridNumberNumberOptic: Optic<GridDimension, number> =
gridNumberValueOptic.compose(fromField('value'))
if (exists(valueUnitOptic, value)) {
function updateFractional(fractionalValue: number): number {
return fractionalValue + (e.key === 'ArrowUp' ? 0.1 : -0.1)
seanparsons marked this conversation as resolved.
Show resolved Hide resolved
}
const updatedDimension = modify(gridNumberNumberOptic, updateFractional, value)
onUpdateDimension(updatedDimension)
}
break
}
if (isValidGridDimensionKeyword(printValue)) {
return onUpdateNumberOrKeyword(cssKeyword(printValue))
}

const defaultUnit = isGridCSSNumber(value) ? value.value.unit : 'px'
const maybeNumber = parseCSSNumber(printValue, 'AnyValid', defaultUnit)
if (isRight(maybeNumber)) {
return onUpdateNumberOrKeyword(maybeNumber.value)
}

const maybeMinmax = parseGridCSSMinmaxOrRepeat(printValue)
if (maybeMinmax != null) {
return onUpdateDimension({
...maybeMinmax,
lineName: value.lineName,
} as GridDimension)
}

if (printValue === '') {
return onUpdateNumberOrKeyword(cssKeyword('auto'))
}

setPrintValue(stringifyGridDimension(value))
},
[printValue, onUpdateNumberOrKeyword, onUpdateDimension, value],
)
Expand Down
Loading