-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Extensibility: Define customClassname behavior as filtered blocks support #3472
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
e1e8138
Extensibility: Define customClassname behavior as filtered blocks sup…
youknowriad 6667db0
Extensibility: Fix extending the BlockEdit
youknowriad 6d94b55
Extensibility: Changes per review for the Generated ClassName extension
youknowriad 74b8cfa
Tests: Fix registration unit test post rebase
youknowriad 3d54d33
Blocks: Try to fix rerender issue for custom class name
gziolo 73c3e30
Components: Preserve fill ordering through unmounts
aduth 5c38f6a
Extensibility: Unify className word usage
gziolo 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
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,89 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { assign } from 'lodash'; | ||
import classnames from 'classnames'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { __ } from '@wordpress/i18n'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { hasBlockSupport } from '../api'; | ||
import InspectorControls from '../inspector-controls'; | ||
|
||
/** | ||
* Filters registered block settings, extending attributes with anchor using ID | ||
* of the first node | ||
* | ||
* @param {Object} settings Original block settings | ||
* @return {Object} Filtered block settings | ||
*/ | ||
export function addAttribute( settings ) { | ||
if ( hasBlockSupport( settings, 'customClassName', true ) ) { | ||
// Use Lodash's assign to gracefully handle if attributes are undefined | ||
settings.attributes = assign( settings.attributes, { | ||
className: { | ||
type: 'string', | ||
}, | ||
} ); | ||
} | ||
|
||
return settings; | ||
} | ||
|
||
/** | ||
* Override the default edit UI to include a new block inspector control for | ||
* assigning the anchor ID, if block supports anchor | ||
* | ||
* @param {Element} element Original edit element | ||
* @param {Object} props Props passed to BlockEdit | ||
* @return {Element} Filtered edit element | ||
*/ | ||
export function addInspectorControl( element, props ) { | ||
if ( hasBlockSupport( props.name, 'customClassName', true ) && props.focus ) { | ||
element = [ | ||
element, | ||
<InspectorControls key="inspector-custom-class-name"> | ||
<InspectorControls.TextControl | ||
label={ __( 'Additional CSS Class' ) } | ||
value={ props.attributes.className || '' } | ||
onChange={ ( nextValue ) => { | ||
props.setAttributes( { | ||
className: nextValue, | ||
} ); | ||
} } | ||
/> | ||
</InspectorControls>, | ||
]; | ||
} | ||
|
||
return element; | ||
} | ||
|
||
/** | ||
* Override props assigned to save component to inject anchor ID, if block | ||
* supports anchor. This is only applied if the block's save result is an | ||
* element and not a markup string. | ||
* | ||
* @param {Object} extraProps Additional props applied to save element | ||
* @param {Object} blockType Block type | ||
* @param {Object} attributes Current block attributes | ||
* @return {Object} Filtered props applied to save element | ||
*/ | ||
export function addSaveProps( extraProps, blockType, attributes ) { | ||
if ( hasBlockSupport( blockType, 'customClassName', true ) && attributes.className ) { | ||
extraProps.className = classnames( extraProps.className, attributes.className ); | ||
} | ||
|
||
return extraProps; | ||
} | ||
|
||
export default function customClassName( { addFilter } ) { | ||
addFilter( 'registerBlockType', 'core\custom-class-name-attribute', addAttribute ); | ||
addFilter( 'BlockEdit', 'core\custom-class-name-inspector-control', addInspectorControl ); | ||
addFilter( 'getSaveContent.extraProps', 'core\custom-class-name-save-props', addSaveProps ); | ||
} |
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.
Good catch, this is the issue with testing only for non-existence. It is very easy to satisfy 😃