@@ -48,8 +48,6 @@ import {
48
48
gridCSSNumber ,
49
49
isCSSKeyword ,
50
50
isCSSNumber ,
51
- isEmptyInputValue ,
52
- isGridCSSNumber ,
53
51
printArrayGridDimensions ,
54
52
type GridDimension ,
55
53
} from './common/css-utils'
@@ -91,6 +89,12 @@ import {
91
89
} from '../canvas/controls/select-mode/select-mode-hooks'
92
90
import type { Axis } from '../canvas/gap-utils'
93
91
import { GridExpressionInput } from '../../uuiui/inputs/grid-expression-input'
92
+ import {
93
+ gridDimensionDropdownKeywords ,
94
+ parseGridDimensionInput ,
95
+ useGridExpressionInputFocused ,
96
+ } from './grid-helpers'
97
+ import { GridAutoColsOrRowsControl } from './grid-auto-cols-or-rows-control'
94
98
95
99
function getLayoutSystem (
96
100
layoutSystem : DetectedLayoutSystem | null | undefined ,
@@ -233,6 +237,12 @@ const TemplateDimensionControl = React.memo(
233
237
return fromProps
234
238
} , [ grid , axis , values ] )
235
239
240
+ const autoTemplate = React . useMemo ( ( ) => {
241
+ return axis === 'column'
242
+ ? grid . specialSizeMeasurements . containerGridPropertiesFromProps . gridAutoColumns
243
+ : grid . specialSizeMeasurements . containerGridPropertiesFromProps . gridAutoRows
244
+ } , [ grid , axis ] )
245
+
236
246
const onUpdateDimension = React . useCallback (
237
247
( index : number ) => ( newValue : GridDimension ) => {
238
248
if ( template ?. type !== 'DIMENSIONS' ) {
@@ -464,6 +474,20 @@ const TemplateDimensionControl = React.memo(
464
474
465
475
const dimensionsWithGeneratedIndexes = useGeneratedIndexesFromGridDimensions ( values )
466
476
477
+ const showAutoColsOrRows = React . useMemo ( ( ) => {
478
+ return (
479
+ template ?. type !== 'DIMENSIONS' ||
480
+ template . dimensions . length === 0 ||
481
+ ( autoTemplate ?. type === 'DIMENSIONS' &&
482
+ autoTemplate . dimensions . length > 0 &&
483
+ ! (
484
+ autoTemplate . dimensions . length === 1 &&
485
+ autoTemplate . dimensions [ 0 ] . type === 'KEYWORD' &&
486
+ autoTemplate . dimensions [ 0 ] . value . value === 'auto'
487
+ ) )
488
+ )
489
+ } , [ template , autoTemplate ] )
490
+
467
491
return (
468
492
< div
469
493
style = { {
@@ -492,19 +516,20 @@ const TemplateDimensionControl = React.memo(
492
516
opener = { openDropdown }
493
517
/>
494
518
) ) }
495
- < AutoColsOrRowsControl grid = { grid } axis = { axis } />
519
+ { when (
520
+ showAutoColsOrRows ,
521
+ < GridAutoColsOrRowsControl
522
+ grid = { grid }
523
+ axis = { axis }
524
+ label = { axis === 'column' ? 'Auto Cols' : 'Auto Rows' }
525
+ /> ,
526
+ ) }
496
527
</ div >
497
528
)
498
529
} ,
499
530
)
500
531
TemplateDimensionControl . displayName = 'TemplateDimensionControl'
501
532
502
- const gridDimensionDropdownKeywords = [
503
- { label : 'Auto' , value : cssKeyword ( 'auto' ) } ,
504
- { label : 'Min-Content' , value : cssKeyword ( 'min-content' ) } ,
505
- { label : 'Max-Content' , value : cssKeyword ( 'max-content' ) } ,
506
- ]
507
-
508
533
function AxisDimensionControl ( {
509
534
value,
510
535
index,
@@ -597,6 +622,7 @@ function AxisDimensionControl({
597
622
onFocus = { gridExpressionInputFocused . onFocus }
598
623
onBlur = { gridExpressionInputFocused . onBlur }
599
624
keywords = { gridDimensionDropdownKeywords }
625
+ defaultValue = { gridCSSKeyword ( cssKeyword ( 'auto' ) , null ) }
600
626
/>
601
627
{ when (
602
628
( isHovered && ! gridExpressionInputFocused . focused ) || isOpen ,
@@ -1090,106 +1116,3 @@ function useGeneratedIndexesFromGridDimensions(
1090
1116
return result
1091
1117
} , [ dimensions ] )
1092
1118
}
1093
-
1094
- const useGridExpressionInputFocused = ( ) => {
1095
- const [ focused , setFocused ] = React . useState ( false )
1096
- const onFocus = React . useCallback ( ( ) => setFocused ( true ) , [ ] )
1097
- const onBlur = React . useCallback ( ( ) => setFocused ( false ) , [ ] )
1098
- return { focused, onFocus, onBlur }
1099
- }
1100
-
1101
- function parseGridDimensionInput (
1102
- value : UnknownOrEmptyInput < CSSNumber | CSSKeyword < ValidGridDimensionKeyword > > ,
1103
- currentValue : GridDimension | null ,
1104
- ) {
1105
- if ( isCSSNumber ( value ) ) {
1106
- const maybeUnit =
1107
- currentValue != null && isGridCSSNumber ( currentValue ) ? currentValue . value . unit : null
1108
- return gridCSSNumber (
1109
- cssNumber ( value . value , value . unit ?? maybeUnit ) ,
1110
- currentValue ?. areaName ?? null ,
1111
- )
1112
- } else if ( isCSSKeyword ( value ) ) {
1113
- return gridCSSKeyword ( value , currentValue ?. areaName ?? null )
1114
- } else if ( isEmptyInputValue ( value ) ) {
1115
- return gridCSSKeyword ( cssKeyword ( 'auto' ) , currentValue ?. areaName ?? null )
1116
- } else {
1117
- return null
1118
- }
1119
- }
1120
-
1121
- const AutoColsOrRowsControl = React . memo (
1122
- ( props : { axis : 'column' | 'row' ; grid : ElementInstanceMetadata } ) => {
1123
- const value = React . useMemo ( ( ) => {
1124
- const template = props . grid . specialSizeMeasurements . containerGridPropertiesFromProps
1125
- const data = props . axis === 'column' ? template . gridAutoColumns : template . gridAutoRows
1126
- if ( data ?. type !== 'DIMENSIONS' ) {
1127
- return null
1128
- }
1129
- return data . dimensions [ 0 ]
1130
- } , [ props . grid , props . axis ] )
1131
-
1132
- const dispatch = useDispatch ( )
1133
-
1134
- const onUpdateDimension = React . useCallback (
1135
- ( newDimension : GridDimension ) => {
1136
- dispatch ( [
1137
- applyCommandsAction ( [
1138
- setProperty (
1139
- 'always' ,
1140
- props . grid . elementPath ,
1141
- PP . create ( 'style' , props . axis === 'column' ? 'gridAutoColumns' : 'gridAutoRows' ) ,
1142
- printArrayGridDimensions ( [ newDimension ] ) ,
1143
- ) ,
1144
- ] ) ,
1145
- ] )
1146
- } ,
1147
- [ props . grid , props . axis , dispatch ] ,
1148
- )
1149
-
1150
- const onUpdateNumberOrKeyword = React . useCallback (
1151
- ( newValue : UnknownOrEmptyInput < CSSNumber | CSSKeyword < ValidGridDimensionKeyword > > ) => {
1152
- const parsed = parseGridDimensionInput ( newValue , null )
1153
- if ( parsed == null ) {
1154
- return
1155
- }
1156
- onUpdateDimension ( parsed )
1157
- } ,
1158
- [ onUpdateDimension ] ,
1159
- )
1160
-
1161
- const autoColsOrRowsValueFocused = useGridExpressionInputFocused ( )
1162
-
1163
- return (
1164
- < div
1165
- style = { {
1166
- display : 'grid' ,
1167
- gridAutoFlow : 'column' ,
1168
- alignItems : 'center' ,
1169
- gap : 6 ,
1170
- gridTemplateColumns : autoColsOrRowsValueFocused . focused
1171
- ? '40px auto'
1172
- : `40px auto ${ UtopiaTheme . layout . inputHeight . default } px` ,
1173
- gridTemplateRows : '1fr' ,
1174
- width : '100%' ,
1175
- } }
1176
- >
1177
- < div > Default</ div >
1178
- < GridExpressionInput
1179
- testId = { GridAutoColsOrRowsControlTestId ( props . axis ) }
1180
- value = { value ?? gridCSSKeyword ( cssKeyword ( 'auto' ) , null ) }
1181
- onUpdateNumberOrKeyword = { onUpdateNumberOrKeyword }
1182
- onUpdateDimension = { onUpdateDimension }
1183
- onFocus = { autoColsOrRowsValueFocused . onFocus }
1184
- onBlur = { autoColsOrRowsValueFocused . onBlur }
1185
- keywords = { gridDimensionDropdownKeywords }
1186
- />
1187
- </ div >
1188
- )
1189
- } ,
1190
- )
1191
- AutoColsOrRowsControl . displayName = 'AutoColsOrRowsControl'
1192
-
1193
- export function GridAutoColsOrRowsControlTestId ( axis : 'column' | 'row' ) : string {
1194
- return `grid-template-auto-${ axis } `
1195
- }
0 commit comments