Skip to content
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

Update unsaved navigation block flow #35976

Merged
merged 2 commits into from
Oct 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions packages/block-library/src/navigation/edit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import ResponsiveWrapper from './responsive-wrapper';
import NavigationInnerBlocks from './inner-blocks';
import NavigationMenuSelector from './navigation-menu-selector';
import NavigationMenuNameControl from './navigation-menu-name-control';
import UpgradeToNavigationMenu from './upgrade-to-navigation-menu';
import UnsavedInnerBlocks from './unsaved-inner-blocks';

function getComputedStyle( node ) {
return node.ownerDocument.defaultView.getComputedStyle( node );
Expand Down Expand Up @@ -74,6 +74,7 @@ function detectColors( colorsDetectionElement, setColor, setBackground ) {
function Navigation( {
attributes,
setAttributes,
isSelected,
clientId,
className,
backgroundColor,
Expand Down Expand Up @@ -201,13 +202,17 @@ function Navigation( {

// If the block has inner blocks, but no menu id, this was an older
// navigation block added before the block used a wp_navigation entity.
// Offer a UI to upgrade it to using the entity.
if ( hasExistingNavItems && navigationMenuId === undefined ) {
// Consider this 'unsaved'. Offer an uncontrolled version of inner blocks,
// with a prompt to 'save'.
const hasUnsavedBlocks =
hasExistingNavItems && navigationMenuId === undefined;
if ( hasUnsavedBlocks ) {
return (
<UpgradeToNavigationMenu
<UnsavedInnerBlocks
blockProps={ blockProps }
blocks={ innerBlocks }
onUpgrade={ ( post ) =>
isSelected={ isSelected }
onSave={ ( post ) =>
setAttributes( { navigationMenuId: post.id } )
}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ import { __ } from '@wordpress/i18n';
*/
import NavigationMenuNameModal from './navigation-menu-name-modal';

export default function UpgradeToNavigationMenu( {
export default function UnsavedInnerBlocks( {
blockProps,
blocks,
onUpgrade,
onSave,
isSelected,
} ) {
const innerBlocksProps = useInnerBlocksProps( blockProps, {
renderAppender: false,
Expand Down Expand Up @@ -50,26 +51,27 @@ export default function UpgradeToNavigationMenu( {

return (
<>
<Warning
actions={ [
<Button
key="upgrade"
onClick={ () => setIsModalVisible( true ) }
variant="primary"
<nav { ...blockProps }>
{ isSelected && (
<Warning
className="wp-block-navigation__unsaved-changes-warning"
actions={ [
<Button
key="save"
onClick={ () => setIsModalVisible( true ) }
variant="primary"
>
{ __( 'Save as' ) }
</Button>,
] }
>
{ __( 'Upgrade' ) }
</Button>,
] }
>
{ __(
'The navigation block has been updated to store data in a similar way to a reusable block. Please use the upgrade option to save your navigation block data and continue editing your block.'
{ __( 'Save this block to continue editing.' ) }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This message is great for purposes of nav placeholders in patterns, but can be a bit confusing for existing navigation blocks (why do I suddenly need to save this block again?). I suppose we're not able to distinguish between the two? Not a blocker for this PR, but something to iterate on maybe.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I'm not sure what the best option is here. Let's continue improving it. It might be that this message isn't even a thing in the next iterations.

</Warning>
) }
</Warning>
<Disabled>
<nav { ...blockProps }>
<Disabled>
<div { ...innerBlocksProps } />
</nav>
</Disabled>
</Disabled>
</nav>
{ isModalVisible && (
<NavigationMenuNameModal
title={ __( 'Name your navigation menu' ) }
Expand All @@ -78,7 +80,7 @@ export default function UpgradeToNavigationMenu( {
} }
onFinish={ async ( title ) => {
const menu = await createNavigationMenu( title );
onUpgrade( menu );
onSave( menu );
} }
/>
) }
Expand Down
8 changes: 8 additions & 0 deletions packages/block-library/src/navigation/editor.scss
Original file line number Diff line number Diff line change
Expand Up @@ -521,3 +521,11 @@ body.editor-styles-wrapper
margin-top: $grid-unit-20;
}
}

.wp-block-navigation__unsaved-changes-warning {
width: 100%;

.block-editor-warning__actions {
margin-top: 0;
}
}