Skip to content

Commit

Permalink
Spacing presets: add post and site editor UI support to allow selecti…
Browse files Browse the repository at this point in the history
…on of preset values for padding (#42173)

Co-authored-by: Glen Davies <[email protected]>
  • Loading branch information
glendaviesnz and Glen Davies authored Aug 14, 2022
1 parent 31fc7ed commit 04d57b3
Show file tree
Hide file tree
Showing 20 changed files with 1,069 additions and 19 deletions.
2 changes: 1 addition & 1 deletion lib/compat/wordpress-6.1/block-editor-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ function gutenberg_get_block_editor_settings( $settings ) {
unset( $settings['__experimentalFeatures']['spacing']['padding'] );
}
if ( isset( $settings['__experimentalFeatures']['spacing']['customSpacingSize'] ) ) {
$settings['disableCustomSpacingSize'] = ! $settings['__experimentalFeatures']['spacing']['customSpacingSize'];
$settings['disableCustomSpacingSizes'] = ! $settings['__experimentalFeatures']['spacing']['customSpacingSize'];
unset( $settings['__experimentalFeatures']['spacing']['customSpacingSize'] );
}

Expand Down
4 changes: 2 additions & 2 deletions lib/compat/wordpress-6.1/theme.json
Original file line number Diff line number Diff line change
Expand Up @@ -269,12 +269,12 @@
"blockGap": null,
"margin": false,
"padding": false,
"customSpacingSizes": true,
"customSpacingSize": true,
"units": [ "px", "em", "rem", "vh", "vw", "%" ],
"spacingScale": {
"operator": "*",
"increment": 1.5,
"steps": 10,
"steps": 7,
"mediumStep": 1.5,
"unit": "rem"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,11 @@ public function get_item_schema() {
'type' => 'array',
'context' => array( 'post-editor', 'site-editor', 'widgets-editor' ),
),
'disableCustomSpacingSizes' => array(
'description' => __( 'Disables custom spacing sizes.', 'gutenberg' ),
'type' => 'boolean',
'context' => array( 'post-editor', 'site-editor', 'widgets-editor' ),
),
),
);

Expand Down
1 change: 1 addition & 0 deletions packages/block-editor/src/components/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ export { default as URLInputButton } from './url-input/button';
export { default as URLPopover } from './url-popover';
export { __experimentalImageURLInputUI } from './url-popover/image-url-input-ui';
export { default as withColorContext } from './color-palette/with-color-context';
export { default as __experimentalSpacingSizesControl } from './spacing-sizes-control';

/*
* Content Related Components
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* WordPress dependencies
*/
import { __experimentalApplyValueToSides as applyValueToSides } from '@wordpress/components';

/**
* Internal dependencies
*/
import SpacingInputControl from './spacing-input-control';
import { getAllRawValue, isValuesMixed, isValuesDefined } from './utils';

export default function AllInputControl( {
onChange,
values,
sides,
spacingSizes,
type,
minimumCustomValue,
} ) {
const allValue = getAllRawValue( values );
const hasValues = isValuesDefined( values );
const isMixed = hasValues && isValuesMixed( values );

const handleOnChange = ( next ) => {
const nextValues = applyValueToSides( values, next, sides );
onChange( nextValues );
};

return (
<SpacingInputControl
value={ allValue }
onChange={ handleOnChange }
side={ 'all' }
spacingSizes={ spacingSizes }
isMixed={ isMixed }
type={ type }
minimumCustomValue={ minimumCustomValue }
/>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/**
* Internal dependencies
*/
import SpacingInputControl from './spacing-input-control';
import { LABELS } from './utils';

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

export default function AxialInputControls( {
onChange,
values,
sides,
spacingSizes,
type,
minimumCustomValue,
} ) {
const createHandleOnChange = ( side ) => ( next ) => {
if ( ! onChange ) {
return;
}
const nextValues = { ...values };

if ( side === 'vertical' ) {
nextValues.top = next;
nextValues.bottom = next;
}

if ( side === 'horizontal' ) {
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;

return (
<>
{ filteredSides.map( ( side ) => {
const axisValue =
side === 'vertical' ? values.top : values.left;
return (
<SpacingInputControl
value={ axisValue }
onChange={ createHandleOnChange( side ) }
label={ LABELS[ side ] }
key={ `spacing-sizes-control-${ side }` }
withInputField={ false }
side={ side }
spacingSizes={ spacingSizes }
type={ type }
minimumCustomValue={ minimumCustomValue }
/>
);
} ) }
</>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/**
* WordPress dependencies
*/
import { useState } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import { __experimentalText as Text } from '@wordpress/components';

/**
* 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';

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

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

const inputValues = values || DEFAULT_VALUES;
const hasInitialValue = isValuesDefined( values );
const hasOneSide = sides?.length === 1;

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

const toggleLinked = () => {
setIsLinked( ! isLinked );
};

const handleOnChange = ( nextValue ) => {
const newValues = { ...values, ...nextValue };
onChange( newValues );
};

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

return (
<fieldset role="region" className="component-spacing-sizes-control">
<Text as="legend">{ label }</Text>
{ ! hasOneSide && (
<LinkedButton onClick={ toggleLinked } isLinked={ isLinked } />
) }
{ isLinked && (
<AllInputControl
aria-label={ label }
{ ...inputControlProps }
/>
) }

{ ! isLinked && splitOnAxis && (
<AxialInputControls { ...inputControlProps } />
) }
{ ! isLinked && ! splitOnAxis && (
<InputControls { ...inputControlProps } />
) }
</fieldset>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* Internal dependencies
*/
import SpacingInputControl from './spacing-input-control';
import { ALL_SIDES, LABELS } from './utils';

export default function BoxInputControls( {
values,
sides,
onChange,
spacingSizes,
type,
minimumCustomValue,
} ) {
// Filter sides if custom configuration provided, maintaining default order.
const filteredSides = sides?.length
? ALL_SIDES.filter( ( side ) => sides.includes( side ) )
: ALL_SIDES;

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

onChange( nextValues );
};

return (
<>
{ filteredSides.map( ( side ) => {
return (
<SpacingInputControl
value={ values[ side ] }
label={ LABELS[ side ] }
key={ `spacing-sizes-control-${ side }` }
withInputField={ false }
side={ side }
onChange={ createHandleOnChange( side ) }
spacingSizes={ spacingSizes }
type={ type }
minimumCustomValue={ minimumCustomValue }
/>
);
} ) }
</>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* WordPress dependencies
*/
import { link, linkOff } from '@wordpress/icons';
import { __ } from '@wordpress/i18n';
import { Button, Tooltip } from '@wordpress/components';

export default function LinkedButton( { isLinked, onClick } ) {
const label = isLinked ? __( 'Unlink Sides' ) : __( 'Link Sides' );

return (
<Tooltip text={ label }>
<span className="component-spacing-sizes-control__linked-button">
<Button
variant={ isLinked ? 'primary' : 'secondary' }
isSmall
icon={ isLinked ? link : linkOff }
iconSize={ 16 }
aria-label={ label }
onClick={ onClick }
/>
</span>
</Tooltip>
);
}
Loading

0 comments on commit 04d57b3

Please sign in to comment.