Skip to content

Commit 34ef9b9

Browse files
committed
fix: consider unit conversion during two-way value conversion between preset and custom mode
1 parent 1cb15b0 commit 34ef9b9

File tree

2 files changed

+22
-14
lines changed

2 files changed

+22
-14
lines changed

src/components/advanced-range-control/index.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ import {
1717
useBlockSetAttributesContext,
1818
useDeviceType,
1919
} from '~stackable/hooks'
20-
import { extractNumbersAndUnits, getCSSVarName } from '~stackable/util'
20+
import {
21+
extractNumbersAndUnits, getCSSVarName, convertToPxIfUnsupported,
22+
} from '~stackable/util'
2123
import { settings as stackableSettings } from 'stackable'
2224

2325
/**
@@ -207,6 +209,9 @@ const AdvancedRangeControl = props => {
207209
[ _value, _unit ] = extractNumbersAndUnits( mark.value )[ 0 ]
208210
} else {
209211
[ _value, _unit ] = extractNumbersAndUnits( mark.size )[ 0 ]
212+
const converted = convertToPxIfUnsupported( props.units, _unit, _value )
213+
_value = converted.value
214+
_unit = converted.unit
210215
}
211216
return _value === derivedValue && ( _unit === '' || _unit === unit )
212217
} )
@@ -218,12 +223,11 @@ const AdvancedRangeControl = props => {
218223
const markValue = props.marks[ value ]?.[ property ] || '0'
219224
let [ newValue, unit ] = extractNumbersAndUnits( markValue )[ 0 ]
220225

221-
// If the attribute has no support for rem, and the
222-
// preset units is rem, convert to px
223-
if ( ( ! hasUnits || ( hasUnits && ! props.units.includes( 'rem' ) ) ) && unit === 'rem' ) {
224-
newValue = `${ parseFloat( newValue ) * 16 }`
225-
unit = 'px'
226-
}
226+
// If the attribute has no support for rem or em, and the
227+
// preset units is rem or em, convert to px
228+
const converted = convertToPxIfUnsupported( props.units, unit, newValue )
229+
newValue = converted.value
230+
unit = converted.unit
227231

228232
// Update the unit.
229233
if ( unit ) {

src/components/four-range-control/index.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ import {
4444
useBlockHoverState,
4545
useBlockSetAttributesContext,
4646
} from '~stackable/hooks'
47-
import { extractNumbersAndUnits, getCSSVarName } from '~stackable/util'
47+
import {
48+
extractNumbersAndUnits, getCSSVarName, convertToPxIfUnsupported,
49+
} from '~stackable/util'
4850

4951
const isEqualInitial = ( props, value, firstValue ) => {
5052
let isEqual = true
@@ -364,6 +366,9 @@ const FourRangeControl = memo( props => {
364366
[ _value, _unit ] = extractNumbersAndUnits( mark.value )[ 0 ]
365367
} else {
366368
[ _value, _unit ] = extractNumbersAndUnits( mark.size )[ 0 ]
369+
const converted = convertToPxIfUnsupported( props.units, _unit, _value )
370+
_value = converted.value
371+
_unit = converted.unit
367372
}
368373
return _value === initialValue && ( _unit === '' || _unit === unit )
369374
} )
@@ -376,12 +381,11 @@ const FourRangeControl = memo( props => {
376381
const markValue = props.marks[ value ]?.[ property ] || '0'
377382
let [ newValue, unit ] = extractNumbersAndUnits( markValue )[ 0 ]
378383

379-
// If the attribute has no support for rem, and the
380-
// preset units is rem, convert to px
381-
if ( ( ! hasUnits || ( hasUnits && ! props.units.includes( 'rem' ) ) ) && unit === 'rem' ) {
382-
newValue = `${ parseFloat( newValue ) * 16 }`
383-
unit = 'px'
384-
}
384+
// If the attribute has no support for rem or em, and the
385+
// preset units is rem or em, convert to px
386+
const converted = convertToPxIfUnsupported( props.units, unit, newValue )
387+
newValue = converted.value
388+
unit = converted.unit
385389

386390
// Update the unit.
387391
if ( unit ) {

0 commit comments

Comments
 (0)