@@ -13,6 +13,7 @@ import {
1313 useBlockSetAttributesContext ,
1414 useDeviceType ,
1515} from '~stackable/hooks'
16+ import { extractNumbersAndUnits } from '~stackable/util'
1617
1718/**
1819 * External dependencies
@@ -186,18 +187,23 @@ const AdvancedRangeControl = props => {
186187 let rangeOnChange = _onChange
187188 if ( isMarkMode ) {
188189 rangeValue = props . marks . findIndex ( mark => {
189- const [ _value , _unit ] = extractNumberAndUnit ( mark . value )
190+ const [ _value , _unit ] = extractNumbersAndUnits ( mark . value ) [ 0 ]
190191 return _value === derivedValue
191192 } )
192- rangeOnChange = value => {
193+ rangeOnChange = ( value , property = 'value' ) => {
193194 if ( value === '' ) {
194195 return _onChange ( value )
195196 }
196-
197197 // Extract the unit and value.
198- const markValue = props . marks [ value ] ?. value || '0'
199- const [ _newValue , unit ] = extractNumberAndUnit ( markValue )
200- const newValue = _newValue
198+ const markValue = props . marks [ value ] ?. [ property ] || '0'
199+ let [ newValue , unit ] = extractNumbersAndUnits ( markValue ) [ 0 ]
200+
201+ // If the attribute has no units (only support px), and the
202+ // preset units are rem or em, convert to px
203+ if ( ! hasUnits && ( unit === 'rem' || unit === 'em' ) ) {
204+ newValue = `${ parseFloat ( newValue ) * 16 } `
205+ unit = 'px'
206+ }
201207
202208 // Update the unit.
203209 if ( unit ) {
@@ -229,7 +235,13 @@ const AdvancedRangeControl = props => {
229235 className = "stk-range-control__custom-button"
230236 size = "small"
231237 variant = "tertiary"
232- onClick = { ( ) => setIsMarkMode ( ! isMarkMode ) }
238+ onClick = { ( ) => {
239+ // Set the value when changing from mark mode to custom
240+ if ( isMarkMode && rangeValue !== - 1 ) {
241+ rangeOnChange ( rangeValue , 'size' )
242+ }
243+ setIsMarkMode ( ! isMarkMode )
244+ } }
233245 icon = { settings }
234246 >
235247 </ Button >
@@ -272,14 +284,3 @@ AdvancedRangeControl.defaultProps = {
272284}
273285
274286export default memo ( AdvancedRangeControl , isEqual )
275-
276- // The value can be in the format '10px' or '10.0em' or '10rem'.
277- // Return an array with the number and the unit.
278- const extractNumberAndUnit = value => {
279- // Match the last characters that are not numbers.
280- const matches = value . match ( / ( [ \d . ] + ) ( \D * ) $ / )
281- if ( ! matches || value . startsWith ( 'var(--stk' ) ) {
282- return [ value , '' ]
283- }
284- return [ matches [ 1 ] , matches [ 2 ] ]
285- }
0 commit comments