Skip to content

Commit 8b915f1

Browse files
Export applyValueToSides util function from BoxControl
This could avoid some code duplication for new components around spacing size presets.
1 parent 226f3fe commit 8b915f1

File tree

4 files changed

+41
-28
lines changed

4 files changed

+41
-28
lines changed

packages/components/src/box-control/all-input-control.js

+3-27
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
*/
44
import UnitControl from './unit-control';
55
import {
6-
ALL_SIDES,
76
LABELS,
7+
applyValueToSides,
88
getAllValue,
99
isValuesMixed,
1010
isValuesDefined,
@@ -32,42 +32,18 @@ export default function AllInputControl( {
3232
onFocus( event, { side: 'all' } );
3333
};
3434

35-
// Applies a value to an object representing top, right, bottom and left
36-
// sides while taking into account any custom side configuration.
37-
const applyValueToSides = ( currentValues, newValue ) => {
38-
const newValues = { ...currentValues };
39-
40-
if ( sides?.length ) {
41-
sides.forEach( ( side ) => {
42-
if ( side === 'vertical' ) {
43-
newValues.top = newValue;
44-
newValues.bottom = newValue;
45-
} else if ( side === 'horizontal' ) {
46-
newValues.left = newValue;
47-
newValues.right = newValue;
48-
} else {
49-
newValues[ side ] = newValue;
50-
}
51-
} );
52-
} else {
53-
ALL_SIDES.forEach( ( side ) => ( newValues[ side ] = newValue ) );
54-
}
55-
56-
return newValues;
57-
};
58-
5935
const handleOnChange = ( next ) => {
6036
const isNumeric = ! isNaN( parseFloat( next ) );
6137
const nextValue = isNumeric ? next : undefined;
62-
const nextValues = applyValueToSides( values, nextValue );
38+
const nextValues = applyValueToSides( values, nextValue, sides );
6339

6440
onChange( nextValues );
6541
};
6642

6743
// Set selected unit so it can be used as fallback by unlinked controls
6844
// when individual sides do not have a value containing a unit.
6945
const handleOnUnitChange = ( unit ) => {
70-
const newUnits = applyValueToSides( selectedUnits, unit );
46+
const newUnits = applyValueToSides( selectedUnits, unit, sides );
7147
setSelectedUnits( newUnits );
7248
};
7349

packages/components/src/box-control/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -173,3 +173,5 @@ export default function BoxControl( {
173173
</Root>
174174
);
175175
}
176+
177+
export { applyValueToSides } from './utils';

packages/components/src/box-control/utils.js

+32
Original file line numberDiff line numberDiff line change
@@ -194,3 +194,35 @@ export function normalizeSides( sides ) {
194194

195195
return filteredSides;
196196
}
197+
198+
/**
199+
* Applies a value to an object representing top, right, bottom and left sides
200+
* while taking into account any custom side configuration.
201+
*
202+
* @param {Object} currentValues The current values for each side.
203+
* @param {string|number} newValue The value to apply to the sides object.
204+
* @param {string[]} sides Array defining valid sides.
205+
*
206+
* @return {Object} Object containing the updated values for each side.
207+
*/
208+
export function applyValueToSides( currentValues, newValue, sides ) {
209+
const newValues = { ...currentValues };
210+
211+
if ( sides?.length ) {
212+
sides.forEach( ( side ) => {
213+
if ( side === 'vertical' ) {
214+
newValues.top = newValue;
215+
newValues.bottom = newValue;
216+
} else if ( side === 'horizontal' ) {
217+
newValues.left = newValue;
218+
newValues.right = newValue;
219+
} else {
220+
newValues[ side ] = newValue;
221+
}
222+
} );
223+
} else {
224+
ALL_SIDES.forEach( ( side ) => ( newValues[ side ] = newValue ) );
225+
}
226+
227+
return newValues;
228+
}

packages/components/src/index.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@ export {
3030
isEmptyBorder as __experimentalIsEmptyBorder,
3131
} from './border-box-control';
3232
export { BorderControl as __experimentalBorderControl } from './border-control';
33-
export { default as __experimentalBoxControl } from './box-control';
33+
export {
34+
default as __experimentalBoxControl,
35+
applyValueToSides as __experimentalApplyValueToSides,
36+
} from './box-control';
3437
export { default as Button } from './button';
3538
export { default as ButtonGroup } from './button-group';
3639
export {

0 commit comments

Comments
 (0)