Style Book: Fix useForcedLayout
to prevent breaking style book
#8243
Conversation
This is because by the time we reach this line, innerBlocks will be an empty array (or we wouldn't make it this far) and if nextBlocks contains ANY items it will, by definition be unequal, so a length check is simpler and more performant. Also we can remove the dependence on yet another lodash function by doing it this way.
The release ZIP for this PR is accessible via:
Script Dependencies ReportThere is no changed script dependency between this branch and trunk. This comment was automatically generated by the TypeScript Errors Report
assets/js/blocks/cart-checkout-shared/use-forced-layout.ts
|
Size Change: 0 B Total Size: 1.08 MB
ℹ️ View Unchanged
|
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.
@@ -116,6 +115,7 @@ export const useForcedLayout = ( { | |||
|
|||
const registry = useRegistry(); | |||
useEffect( () => { | |||
let templateSynced = false; |
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.
Wouldn't templateSynced
get reset on every re-render, or is the useRegistry
here stable?
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.
It seems stable, logging this happens only once when a block is rendered for the first time.
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.
Logic sounds fine to me.
useForcedLayout
to prevent breaking style bookuseForcedLayout
to prevent breaking style book
In this PR we will update
useForcedLayout
to check if it has already dispatchedreplaceInnerBlocks
- if it has we won't do that again.The hook subscribes to the block editor registry and the callback is executed when a data store changes. We updated the second argument of
subscribe
so it only listens for changes oncore/block-editor
.When the callback is executed, if the current block does not have any inner blocks, but it should, then
replaceInnerBlocks
is dispatched, causing these blocks to be added. Once they are added, then this shouldn't need to be done again, that is why we introduced thetemplatesSynced
variable to track this, per hook.Fixes #8179
Fixes #8177
Fixes #8178
Testing
Internal developer testing - do not include these in testing notes.
wp-content/plugins
directory, run:npx @wordpress/create-block -t @woocommerce/extend-cart-checkout-block newsletter-plugin
User Facing Testing
WooCommerce Visibility
Performance Impact
This should have a slight performance increase in the editor due to
useForcedLayout
doing less work once templates have already been synced.Changelog