Skip to content

Commit 46a7b29

Browse files
committed
fix: still match previous value even the custom values changes
1 parent 130d498 commit 46a7b29

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

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

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
useBlockSetAttributesContext,
1414
useDeviceType,
1515
} from '~stackable/hooks'
16-
import { extractNumbersAndUnits } from '~stackable/util'
16+
import { extractNumbersAndUnits, getCSSVarName } from '~stackable/util'
1717

1818
/**
1919
* External dependencies
@@ -106,13 +106,18 @@ const AdvancedRangeControl = props => {
106106

107107
// Is value at first render the same as a step value? If so, do mark mode
108108
// at the start, or show custom
109-
let isMarkValue = !! props.marks
109+
let currentMark = null
110110
if ( props.marks && value ) {
111111
const valueToCheck = value + ( props.hasCSSVariableValue ? '' : unit )
112-
// Check if the current value exsits in the marks
113-
isMarkValue = isMarkValue && props.marks.some( mark => mark.value === valueToCheck )
112+
// Check if the current value exists in the marks only by their CSS variable name
113+
// to match in case the fallback size changes.
114+
props.marks.forEach( mark => {
115+
if ( getCSSVarName( mark.value ) === getCSSVarName( valueToCheck ) ) {
116+
currentMark = mark
117+
}
118+
} )
114119
}
115-
const [ isMarkMode, setIsMarkMode ] = useState( isMarkValue )
120+
const [ isMarkMode, setIsMarkMode ] = useState( !! currentMark )
116121

117122
// If this supports dynamic content, the value should be saved as a String.
118123
// Similar if using marks to accomodate CSS variable
@@ -135,7 +140,10 @@ const AdvancedRangeControl = props => {
135140
onChangeFunc( newValue )
136141
}
137142

138-
const derivedValue = typeof props.value === 'undefined' ? value : props.value
143+
const derivedValue = typeof props.value === 'undefined'
144+
// Use the value from the provided marks
145+
? currentMark ? currentMark.value : value
146+
: props.value
139147

140148
const dynamicContentProps = useDynamicContentControlProps( {
141149
value: derivedValue,

src/util/index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,3 +417,9 @@ export const extractNumbersAndUnits = value => {
417417
}
418418
return [ [ '0', 'px' ] ]
419419
}
420+
421+
// Return only the CSS variable name given a CSS variable
422+
export const getCSSVarName = value => {
423+
const match = value.match( /var\(\s*([^,)\s]+)/ )
424+
return match ? match[ 1 ] : null
425+
}

0 commit comments

Comments
 (0)