-
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
Block editor: hooks: manage BlockListBlock filters in one place #56875
Changes from all commits
3aeb8dc
a4662a6
4d1f51b
b722b46
f4b426b
5279781
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -1,7 +1,6 @@ | |||
/** | /** | ||
* External dependencies | * External dependencies | ||
*/ | */ | ||
import classnames from 'classnames'; | |||
import { extend } from 'colord'; | import { extend } from 'colord'; | ||
import namesPlugin from 'colord/plugins/names'; | import namesPlugin from 'colord/plugins/names'; | ||
|
|
||
|
@@ -13,7 +12,7 @@ import { | ||
getBlockType, | getBlockType, | ||
hasBlockSupport, | hasBlockSupport, | ||
} from '@wordpress/blocks'; | } from '@wordpress/blocks'; | ||
import { createHigherOrderComponent, useInstanceId } from '@wordpress/compose'; | import { useInstanceId } from '@wordpress/compose'; | ||
import { addFilter } from '@wordpress/hooks'; | import { addFilter } from '@wordpress/hooks'; | ||
import { useMemo, useEffect } from '@wordpress/element'; | import { useMemo, useEffect } from '@wordpress/element'; | ||
|
|
||
|
@@ -178,6 +177,7 @@ function DuotonePanelPure( { style, setAttributes, name } ) { | ||
export default { | export default { | ||
shareWithChildBlocks: true, | shareWithChildBlocks: true, | ||
edit: DuotonePanelPure, | edit: DuotonePanelPure, | ||
useBlockProps, | |||
attributeKeys: [ 'style' ], | attributeKeys: [ 'style' ], | ||
hasSupport( name ) { | hasSupport( name ) { | ||
return hasBlockSupport( name, 'filter.duotone' ); | return hasBlockSupport( name, 'filter.duotone' ); | ||
|
@@ -212,7 +212,7 @@ function addDuotoneAttributes( settings ) { | ||
return settings; | return settings; | ||
} | } | ||
|
|
||
function DuotoneStyles( { | function useDuotoneStyles( { | ||
clientId, | clientId, | ||
id: filterId, | id: filterId, | ||
selector: duotoneSelector, | selector: duotoneSelector, | ||
|
@@ -310,23 +310,12 @@ function DuotoneStyles( { | ||
blockElement.style.display = display; | blockElement.style.display = display; | ||
} | } | ||
}, [ isValidFilter, blockElement ] ); | }, [ isValidFilter, blockElement ] ); | ||
|
|||
return null; | |||
} | } | ||
|
|
||
/** | function useBlockProps( { name, style } ) { | ||
* Override the default block element to include duotone styles. | const id = useInstanceId( useBlockProps ); | ||
* | |||
* @param {Function} BlockListBlock Original component. | |||
* | |||
* @return {Function} Wrapped component. | |||
*/ | |||
const withDuotoneStyles = createHigherOrderComponent( | |||
( BlockListBlock ) => ( props ) => { | |||
const id = useInstanceId( BlockListBlock ); | |||
|
|||
const selector = useMemo( () => { | const selector = useMemo( () => { | ||
const blockType = getBlockType( props.name ); | const blockType = getBlockType( name ); | ||
|
|
||
if ( blockType ) { | if ( blockType ) { | ||
// Backwards compatibility for `supports.color.__experimentalDuotone` | // Backwards compatibility for `supports.color.__experimentalDuotone` | ||
|
@@ -362,46 +351,28 @@ const withDuotoneStyles = createHigherOrderComponent( | ||
fallback: true, | fallback: true, | ||
} ); | } ); | ||
} | } | ||
}, [ props.name ] ); | }, [ name ] ); | ||
|
|
||
const attribute = props?.attributes?.style?.color?.duotone; | const attribute = style?.color?.duotone; | ||
|
|
||
const filterClass = `wp-duotone-${ id }`; | const filterClass = `wp-duotone-${ id }`; | ||
|
|
||
const shouldRender = selector && attribute; | const shouldRender = selector && attribute; | ||
|
|
||
const className = shouldRender | useDuotoneStyles( { | ||
? classnames( props?.className, filterClass ) | clientId: id, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @ellatrix I noticed this when refactoring block refs in #60945: you're passing an instance ID instead of the block's This is a bug introduced by this PR. The original implementation in #55288 by @andrewserong was correct. I don't know how to fix it: the |
|||
: props?.className; | id: filterClass, | ||
selector, | |||
attribute, | |||
} ); | |||
|
|
||
// CAUTION: code added before this line will be executed | return { | ||
// for all blocks, not just those that support duotone. Code added | className: shouldRender ? filterClass : '', | ||
// above this line should be carefully evaluated for its impact on | }; | ||
// performance. | } | ||
return ( | |||
<> | |||
{ shouldRender && ( | |||
<DuotoneStyles | |||
clientId={ props.clientId } | |||
id={ filterClass } | |||
selector={ selector } | |||
attribute={ attribute } | |||
/> | |||
) } | |||
<BlockListBlock { ...props } className={ className } /> | |||
</> | |||
); | |||
}, | |||
'withDuotoneStyles' | |||
); | |||
|
|
||
addFilter( | addFilter( | ||
'blocks.registerBlockType', | 'blocks.registerBlockType', | ||
'core/editor/duotone/add-attributes', | 'core/editor/duotone/add-attributes', | ||
addDuotoneAttributes | addDuotoneAttributes | ||
); | ); | ||
addFilter( | |||
'editor.BlockListBlock', | |||
'core/editor/duotone/with-styles', | |||
withDuotoneStyles | |||
); |
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.
Maybe useBlockEditProps, I can see us having useBlockSaveProps, or useBlockProps for both?
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.
Maybe we should keep it consistent with the block API?
useBlockProps
anduseBlockProps.save
? One is a hook and the other one is static.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.
Works for me.
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.
But yes, it's not entirely the same so maybe a different naming is appropriate.