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

Spacing: Optimize & condense unlinked spacing controls #50660

Merged
merged 25 commits into from
May 29, 2023
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
6ef8242
Add useSpacingSizes hook
aaronrobertshaw May 11, 2023
2186d2c
Use spacing sizes hook in spacing control
aaronrobertshaw May 11, 2023
d6eac79
Add temporary SVG side icons
aaronrobertshaw May 12, 2023
cb376eb
Add new utils and unit tests to cover them
aaronrobertshaw May 11, 2023
2b8f5d0
Add SideDropdown to select sides view
aaronrobertshaw May 12, 2023
0497541
Relocate existing input controls
aaronrobertshaw May 12, 2023
84bdacc
Update input controls
aaronrobertshaw May 12, 2023
1cf9ddf
Remove old LinkedButton
aaronrobertshaw May 12, 2023
014ea1d
Refactor SpacingSizesControl layout
aaronrobertshaw May 14, 2023
3ecb7f3
Update SpacingSizesControl styling
aaronrobertshaw May 12, 2023
161b77c
Update dimensions panel use of SpacingSizesControl
aaronrobertshaw May 15, 2023
f924741
Clean up unnecessary toggle classes
aaronrobertshaw May 16, 2023
5db0170
Restore All sides aria label
aaronrobertshaw May 16, 2023
f43d957
Remove linked view and tweak default views
aaronrobertshaw May 18, 2023
b33165d
Improve labelling
aaronrobertshaw May 19, 2023
f3624b8
Improve control's spacing
aaronrobertshaw May 22, 2023
94d0570
Add means for block spacing to opt out of side in label
aaronrobertshaw May 22, 2023
cd2974b
Tweak styles a little more
aaronrobertshaw May 22, 2023
88e333c
Avoid using components prefix on classes
aaronrobertshaw May 25, 2023
f3afd62
Remove as many uses of internal component styles as possible
aaronrobertshaw May 25, 2023
7c14c9f
Fix display of custom value slider
aaronrobertshaw May 29, 2023
2ff0d7b
Fix typ
aaronrobertshaw May 29, 2023
8b05919
Fix another typo
aaronrobertshaw May 29, 2023
d6eb624
Ensure no bottom margin on custom value input
aaronrobertshaw May 29, 2023
34c172e
Fix labelling for all or fudged sides
aaronrobertshaw May 29, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,6 @@ export default function DimensionsPanel( {
sides={ paddingSides }
units={ units }
allowReset={ false }
splitOnAxis={ isAxialPadding }
onMouseOver={ onMouseOverPadding }
onMouseOut={ onMouseLeaveControls }
/>
Expand Down Expand Up @@ -529,7 +528,6 @@ export default function DimensionsPanel( {
sides={ marginSides }
units={ units }
allowReset={ false }
splitOnAxis={ isAxialMargin }
onMouseOver={ onMouseOverMargin }
onMouseOut={ onMouseLeaveControls }
/>
Expand Down Expand Up @@ -576,10 +574,10 @@ export default function DimensionsPanel( {
label={ __( 'Block spacing' ) }
min={ 0 }
onChange={ setGapValues }
showSideInLabel={ false }
sides={ isAxialGap ? gapSides : [ 'top' ] } // Use 'top' as the shorthand property in non-axial configurations.
values={ gapValues }
allowReset={ false }
splitOnAxis={ isAxialGap }
/>
) }
</ToolsPanelItem>
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';

/**
* Internal dependencies
*/
import useSetting from '../../use-setting';

export default function useSpacingSizes() {
const spacingSizes = [
{ name: 0, slug: '0', side: 0 },
...( useSetting( 'spacing.spacingSizes' ) || [] ),
];

if ( spacingSizes.length > 8 ) {
spacingSizes.unshift( {
name: __( 'Default' ),
slug: 'default',
size: undefined,
} );
}

return spacingSizes;
}
150 changes: 79 additions & 71 deletions packages/block-editor/src/components/spacing-sizes-control/index.js
Original file line number Diff line number Diff line change
@@ -1,61 +1,50 @@
/**
* External dependencies
*/
import classnames from 'classnames';

/**
* WordPress dependencies
*/
import {
BaseControl,
__experimentalHStack as HStack,
} from '@wordpress/components';
import { useState } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import { BaseControl } from '@wordpress/components';
import { __, _x, sprintf } from '@wordpress/i18n';

/**
* Internal dependencies
*/
import AllInputControl from './all-input-control';
import InputControls from './input-controls';
import AxialInputControls from './axial-input-controls';
import LinkedButton from './linked-button';
import { DEFAULT_VALUES, isValuesMixed, isValuesDefined } from './utils';
import useSetting from '../use-setting';
import AxialInputControls from './input-controls/axial';
import SeparatedInputControls from './input-controls/separated';
import SingleInputControl from './input-controls/single';
import SidesDropdown from './sides-dropdown';
import useSpacingSizes from './hooks/use-spacing-sizes';
import {
ALL_SIDES,
DEFAULT_VALUES,
LABELS,
VIEWS,
getInitialView,
} from './utils';

export default function SpacingSizesControl( {
inputProps,
onChange,
label = __( 'Spacing Control' ),
values,
sides,
splitOnAxis = false,
useSelect,
label: labelProp,
minimumCustomValue = 0,
onMouseOver,
onChange,
onMouseOut,
onMouseOver,
showSideInLabel = true,
sides = ALL_SIDES,
useSelect,
values,
} ) {
const spacingSizes = [
{ name: 0, slug: '0', size: 0 },
...( useSetting( 'spacing.spacingSizes' ) || [] ),
];

if ( spacingSizes.length > 8 ) {
spacingSizes.unshift( {
name: __( 'Default' ),
slug: 'default',
size: undefined,
} );
}

const spacingSizes = useSpacingSizes();
const inputValues = values || DEFAULT_VALUES;
const hasInitialValue = isValuesDefined( values );
const hasOneSide = sides?.length === 1;
const hasOnlyAxialSides =
sides?.includes( 'horizontal' ) &&
sides?.includes( 'vertical' ) &&
sides?.length === 2;

const [ isLinked, setIsLinked ] = useState(
! hasInitialValue || ! isValuesMixed( inputValues, sides ) || hasOneSide
);

const toggleLinked = () => {
setIsLinked( ! isLinked );
};
const [ view, setView ] = useState( getInitialView( inputValues, sides ) );

const handleOnChange = ( nextValue ) => {
const newValues = { ...values, ...nextValue };
Expand All @@ -64,43 +53,62 @@ export default function SpacingSizesControl( {

const inputControlProps = {
...inputProps,
minimumCustomValue,
onChange: handleOnChange,
isLinked,
onMouseOut,
onMouseOver,
sides,
values: inputValues,
spacingSizes,
type: labelProp,
useSelect,
type: label,
minimumCustomValue,
onMouseOver,
onMouseOut,
values: inputValues,
};

return (
<fieldset
className={ classnames( 'component-spacing-sizes-control', {
'is-unlinked': ! isLinked,
} ) }
>
<BaseControl.VisualLabel as="legend">
{ label }
</BaseControl.VisualLabel>
{ ! hasOneSide && (
<LinkedButton onClick={ toggleLinked } isLinked={ isLinked } />
) }
{ isLinked && (
<AllInputControl
aria-label={ label }
{ ...inputControlProps }
/>
) }
const renderControls = () => {
if ( view === VIEWS.axial ) {
return <AxialInputControls { ...inputControlProps } />;
}
if ( view === VIEWS.custom ) {
return <SeparatedInputControls { ...inputControlProps } />;
}
return <SingleInputControl side={ view } { ...inputControlProps } />;
};

const sideLabel =
ALL_SIDES.includes( view ) && showSideInLabel ? LABELS[ view ] : '';

const label = sprintf(
// translators: 2. Type of spacing being modified (Padding, margin, etc). 1: The side of the block being modified (top, bottom, left etc.).
__( '%1$s %2$s' ),
labelProp,
sideLabel
);

const dropdownLabelText = sprintf(
// translators: %s: The current spacing property e.g. "Padding", "Margin".
_x( '%s options', 'Button label to reveal side configuration options' ),
labelProp
);

{ ! isLinked && splitOnAxis && (
<AxialInputControls { ...inputControlProps } />
) }
{ ! isLinked && ! splitOnAxis && (
<InputControls { ...inputControlProps } />
) }
return (
<fieldset className="spacing-sizes-control">
<HStack className="spacing-sizes-control__header">
<BaseControl.VisualLabel
as="legend"
className="spacing-sizes-control__label"
>
{ label }
</BaseControl.VisualLabel>
{ ! hasOneSide && ! hasOnlyAxialSides && (
<SidesDropdown
label={ dropdownLabelText }
onChange={ setView }
sides={ sides }
value={ view }
/>
) }
</HStack>
{ renderControls() }
</fieldset>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@
* Internal dependencies
*/
import SpacingInputControl from './spacing-input-control';
import { LABELS } from './utils';
import { LABELS, ICONS, hasAxisSupport } from '../utils';

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

export default function AxialInputControls( {
minimumCustomValue,
onChange,
values,
onMouseOut,
onMouseOver,
sides,
spacingSizes,
type,
minimumCustomValue,
onMouseOver,
onMouseOut,
values,
} ) {
const createHandleOnChange = ( side ) => ( next ) => {
if ( ! onChange ) {
Expand All @@ -37,7 +37,7 @@ export default function AxialInputControls( {

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

return (
Expand All @@ -47,17 +47,18 @@ export default function AxialInputControls( {
side === 'vertical' ? values.top : values.left;
return (
<SpacingInputControl
value={ axisValue }
onChange={ createHandleOnChange( side ) }
label={ LABELS[ side ] }
key={ `spacing-sizes-control-${ side }` }
withInputField={ false }
icon={ ICONS[ side ] }
label={ LABELS[ side ] }
minimumCustomValue={ minimumCustomValue }
onChange={ createHandleOnChange( side ) }
onMouseOut={ onMouseOut }
onMouseOver={ onMouseOver }
side={ side }
spacingSizes={ spacingSizes }
type={ type }
minimumCustomValue={ minimumCustomValue }
onMouseOver={ onMouseOver }
onMouseOut={ onMouseOut }
value={ axisValue }
withInputField={ false }
/>
);
} ) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@
* Internal dependencies
*/
import SpacingInputControl from './spacing-input-control';
import { ALL_SIDES, LABELS } from './utils';
import { ALL_SIDES, LABELS, ICONS } from '../utils';

export default function BoxInputControls( {
values,
sides,
export default function SeparatedInputControls( {
minimumCustomValue,
onChange,
onMouseOut,
onMouseOver,
sides,
spacingSizes,
type,
minimumCustomValue,
onMouseOver,
onMouseOut,
values,
} ) {
// Filter sides if custom configuration provided, maintaining default order.
const filteredSides = sides?.length
Expand All @@ -31,17 +31,18 @@ export default function BoxInputControls( {
{ filteredSides.map( ( side ) => {
return (
<SpacingInputControl
value={ values[ side ] }
label={ LABELS[ side ] }
key={ `spacing-sizes-control-${ side }` }
withInputField={ false }
side={ side }
icon={ ICONS[ side ] }
label={ LABELS[ side ] }
minimumCustomValue={ minimumCustomValue }
onChange={ createHandleOnChange( side ) }
onMouseOut={ onMouseOut }
onMouseOver={ onMouseOver }
side={ side }
spacingSizes={ spacingSizes }
type={ type }
minimumCustomValue={ minimumCustomValue }
onMouseOver={ onMouseOver }
onMouseOut={ onMouseOut }
value={ values[ side ] }
withInputField={ false }
/>
);
} ) }
Expand Down
Loading