Skip to content

Commit

Permalink
Fix the arbitrary sides use-case and swap sides
Browse files Browse the repository at this point in the history
  • Loading branch information
youknowriad committed Dec 6, 2024
1 parent 6546f48 commit 82dafce
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 22 deletions.
7 changes: 4 additions & 3 deletions packages/components/src/box-control/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {
getInitialSide,
isValuesMixed,
isValuesDefined,
ALL_SIDES,
getAllowedSides,
} from './utils';
import { useControlledState } from '../utils/hooks';
import type {
Expand Down Expand Up @@ -160,6 +160,7 @@ function BoxControl( {
__next40pxDefaultSize,
size: undefined,
} );
const sidesToRender = getAllowedSides( sides );

return (
<Grid
Expand Down Expand Up @@ -188,7 +189,7 @@ function BoxControl( {

{ ! isLinked &&
splitOnAxis &&
[ 'horizontal', 'vertical' ].map( ( axis ) => (
[ 'vertical', 'horizontal' ].map( ( axis ) => (
<InputControl
key={ axis }
side={ axis as 'horizontal' | 'vertical' }
Expand All @@ -197,7 +198,7 @@ function BoxControl( {
) ) }
{ ! isLinked &&
! splitOnAxis &&
ALL_SIDES.map( ( axis ) => (
Array.from( sidesToRender ).map( ( axis ) => (
<InputControl
key={ axis }
side={ axis }
Expand Down
20 changes: 1 addition & 19 deletions packages/components/src/box-control/input-control.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import { __ } from '@wordpress/i18n';
import Tooltip from '../tooltip';
import { parseQuantityAndUnitFromRawValue } from '../unit-control/utils';
import {
ALL_SIDES,
CUSTOM_VALUE_SETTINGS,
getAllowedSides,
getAllValue,
isValuesDefined,
isValuesMixed,
Expand All @@ -27,24 +27,6 @@ import type { BoxControlInputControlProps, BoxControlValue } from './types';

const noop = () => {};

function getAllowedSides( sides: BoxControlInputControlProps[ 'sides' ] ) {
const allowedSides: Set< keyof BoxControlValue > = new Set(
! sides ? ALL_SIDES : []
);
sides?.forEach( ( allowedSide ) => {
if ( allowedSide === 'vertical' ) {
allowedSides.add( 'top' );
allowedSides.add( 'bottom' );
} else if ( allowedSide === 'horizontal' ) {
allowedSides.add( 'right' );
allowedSides.add( 'left' );
} else {
allowedSides.add( allowedSide );
}
} );
return allowedSides;
}

function getSidesToModify(
side: BoxControlInputControlProps[ 'side' ],
sides: BoxControlInputControlProps[ 'sides' ],
Expand Down
27 changes: 27 additions & 0 deletions packages/components/src/box-control/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { __ } from '@wordpress/i18n';
*/
import { parseQuantityAndUnitFromRawValue } from '../unit-control/utils';
import type {
BoxControlInputControlProps,
BoxControlProps,
BoxControlValue,
CustomValueUnits,
Expand Down Expand Up @@ -275,3 +276,29 @@ export function applyValueToSides(

return newValues;
}

/**
* Return the allowed sides based on the sides configuration.
*
* @param sides Sides configuration.
* @return Allowed sides.
*/
export function getAllowedSides(
sides: BoxControlInputControlProps[ 'sides' ]
) {
const allowedSides: Set< keyof BoxControlValue > = new Set(
! sides ? ALL_SIDES : []
);
sides?.forEach( ( allowedSide ) => {
if ( allowedSide === 'vertical' ) {
allowedSides.add( 'top' );
allowedSides.add( 'bottom' );
} else if ( allowedSide === 'horizontal' ) {
allowedSides.add( 'right' );
allowedSides.add( 'left' );
} else {
allowedSides.add( allowedSide );
}
} );
return allowedSides;
}

0 comments on commit 82dafce

Please sign in to comment.