-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Block Support: Add gap block support feature #33991
Merged
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
a3bf1e3
Add gap block support feature based on #32571
andrewserong 361c744
Rename CSS variable, refactor to use single variable rather than spli…
andrewserong b716885
Switch block gap to be displayed in a single column
andrewserong 60491e4
Fix failing JS test
andrewserong 4550685
Try server-rendering the gap block support, and skip serialization on…
andrewserong a6d8df9
Fix regression where padding/margin values were skipping serialization
andrewserong b74b73a
Update types
andrewserong d53333c
Move blockGap support to spacing.php
andrewserong 7643f7f
Add PHP tests for spacing gap support server-side rendering
andrewserong c9d87a5
Fix whitespace alignment in spacing block support tests
andrewserong edb506e
Refactor server-rendering to use an inline style, update tests
andrewserong 016f4a7
Tweak regex to fix issue where wrapper has no style attribute, but ch…
andrewserong 1a22b15
Add additional tests
andrewserong 0d52de9
Align equals signs correctly
andrewserong f8a0932
Fix typo / phpcs warning
andrewserong a974db1
Remove gap from apply_spacing_support so it isn't applied twice for d…
andrewserong 276837e
Remove duplicate block gap style attribute from constants
andrewserong 530d8b5
Tweak regex
andrewserong 9a421ca
Rename support from customBlockGap to blockGap
andrewserong 779afe5
Align whitespace
andrewserong File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if it's the responsibility of this function to do this check (as opposed to presave validation)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good question: my thinking here was that it'd be good to include some form of CSS value filtering as a defensive conditional so that the rendering logic doesn't depend on the value within the attribute being saved in the correct form, mostly to ensure we don't trust the inputs too much.
The attribute values themselves don't get run through
safecss_filter_attr
when saved (I think!), so this adds a tiny bit of hardening to the render logic, where we assume that we'll have a fairly limited range of desired simple values being entered into the gap block support.Happy to change this if you think it's better to pull it out or add a wider range of allowed characters.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well I'm not an expert here :) so I'm happy if we keep it this way. Maybe @jorgefilipecosta would have a more informed opinion.