Skip to content

Commit

Permalink
Spacing control: Replace sides dropdwon with link button (WordPress#6…
Browse files Browse the repository at this point in the history
…5193)

* replace sides dropdown with link button for spacing control

* remove sides dropdown

* lowercase the link button tooltip

* fix unit tests
  • Loading branch information
madhusudhand authored Sep 13, 2024
1 parent 1664022 commit 632d8d6
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 137 deletions.
23 changes: 10 additions & 13 deletions packages/block-editor/src/components/spacing-sizes-control/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ import {
__experimentalVStack as VStack,
} from '@wordpress/components';
import { useState } from '@wordpress/element';
import { __, _x, sprintf } from '@wordpress/i18n';
import { __, sprintf } from '@wordpress/i18n';

/**
* Internal dependencies
*/
import AxialInputControls from './input-controls/axial';
import SeparatedInputControls from './input-controls/separated';
import SingleInputControl from './input-controls/single';
import SidesDropdown from './sides-dropdown';
import LinkedButton from './linked-button';
import useSpacingSizes from './hooks/use-spacing-sizes';
import {
ALL_SIDES,
Expand Down Expand Up @@ -47,6 +47,10 @@ export default function SpacingSizesControl( {

const [ view, setView ] = useState( getInitialView( inputValues, sides ) );

const toggleLinked = () => {
setView( view === VIEWS.axial ? VIEWS.custom : VIEWS.axial );
};

const handleOnChange = ( nextValue ) => {
const newValues = { ...values, ...nextValue };
onChange( newValues );
Expand Down Expand Up @@ -91,12 +95,6 @@ export default function SpacingSizesControl( {
sideLabel
).trim();

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

return (
<fieldset className="spacing-sizes-control">
<HStack className="spacing-sizes-control__header">
Expand All @@ -107,11 +105,10 @@ export default function SpacingSizesControl( {
{ label }
</BaseControl.VisualLabel>
{ ! hasOneSide && ! hasOnlyAxialSides && (
<SidesDropdown
label={ dropdownLabelText }
onChange={ setView }
sides={ sides }
value={ view }
<LinkedButton
label={ labelProp }
onClick={ toggleLinked }
isLinked={ view === VIEWS.axial }
/>
) }
</HStack>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* WordPress dependencies
*/
import { Button, Tooltip } from '@wordpress/components';
import { link, linkOff } from '@wordpress/icons';
import { __, sprintf } from '@wordpress/i18n';

export default function LinkedButton( { isLinked, ...props } ) {
const label = isLinked
? sprintf(
// translators: 1. Type of spacing being modified (padding, margin, etc).
__( 'Unlink %1$s' ),
props.label.toLowerCase()
).trim()
: sprintf(
// translators: 1. Type of spacing being modified (padding, margin, etc).
__( 'Link %1$s' ),
props.label.toLowerCase()
).trim();

return (
<Tooltip text={ label }>
<Button
{ ...props }
size="small"
icon={ isLinked ? link : linkOff }
iconSize={ 24 }
aria-label={ label }
/>
</Tooltip>
);
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -373,8 +373,8 @@ describe( 'getInitialView', () => {
getInitialView( { top: '1em', right: '10px' }, ALL_SIDES )
).toBe( VIEWS.custom );
} );
it( 'should not return custom view if there is only a single side value', () => {
expect( getInitialView( { top: '1em' }, ALL_SIDES ) ).not.toBe(
it( 'should return custom view if there is only a single side value', () => {
expect( getInitialView( { top: '1em' }, ALL_SIDES ) ).toBe(
VIEWS.custom
);
} );
Expand All @@ -387,20 +387,19 @@ describe( 'getInitialView', () => {
);
} );

it( 'should return the single side view if there is only a single side value set and supported sides > 1', () => {
expect( getInitialView( { top: '1em' }, ALL_SIDES ) ).toBe(
VIEWS.top
);
expect( getInitialView( { right: '1em' }, ALL_SIDES ) ).toBe(
VIEWS.right
);
expect( getInitialView( { bottom: '1em' }, ALL_SIDES ) ).toBe(
VIEWS.bottom
);
expect( getInitialView( { left: '1em' }, ALL_SIDES ) ).toBe(
VIEWS.left
);
it( 'should return single side when only single side supported and no value defined', () => {
expect( getInitialView( {}, [ 'top' ] ) ).toBe( VIEWS.top );
} );

it( 'should return single side when only single side supported and all values defined', () => {
expect(
getInitialView(
{ top: '1em', right: '2em', bottom: '1em', left: '2em' },
[ 'top' ]
)
).toBe( VIEWS.top );
} );

it( 'should return single side view when only one side is supported', () => {
expect( getInitialView( { top: '1em' }, [ 'top' ] ) ).toBe(
VIEWS.top
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -364,31 +364,18 @@ export function getInitialView( values = {}, sides ) {
const hasNoValuesAndBalancedSides =
! sideValues.length && hasBalancedSidesSupport( sides );

// Only single side supported and no value defined.
if ( sides?.length === 1 ) {
return sides[ 0 ];
}

if (
hasAxisSupport( sides ) &&
( hasMatchingAxialValues || hasNoValuesAndBalancedSides )
) {
return VIEWS.axial;
}

// Single side.
// - Ensure the side returned is the first side that has a value.
if ( sideValues.length === 1 ) {
let side;

Object.entries( values ).some( ( [ key, value ] ) => {
side = key;
return value !== undefined;
} );

return side;
}

// Only single side supported and no value defined.
if ( sides?.length === 1 && ! sideValues.length ) {
return sides[ 0 ];
}

// Default to the Custom (separated sides) view.
return VIEWS.custom;
}

0 comments on commit 632d8d6

Please sign in to comment.