-
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.
Update padding block support for configurable sides
This includes the addition of spacing.js which is a slight refactor so the padding support is no longer an ad-hoc addition within style.js and matches the approach with colors, typography, borders etc.
- Loading branch information
1 parent
8921929
commit 2fb3a26
Showing
5 changed files
with
172 additions
and
39 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { getBlockSupport } from '@wordpress/blocks'; | ||
import { Platform } from '@wordpress/element'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { PaddingEdit, useIsPaddingDisabled } from './padding'; | ||
import SpacingPanelControl from '../components/spacing-panel-control'; | ||
|
||
export const SPACING_SUPPORT_KEY = 'spacing'; | ||
|
||
/** | ||
* Inspector controls for spacing support. | ||
* | ||
* @param {Object} props Block props. | ||
* @return {WPElement} Inspector controls for spacing support features. | ||
*/ | ||
export function SpacingPanel( props ) { | ||
const isDisabled = useIsSpacingDisabled( props ); | ||
const isSupported = hasSpacingSupport( props.name ); | ||
|
||
if ( isDisabled || ! isSupported ) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<SpacingPanelControl key="spacing"> | ||
<PaddingEdit { ...props } /> | ||
</SpacingPanelControl> | ||
); | ||
} | ||
|
||
/** | ||
* Determine whether there is block support for padding or margins. | ||
* | ||
* @param {string} blockName Block name. | ||
* @return {boolean} Whether there is support. | ||
*/ | ||
export function hasSpacingSupport( blockName ) { | ||
if ( Platform.OS !== 'web' ) { | ||
return false; | ||
} | ||
|
||
const support = getBlockSupport( blockName, SPACING_SUPPORT_KEY ); | ||
|
||
return !! ( true === support || support?.padding || support?.margin ); | ||
} | ||
|
||
/** | ||
* Determines whether spacing support has been disabled. | ||
* | ||
* @param {Object} props Block properties. | ||
* @return {boolean} If spacing support is completely disabled. | ||
*/ | ||
const useIsSpacingDisabled = ( props = {} ) => { | ||
const paddingDisabled = useIsPaddingDisabled( props ); | ||
|
||
return paddingDisabled; | ||
}; | ||
|
||
/** | ||
* Custom hook to retrieve which padding/margin is supported | ||
* e.g. top, right, bottom or left. | ||
* | ||
* Sides are opted into by default. It is only if a specific side is set to | ||
* false that it is omitted. | ||
* | ||
* @param {string} blockName Block name. | ||
* @param {string} feature The feature custom sides relate to e.g. padding or margins. | ||
* @return {Object} Sides supporting custom margin. | ||
*/ | ||
export function useCustomSides( blockName, feature ) { | ||
const support = getBlockSupport( blockName, SPACING_SUPPORT_KEY ); | ||
|
||
// Return empty config when setting is boolean as theme isn't setting | ||
// arbitrary sides. | ||
if ( typeof support[ feature ] === 'boolean' ) { | ||
return {}; | ||
} | ||
|
||
return support[ feature ]; | ||
} |
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