-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Spacing presets: add post and site editor UI support to allow selecti…
…on of preset values for padding (#42173) Co-authored-by: Glen Davies <[email protected]>
- Loading branch information
1 parent
31fc7ed
commit 04d57b3
Showing
20 changed files
with
1,069 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
packages/block-editor/src/components/spacing-sizes-control/all-input-control.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } | ||
/> | ||
); | ||
} |
62 changes: 62 additions & 0 deletions
62
packages/block-editor/src/components/spacing-sizes-control/axial-input-controls.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } | ||
/> | ||
); | ||
} ) } | ||
</> | ||
); | ||
} |
91 changes: 91 additions & 0 deletions
91
packages/block-editor/src/components/spacing-sizes-control/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
); | ||
} |
46 changes: 46 additions & 0 deletions
46
packages/block-editor/src/components/spacing-sizes-control/input-controls.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } | ||
/> | ||
); | ||
} ) } | ||
</> | ||
); | ||
} |
25 changes: 25 additions & 0 deletions
25
packages/block-editor/src/components/spacing-sizes-control/linked-button.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
); | ||
} |
Oops, something went wrong.