Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BoxControl: Add support for grouped directions (vertical and horizontal controls) #32610

Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions packages/components/src/box-control/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,14 @@ If this property is true, a button to reset the box control is rendered.
- Required: No
- Default: `true`

### isGroupedDirections
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: For this to be more semantic english, should we use has instead of is here given directions is plural?

An even more descriptive prop name might be like splitOnAxis or something like that. Grouped directions could mean corners are grouped together for example.

Suggested change
### isGroupedDirections
### hasGroupedDirections

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I love some good naming tips, the is never sit right with the plural for me. I actually quite like splitOnAxis, so I've updated this PR to use that instead. Happy to rename again if you think there's a better option.


If this property is true, when the box control is unlinked, vertical and horizontal controls can be used instead of updating individual sides.

- Type: `Boolean`
- Required: No
- Default: `false`

### inputProps

Props for the internal [InputControl](../input-control) components.
Expand Down
21 changes: 17 additions & 4 deletions packages/components/src/box-control/all-input-control.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,24 @@ export default function AllInputControl( {

const handleOnChange = ( next ) => {
const nextValues = { ...values };
const selectedSides = sides?.length
? sides
: [ 'top', 'right', 'bottom', 'left' ];

selectedSides.forEach( ( side ) => ( nextValues[ side ] = next ) );
if ( sides?.length ) {
sides.forEach( ( side ) => {
if ( side === 'vertical' ) {
nextValues.top = next;
nextValues.bottom = next;
} else if ( side === 'horizontal' ) {
nextValues.left = next;
nextValues.right = next;
} else {
nextValues[ side ] = next;
}
} );
} else {
[ 'top', 'right', 'bottom', 'left' ].forEach(
( side ) => ( nextValues[ side ] = next )
);
}

onChange( nextValues );
};
Expand Down
8 changes: 4 additions & 4 deletions packages/components/src/box-control/icon.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ export default function BoxControlIcon( {
return side === 'all' || side === value;
};

const top = getSide( 'top' );
const right = getSide( 'right' );
const bottom = getSide( 'bottom' );
const left = getSide( 'left' );
const top = getSide( 'top' ) || getSide( 'vertical' );
const right = getSide( 'right' ) || getSide( 'horizontal' );
const bottom = getSide( 'bottom' ) || getSide( 'vertical' );
const left = getSide( 'left' ) || getSide( 'horizontal' );
ciampo marked this conversation as resolved.
Show resolved Hide resolved

// Simulates SVG Icon scaling
const scale = size / BASE_ICON_SIZE;
Expand Down
20 changes: 17 additions & 3 deletions packages/components/src/box-control/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import Button from '../button';
import { FlexItem, FlexBlock } from '../flex';
import AllInputControl from './all-input-control';
import InputControls from './input-controls';
import VerticalHorizontalInputControls from './vertical-horizontal-input-controls';
import BoxControlIcon from './icon';
import { Text } from '../text';
import LinkedButton from './linked-button';
Expand All @@ -29,6 +30,7 @@ import {
import {
DEFAULT_VALUES,
DEFAULT_VISUALIZER_VALUES,
getInitialSide,
isValuesMixed,
isValuesDefined,
} from './utils';
Expand All @@ -52,6 +54,7 @@ export default function BoxControl( {
values: valuesProp,
units,
sides,
isGroupedDirections = false,
allowReset = true,
resetValues = DEFAULT_VALUES,
} ) {
Expand All @@ -67,14 +70,16 @@ export default function BoxControl( {
! hasInitialValue || ! isValuesMixed( inputValues ) || hasOneSide
);

const [ side, setSide ] = useState( isLinked ? 'all' : 'top' );
const [ side, setSide ] = useState(
getInitialSide( isLinked, isGroupedDirections )
);

const id = useUniqueId( idProp );
const headingId = `${ id }-heading`;

const toggleLinked = () => {
setIsLinked( ! isLinked );
setSide( ! isLinked ? 'all' : 'top' );
setSide( getInitialSide( ! isLinked, isGroupedDirections ) );
};

const handleOnFocus = ( event, { side: nextSide } ) => {
Expand Down Expand Up @@ -150,6 +155,13 @@ export default function BoxControl( {
/>
</FlexBlock>
) }
{ ! isLinked && isGroupedDirections && (
<FlexBlock>
<VerticalHorizontalInputControls
{ ...inputControlProps }
/>
</FlexBlock>
) }
{ ! hasOneSide && (
<FlexItem>
<LinkedButton
Expand All @@ -159,7 +171,9 @@ export default function BoxControl( {
</FlexItem>
) }
</HeaderControlWrapper>
{ ! isLinked && <InputControls { ...inputControlProps } /> }
{ ! isLinked && ! isGroupedDirections && (
<InputControls { ...inputControlProps } />
) }
</Root>
);
}
Expand Down
21 changes: 20 additions & 1 deletion packages/components/src/box-control/stories/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ const defaultSideValues = {
left: '10px',
};

function DemoExample( { sides, defaultValues = defaultSideValues } ) {
function DemoExample( {
sides,
defaultValues = defaultSideValues,
isGroupedDirections = false,
} ) {
const [ values, setValues ] = useState( defaultValues );
const [ showVisualizer, setShowVisualizer ] = useState( {} );

Expand All @@ -41,6 +45,7 @@ function DemoExample( { sides, defaultValues = defaultSideValues } ) {
sides={ sides }
onChange={ setValues }
onChangeShowVisualizer={ setShowVisualizer }
isGroupedDirections={ isGroupedDirections }
/>
</Content>
</FlexBlock>
Expand Down Expand Up @@ -82,6 +87,20 @@ export const singleSide = () => {
);
};

export const groupedDirections = () => {
return <DemoExample isGroupedDirections={ true } />;
};

export const groupedDirectionsWithSingleSide = () => {
return (
<DemoExample
sides={ [ 'horizontal' ] }
defaultValues={ { left: '10px', right: '10px' } }
isGroupedDirections={ true }
/>
);
};

const Container = styled( Flex )`
max-width: 780px;
`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export const Header = styled( Flex )`

export const HeaderControlWrapper = styled( Flex )`
min-height: 30px;
gap: 0;
`;

export const UnitControlWrapper = styled.div`
Expand Down Expand Up @@ -59,8 +60,8 @@ const unitControlBorderRadiusStyles = ( { isFirst, isLast, isOnly } ) => {
} );
};

const unitControlMarginStyles = ( { isFirst } ) => {
const marginLeft = isFirst ? 0 : -1;
const unitControlMarginStyles = ( { isFirst, isOnly } ) => {
const marginLeft = isFirst || isOnly ? 0 : -1;

return rtl( { marginLeft } )();
};
Expand Down
20 changes: 20 additions & 0 deletions packages/components/src/box-control/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ export const LABELS = {
left: __( 'Left' ),
right: __( 'Right' ),
mixed: __( 'Mixed' ),
vertical: __( 'Vertical' ),
horizontal: __( 'Horizontal' ),
};

export const DEFAULT_VALUES = {
Expand Down Expand Up @@ -119,3 +121,21 @@ export function isValuesDefined( values ) {
)
);
}

/**
* Get initial selected side, factoring in whether the sides are linked,
* and whether or not the vertical / horizontal directions are grouped.
*
* @param {boolean} isLinked
* @param {boolean} isGroupedDirections
* @return {string} The initial side.
*/
export function getInitialSide( isLinked, isGroupedDirections ) {
let initialSide = 'all';

if ( ! isLinked ) {
initialSide = isGroupedDirections ? 'vertical' : 'top';
}

return initialSide;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/**
* External dependencies
*/
import { noop } from 'lodash';

/**
* Internal dependencies
*/
import UnitControl from './unit-control';
import { LABELS } from './utils';
import { Layout } from './styles/box-control-styles';

const groupedSides = [ 'vertical', 'horizontal' ];

export default function VerticalHorizontalInputControls( {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems we could call this AxialInputControl to be more succinct and clear?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I like that name! Thanks for the suggestion, I can put up a small PR to rename it 👍

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great, thanks!

onChange = noop,
andrewserong marked this conversation as resolved.
Show resolved Hide resolved
onFocus = noop,
onHoverOn = noop,
onHoverOff = noop,
values,
sides,
...props
} ) {
const createHandleOnFocus = ( side ) => ( event ) => {
onFocus( event, { side } );
};

const createHandleOnHoverOn = ( side ) => () => {
if ( side === 'vertical' ) {
onHoverOn( {
top: true,
bottom: true,
} );
} else {
onHoverOn( {
left: true,
right: true,
} );
}
};

const createHandleOnHoverOff = ( side ) => () => {
if ( side === 'vertical' ) {
onHoverOff( {
top: false,
bottom: false,
} );
} else {
onHoverOff( {
left: false,
right: false,
} );
}
};

const createHandleOnChange = ( side ) => ( next ) => {
const nextValues = { ...values };

if ( side === 'vertical' ) {
nextValues.top = next;
nextValues.bottom = next;
}
if ( side === 'horizontal' ) {
andrewserong marked this conversation as resolved.
Show resolved Hide resolved
nextValues.left = next;
nextValues.right = next;
}

onChange( nextValues );
};

// Filter sides if custom configuration provided, maintaining default order.
const filteredSides = sides?.length
? groupedSides.filter( ( side ) => sides.includes( side ) )
: groupedSides;

const first = filteredSides[ 0 ];
const last = filteredSides[ filteredSides.length - 1 ];
const only = first === last && first;
andrewserong marked this conversation as resolved.
Show resolved Hide resolved

return (
<Layout
gap={ 0 }
align="top"
className="component-box-control__vertical-horizontal-input-controls"
>
{ filteredSides.map( ( side ) => (
<UnitControl
{ ...props }
isFirst={ first === side }
isLast={ last === side }
isOnly={ only === side }
value={ 'vertical' === side ? values.top : values.left }
onChange={ createHandleOnChange( side ) }
onFocus={ createHandleOnFocus( side ) }
onHoverOn={ createHandleOnHoverOn( side ) }
onHoverOff={ createHandleOnHoverOff( side ) }
label={ LABELS[ side ] }
key={ `box-control-${ side }` }
andrewserong marked this conversation as resolved.
Show resolved Hide resolved
/>
) ) }
</Layout>
);
}