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 control: Replace sides dropdwon with link button #65193

Merged
merged 4 commits into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
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' ),
madhusudhand marked this conversation as resolved.
Show resolved Hide resolved
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;
}
Loading