-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Block Support: Add gap block support feature (#33991)
* Add gap block support feature based on #32571 * Server-render the gap block support, and skip serialization on save in the editor * Add PHP tests for spacing gap support server-side rendering * Rename support from customBlockGap to blockGap * Align whitespace
- Loading branch information
1 parent
32a370f
commit f0ee759
Showing
10 changed files
with
435 additions
and
10 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
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,128 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { __ } from '@wordpress/i18n'; | ||
import { Platform } from '@wordpress/element'; | ||
import { getBlockSupport } from '@wordpress/blocks'; | ||
import { | ||
__experimentalUseCustomUnits as useCustomUnits, | ||
__experimentalUnitControl as UnitControl, | ||
} from '@wordpress/components'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import useSetting from '../components/use-setting'; | ||
import { SPACING_SUPPORT_KEY } from './dimensions'; | ||
import { cleanEmptyObject } from './utils'; | ||
|
||
/** | ||
* Determines if there is gap support. | ||
* | ||
* @param {string|Object} blockType Block name or Block Type object. | ||
* @return {boolean} Whether there is support. | ||
*/ | ||
export function hasGapSupport( blockType ) { | ||
const support = getBlockSupport( blockType, SPACING_SUPPORT_KEY ); | ||
return !! ( true === support || support?.blockGap ); | ||
} | ||
|
||
/** | ||
* Checks if there is a current value in the gap block support attributes. | ||
* | ||
* @param {Object} props Block props. | ||
* @return {boolean} Whether or not the block has a gap value set. | ||
*/ | ||
export function hasGapValue( props ) { | ||
return props.attributes.style?.spacing?.blockGap !== undefined; | ||
} | ||
|
||
/** | ||
* Resets the gap block support attribute. This can be used when disabling | ||
* the gap support controls for a block via a progressive discovery panel. | ||
* | ||
* @param {Object} props Block props. | ||
* @param {Object} props.attributes Block's attributes. | ||
* @param {Object} props.setAttributes Function to set block's attributes. | ||
*/ | ||
export function resetGap( { attributes = {}, setAttributes } ) { | ||
const { style } = attributes; | ||
|
||
setAttributes( { | ||
style: { | ||
...style, | ||
spacing: { | ||
...style?.spacing, | ||
blockGap: undefined, | ||
}, | ||
}, | ||
} ); | ||
} | ||
|
||
/** | ||
* Custom hook that checks if gap settings have been disabled. | ||
* | ||
* @param {string} name The name of the block. | ||
* @return {boolean} Whether the gap setting is disabled. | ||
*/ | ||
export function useIsGapDisabled( { name: blockName } = {} ) { | ||
const isDisabled = ! useSetting( 'spacing.blockGap' ); | ||
return ! hasGapSupport( blockName ) || isDisabled; | ||
} | ||
|
||
/** | ||
* Inspector control panel containing the gap related configuration | ||
* | ||
* @param {Object} props | ||
* | ||
* @return {WPElement} Gap edit element. | ||
*/ | ||
export function GapEdit( props ) { | ||
const { | ||
attributes: { style }, | ||
setAttributes, | ||
} = props; | ||
|
||
const units = useCustomUnits( { | ||
availableUnits: useSetting( 'spacing.units' ) || [ | ||
'%', | ||
'px', | ||
'em', | ||
'rem', | ||
'vw', | ||
], | ||
} ); | ||
|
||
if ( useIsGapDisabled( props ) ) { | ||
return null; | ||
} | ||
|
||
const onChange = ( next ) => { | ||
const newStyle = { | ||
...style, | ||
spacing: { | ||
...style?.spacing, | ||
blockGap: next, | ||
}, | ||
}; | ||
|
||
setAttributes( { | ||
style: cleanEmptyObject( newStyle ), | ||
} ); | ||
}; | ||
|
||
return Platform.select( { | ||
web: ( | ||
<> | ||
<UnitControl | ||
label={ __( 'Block gap' ) } | ||
min={ 0 } | ||
onChange={ onChange } | ||
units={ units } | ||
value={ style?.spacing?.blockGap } | ||
/> | ||
</> | ||
), | ||
native: null, | ||
} ); | ||
} |
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
Oops, something went wrong.