@@ -38,6 +38,8 @@ import { T_SHIRT_NAMES } from './constants';
38
38
39
39
const DEFAULT_UNITS = [ 'px' , 'em' , 'rem' , 'vw' , 'vh' ] ;
40
40
41
+ const MAX_TOGGLE_GROUP_SIZES = 5 ;
42
+
41
43
const UnforwardedFontSizePicker = (
42
44
props : FontSizePickerProps ,
43
45
ref : ForwardedRef < any >
@@ -59,43 +61,49 @@ const UnforwardedFontSizePicker = (
59
61
availableUnits : unitsProp ,
60
62
} ) ;
61
63
62
- const shouldUseSelectControl = fontSizes . length > 5 ;
63
64
const selectedFontSize = fontSizes . find (
64
65
( fontSize ) => fontSize . size === value
65
66
) ;
66
67
const isCustomValue = ! ! value && ! selectedFontSize ;
67
68
68
- const [ showCustomValueControl , setShowCustomValueControl ] = useState (
69
- ! disableCustomFontSizes && isCustomValue
70
- ) ;
71
-
72
- const headerHint = useMemo ( ( ) => {
73
- if ( showCustomValueControl ) {
74
- return __ ( 'Custom' ) ;
75
- }
69
+ // Initially request a custom picker if the value is not from the predef list.
70
+ const [ userRequestedCustom , setUserRequestedCustom ] =
71
+ useState ( isCustomValue ) ;
76
72
77
- if ( ! shouldUseSelectControl ) {
78
- if ( selectedFontSize ) {
79
- return (
80
- selectedFontSize . name ||
81
- T_SHIRT_NAMES [ fontSizes . indexOf ( selectedFontSize ) ]
82
- ) ;
83
- }
84
- return '' ;
85
- }
73
+ let currentPickerType ;
74
+ if ( ! disableCustomFontSizes && userRequestedCustom ) {
75
+ // While showing the custom value picker, switch back to predef only if
76
+ // `disableCustomFontSizes` is set to `true`.
77
+ currentPickerType = 'custom' as const ;
78
+ } else {
79
+ currentPickerType =
80
+ fontSizes . length > MAX_TOGGLE_GROUP_SIZES
81
+ ? ( 'select' as const )
82
+ : ( 'togglegroup' as const ) ;
83
+ }
86
84
87
- const commonUnit = getCommonSizeUnit ( fontSizes ) ;
88
- if ( commonUnit ) {
89
- return `(${ commonUnit } )` ;
85
+ const headerHint = useMemo ( ( ) => {
86
+ switch ( currentPickerType ) {
87
+ case 'custom' :
88
+ return __ ( 'Custom' ) ;
89
+ case 'togglegroup' :
90
+ if ( selectedFontSize ) {
91
+ return (
92
+ selectedFontSize . name ||
93
+ T_SHIRT_NAMES [ fontSizes . indexOf ( selectedFontSize ) ]
94
+ ) ;
95
+ }
96
+ break ;
97
+ case 'select' :
98
+ const commonUnit = getCommonSizeUnit ( fontSizes ) ;
99
+ if ( commonUnit ) {
100
+ return `(${ commonUnit } )` ;
101
+ }
102
+ break ;
90
103
}
91
104
92
105
return '' ;
93
- } , [
94
- showCustomValueControl ,
95
- shouldUseSelectControl ,
96
- selectedFontSize ,
97
- fontSizes ,
98
- ] ) ;
106
+ } , [ currentPickerType , selectedFontSize , fontSizes ] ) ;
99
107
100
108
if ( fontSizes . length === 0 && disableCustomFontSizes ) {
101
109
return null ;
@@ -133,53 +141,45 @@ const UnforwardedFontSizePicker = (
133
141
{ ! disableCustomFontSizes && (
134
142
< HeaderToggle
135
143
label = {
136
- showCustomValueControl
144
+ currentPickerType === 'custom'
137
145
? __ ( 'Use size preset' )
138
146
: __ ( 'Set custom size' )
139
147
}
140
148
icon = { settings }
141
- onClick = { ( ) => {
142
- setShowCustomValueControl (
143
- ! showCustomValueControl
144
- ) ;
145
- } }
146
- isPressed = { showCustomValueControl }
149
+ onClick = { ( ) =>
150
+ setUserRequestedCustom ( ! userRequestedCustom )
151
+ }
152
+ isPressed = { currentPickerType === 'custom' }
147
153
size = "small"
148
154
/>
149
155
) }
150
156
</ Header >
151
157
</ Spacer >
152
158
< div >
153
- { ! ! fontSizes . length &&
154
- shouldUseSelectControl &&
155
- ! showCustomValueControl && (
156
- < FontSizePickerSelect
157
- __next40pxDefaultSize = { __next40pxDefaultSize }
158
- fontSizes = { fontSizes }
159
- value = { value }
160
- disableCustomFontSizes = { disableCustomFontSizes }
161
- size = { size }
162
- onChange = { ( newValue ) => {
163
- if ( newValue === undefined ) {
164
- onChange ?.( undefined ) ;
165
- } else {
166
- onChange ?.(
167
- hasUnits
168
- ? newValue
169
- : Number ( newValue ) ,
170
- fontSizes . find (
171
- ( fontSize ) =>
172
- fontSize . size === newValue
173
- )
174
- ) ;
175
- }
176
- } }
177
- onSelectCustom = { ( ) =>
178
- setShowCustomValueControl ( true )
159
+ { currentPickerType === 'select' && (
160
+ < FontSizePickerSelect
161
+ __next40pxDefaultSize = { __next40pxDefaultSize }
162
+ fontSizes = { fontSizes }
163
+ value = { value }
164
+ disableCustomFontSizes = { disableCustomFontSizes }
165
+ size = { size }
166
+ onChange = { ( newValue ) => {
167
+ if ( newValue === undefined ) {
168
+ onChange ?.( undefined ) ;
169
+ } else {
170
+ onChange ?.(
171
+ hasUnits ? newValue : Number ( newValue ) ,
172
+ fontSizes . find (
173
+ ( fontSize ) =>
174
+ fontSize . size === newValue
175
+ )
176
+ ) ;
179
177
}
180
- />
181
- ) }
182
- { ! shouldUseSelectControl && ! showCustomValueControl && (
178
+ } }
179
+ onSelectCustom = { ( ) => setUserRequestedCustom ( true ) }
180
+ />
181
+ ) }
182
+ { currentPickerType === 'togglegroup' && (
183
183
< FontSizePickerToggleGroup
184
184
fontSizes = { fontSizes }
185
185
value = { value }
@@ -200,7 +200,7 @@ const UnforwardedFontSizePicker = (
200
200
} }
201
201
/>
202
202
) }
203
- { ! disableCustomFontSizes && showCustomValueControl && (
203
+ { currentPickerType === 'custom' && (
204
204
< Flex className = "components-font-size-picker__custom-size-control" >
205
205
< FlexItem isBlock >
206
206
< UnitControl
@@ -210,6 +210,8 @@ const UnforwardedFontSizePicker = (
210
210
hideLabelFromVision
211
211
value = { value }
212
212
onChange = { ( newValue ) => {
213
+ setUserRequestedCustom ( true ) ;
214
+
213
215
if ( newValue === undefined ) {
214
216
onChange ?.( undefined ) ;
215
217
} else {
@@ -240,6 +242,8 @@ const UnforwardedFontSizePicker = (
240
242
initialPosition = { fallbackFontSize }
241
243
withInputField = { false }
242
244
onChange = { ( newValue ) => {
245
+ setUserRequestedCustom ( true ) ;
246
+
243
247
if ( newValue === undefined ) {
244
248
onChange ?.( undefined ) ;
245
249
} else if ( hasUnits ) {
0 commit comments