@@ -18,7 +18,6 @@ import RangeControl from '../advanced-range-control/range-control'
1818import { ResetButton } from '../base-control2/reset-button'
1919import AdvancedControl , { extractControlProps } from '../base-control2'
2020import { useControlHandlers } from '../base-control2/hooks'
21- import { extractNumbersAndUnits } from '~stackable/util'
2221
2322/**
2423 * WordPress dependencies
@@ -45,6 +44,7 @@ import {
4544 useBlockHoverState ,
4645 useBlockSetAttributesContext ,
4746} from '~stackable/hooks'
47+ import { extractNumbersAndUnits , getCSSVarName } from '~stackable/util'
4848
4949const isEqualInitial = ( props , value , firstValue ) => {
5050 let isEqual = true
@@ -81,7 +81,7 @@ const FourRangeControl = memo( props => {
8181 ( props . enableBottom && value . bottom === '' ) &&
8282 ( props . enableLeft && value . left === '' )
8383
84- const firstValue = props . enableTop ? value . top
84+ let firstValue = props . enableTop ? value . top
8585 : props . enableRight ? value . right
8686 : props . enableBottom ? value . bottom
8787 : value . left
@@ -195,17 +195,26 @@ const FourRangeControl = memo( props => {
195195 left : ! ! props . marks ,
196196 }
197197 if ( props . marks && firstValue ) {
198- // Check if the current value exsits in the marks
199- const marksUnit = ( props . hasCSSVariableValue ? '' : unit )
200- isMarkValue . first = isMarkValue . first && props . marks . some ( mark => mark . value === firstValue + marksUnit )
201- isMarkValue . top = isMarkValue . top && props . marks . some ( mark => mark . value === value . top + marksUnit )
202- isMarkValue . right = isMarkValue . right && props . marks . some ( mark => mark . value === value . right + marksUnit )
203- isMarkValue . bottom = isMarkValue . bottom && props . marks . some ( mark => mark . value === value . bottom + marksUnit )
204- isMarkValue . left = isMarkValue . left && props . marks . some ( mark => mark . value === value . left + marksUnit )
198+ // Check if the current value exists in the marks only by their CSS variable name
199+ // to match in case the fallback size changes.
200+ const firstMatchedMark = props . marks . find ( mark => getCSSVarName ( mark . value ) === getCSSVarName ( firstValue ) )
201+ isMarkValue . first = ! ! firstMatchedMark
202+ if ( firstMatchedMark ) {
203+ firstValue = firstMatchedMark . value
204+ }
205+
206+ [ 'top' , 'right' , 'bottom' , 'left' ] . forEach ( side => {
207+ const matchedMark = props . marks . find ( mark => getCSSVarName ( mark . value ) === getCSSVarName ( value [ side ] ) )
208+ isMarkValue [ side ] = ! ! matchedMark
209+ if ( matchedMark ) {
210+ value [ side ] = matchedMark . value
211+ }
212+ } )
205213 }
206214 const [ isFourMarkMode , setIsFourMarkMode ] = useState ( isMarkValue )
207215
208- const onChangeAll = newValue => {
216+ const onChangeAll = _newValue => {
217+ const newValue = props . marks ? String ( _newValue ) : _newValue
209218 onChange ( {
210219 top : props . enableTop ? newValue : value . top ,
211220 right : props . enableRight ? newValue : value . right ,
@@ -221,7 +230,8 @@ const FourRangeControl = memo( props => {
221230 } ) )
222231 }
223232
224- const onChangeTop = newValue => {
233+ const onChangeTop = _newValue => {
234+ const newValue = props . marks ? String ( _newValue ) : _newValue
225235 onChange ( {
226236 top : newValue ,
227237 right : value . right ,
@@ -231,7 +241,8 @@ const FourRangeControl = memo( props => {
231241 setIsFourMarkMode ( prev => ( { ...prev , first : prev . top } ) )
232242 }
233243
234- const onChangeRight = newValue => {
244+ const onChangeRight = _newValue => {
245+ const newValue = props . marks ? String ( _newValue ) : _newValue
235246 onChange ( {
236247 top : value . top ,
237248 right : newValue ,
@@ -241,7 +252,8 @@ const FourRangeControl = memo( props => {
241252 setIsFourMarkMode ( prev => ( { ...prev , first : prev . right } ) )
242253 }
243254
244- const onChangeBottom = newValue => {
255+ const onChangeBottom = _newValue => {
256+ const newValue = props . marks ? String ( _newValue ) : _newValue
245257 onChange ( {
246258 top : value . top ,
247259 right : value . right ,
@@ -251,7 +263,8 @@ const FourRangeControl = memo( props => {
251263 setIsFourMarkMode ( prev => ( { ...prev , first : prev . bottom } ) )
252264 }
253265
254- const onChangeLeft = newValue => {
266+ const onChangeLeft = _newValue => {
267+ const newValue = props . marks ? String ( _newValue ) : _newValue
255268 onChange ( {
256269 top : value . top ,
257270 right : value . right ,
@@ -261,7 +274,8 @@ const FourRangeControl = memo( props => {
261274 setIsFourMarkMode ( prev => ( { ...prev , first : prev . left } ) )
262275 }
263276
264- const onChangeVertical = newValue => {
277+ const onChangeVertical = _newValue => {
278+ const newValue = props . marks ? String ( _newValue ) : _newValue
265279 onChange ( {
266280 top : newValue ,
267281 right : value . right ,
@@ -275,7 +289,8 @@ const FourRangeControl = memo( props => {
275289 } ) )
276290 }
277291
278- const onChangeHorizontal = newValue => {
292+ const onChangeHorizontal = _newValue => {
293+ const newValue = props . marks ? String ( _newValue ) : _newValue
279294 onChange ( {
280295 top : value . top ,
281296 right : newValue ,
@@ -327,7 +342,7 @@ const FourRangeControl = memo( props => {
327342 }
328343
329344 // We need to change the way we handle the value and onChange if we're doing marks
330- let rangeValue = props . hasCSSVariableValue ? parseFloat ( initialValue ) : initialValue
345+ let rangeValue = props . marks ? parseFloat ( initialValue ) : initialValue
331346 let rangeOnChange = initialOnChange
332347 if ( props . marks && isMarkMode ) {
333348 rangeValue = props . marks . findIndex ( mark => {
@@ -341,12 +356,23 @@ const FourRangeControl = memo( props => {
341356
342357 // Extract the unit and value.
343358 const markValue = props . marks [ value ] ?. [ property ] || '0'
344- const [ _newValue , unit ] = extractNumbersAndUnits ( markValue ) [ 0 ]
345- const newValue = _newValue
359+ let [ newValue , unit ] = extractNumbersAndUnits ( markValue ) [ 0 ]
360+
361+ // If the attribute has no support for rem, and the
362+ // preset units is rem, convert to px
363+ if ( ( ! hasUnits || ( hasUnits && ! props . units . includes ( 'rem' ) ) ) && unit === 'rem' ) {
364+ newValue = `${ parseFloat ( newValue ) * 16 } `
365+ unit = 'px'
366+ }
346367
347368 // Update the unit.
348- dispatch ( 'core/block-editor' ) . __unstableMarkNextChangeAsNotPersistent ( )
349- setAttributes ( { [ unitAttrName ] : unit } )
369+ if ( unit ) {
370+ dispatch ( 'core/block-editor' ) . __unstableMarkNextChangeAsNotPersistent ( )
371+ setAttributes ( { [ unitAttrName ] : unit } )
372+ if ( props . onChangeUnit ) {
373+ props . onChangeUnit ( unit )
374+ }
375+ }
350376
351377 initialOnChange ( newValue )
352378 }
@@ -890,7 +916,6 @@ FourRangeControl.defaultProps = {
890916
891917 marks : undefined ,
892918 allowCustom : true ,
893- hasCSSVariableValue : false ,
894919}
895920
896921export default memo ( FourRangeControl )
0 commit comments