Skip to content

Commit 825b58f

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

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

src/util/index.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,3 +426,36 @@ export const getCSSVarName = value => {
426426
const match = value?.match( /var\(\s*([^,)\s]+)/ )
427427
return match ? match[ 1 ] : null
428428
}
429+
430+
/**
431+
* Convert a value from rem/em to px if the units array doesn't support it.
432+
*
433+
* @param {string[]} units - The list of supported units.
434+
* @param {string} currentUnit - The current unit of the value.
435+
* @param {string|number} currentValue - The current value to convert.
436+
*
437+
* @return {Object} An object containing the converted value and unit.
438+
*/
439+
export function convertToPxIfUnsupported( units, currentUnit, currentValue ) {
440+
const unitMultipliers = {
441+
rem: 16,
442+
em: 16,
443+
}
444+
445+
const normalizedUnit = currentUnit?.toLowerCase()
446+
447+
if (
448+
( ! units?.length || ! units.includes( normalizedUnit ) ) &&
449+
unitMultipliers[ normalizedUnit ]
450+
) {
451+
return {
452+
value: `${ parseFloat( currentValue ) * unitMultipliers[ normalizedUnit ] }`,
453+
unit: 'px',
454+
}
455+
}
456+
457+
return {
458+
value: currentValue,
459+
unit: currentUnit,
460+
}
461+
}

0 commit comments

Comments
 (0)