-
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
Simplfy handling of save of Nav block uncontrolled inner blocks #45517
Conversation
Open in CodeSandbox Web Editor | VS Code | VS Code Insiders |
@@ -191,9 +191,6 @@ function Navigation( { | |||
__unstableMarkNextChangeAsNotPersistent, | |||
} = useDispatch( blockEditorStore ); | |||
|
|||
const [ hasSavedUnsavedInnerBlocks, setHasSavedUnsavedInnerBlocks ] = |
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.
We shouldn't need this state in the parent.
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.
Yeah, agree I don't think it's needed. I think it was only there to support the previous behavior where a menu would be created whenever UnsavedInnerBlocks
mounted. From memory there where issues where UnsavedInnerBlocks
would be unmounted and remounted and could cause duplicate menus to be created—the parent block needed a way to guard the menu creation process.
Now this only happens when a user selects/modifies the inner blocks, and I don't think as much guarding is needed. 👍
I think it's worth giving this a lot of manual testing to be sure, as it's hard to reason about some aspects of the nav block code and also core data resolution.
@@ -646,7 +649,8 @@ function Navigation( { | |||
// Consider this state as 'unsaved' and offer an uncontrolled version of inner blocks, | |||
// that automatically saves the menu as an entity when changes are made to the inner blocks. | |||
const hasUnsavedBlocks = hasUncontrolledInnerBlocks && ! isEntityAvailable; | |||
if ( hasUnsavedBlocks ) { | |||
|
|||
if ( hasUnsavedBlocks && ! isCreatingNavigationMenu ) { |
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.
Addition of ! isCreatingNavigationMenu
is important here. We shouldn't render the uncontrolled inner blocks if a menu is being created. That way we can defer all the UI logic for a "loading" or "saving" state to the main edit.js
component.
@@ -703,24 +707,11 @@ function Navigation( { | |||
overlayTextColor={ overlayTextColor } | |||
> | |||
<UnsavedInnerBlocks | |||
createNavigationMenu={ createNavigationMenu } |
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.
Here's where we pass down the primary creation function.
onSave={ ( post ) => { | ||
// Set some state used as a guard to prevent the creation of multiple posts. | ||
setHasSavedUnsavedInnerBlocks( true ); | ||
// Switch to using the wp_navigation entity. | ||
setRef( post.id ); | ||
|
||
showNavigationMenuStatusNotice( | ||
__( `New Navigation Menu created.` ) | ||
); | ||
} } |
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.
This is now all defer to the centralised logic within the edit
component which handles creation of menus. Saves maintaining another set of "loading" logic.
{ isSaving ? ( | ||
<Spinner | ||
className={ | ||
'wp-block-navigation__uncontrolled-inner-blocks-loading-indicator' | ||
} | ||
/> | ||
) : ( | ||
<Wrapper { ...innerBlocksProps } /> | ||
) } |
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.
We no longer require this logic as the component will not be rendered when creating a menu.
Size Change: -112 B (0%) Total Size: 1.29 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.
Tested and it does what it says on the tin. Also the PR is mostly removing code which is great :)
Co-authored-by: Daniel Richards <daniel.richards@automattic.com>
5e4395e
to
b0aecfa
Compare
Waiting on CI tests 🕐 |
What?
Simplifies and centralising the "saving" logic and rendering for uncontrolled inner blocks by defering to the implementation in the parent component (primary
edit.js
of the block).Why?
Having a separate usage of
createNavigationMenu
withinuncontroller-inner-blocks.js
was requiring the block to maintain a separate "saving" or "loading" state when creating menus from uncontroller inner blocks (for example converting a Page List into a menu).This was leading to potential UI bugs and also greatly increasing the complexity of the code within
<UnControlledInnerBlocks>
.This problem was encountered when working on #45443.
How?
With this change we pass in the "create" function and defer all UI handling for saving/loading to the parent component.
Benefits:
<UncontrolledInnerBlocks>
is no longer rendered if a menu is being created avoiding the need to maintain separeate "loading" UI within that component.UncontrolledInnerBlocks
around "loading" statessetState
call in the parentedit.js
which simplifies that implementationTesting Instructions
The main thing to check is the conversion of inner blocks to sync'd menu.
Delete all Navigation Menus & Classic Menus - http://localhost:8888/wp-admin/edit.php?post_type=wp_navigation
New Post
Add Nav block
Click on Page List block and click "Edit". Then "Convert".
See loading state whilst conversion takes place.
See a single Menu created.
Delete all Navigation Menus & Classic Menus - http://localhost:8888/wp-admin/edit.php?post_type=wp_navigation
New Post
Go to Code view mode and paste in the following:
Screenshots or screencast