Skip to content

Commit

Permalink
Sidebar: Restore Back buton 'go to parent' functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
noisysocks committed Jul 24, 2023
1 parent 500c09f commit 2a57746
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ import {
currentlyPreviewingTheme,
} from '../../utils/is-previewing-theme';
import { unlock } from '../../lock-unlock';
import { getPathFromURL } from '../sync-state-with-url/use-sync-path-with-url';

const { useHistory } = unlock( routerPrivateApis );
const { useLocation, useHistory } = unlock( routerPrivateApis );

export default function LeafMoreMenu( props ) {
const location = useLocation();
const history = useHistory();
const { block } = props;
const { clientId } = block;
Expand Down Expand Up @@ -63,22 +65,32 @@ export default function LeafMoreMenu( props ) {
attributes.type &&
history
) {
history.push( {
postType: attributes.type,
postId: attributes.id,
...( isPreviewingTheme() && {
wp_theme_preview: currentlyPreviewingTheme(),
} ),
} );
history.push(
{
postType: attributes.type,
postId: attributes.id,
...( isPreviewingTheme() && {
wp_theme_preview: currentlyPreviewingTheme(),
} ),
},
{
backPath: getPathFromURL( location.params ),
}
);
}
if ( name === 'core/page-list-item' && attributes.id && history ) {
history.push( {
postType: 'page',
postId: attributes.id,
...( isPreviewingTheme() && {
wp_theme_preview: currentlyPreviewingTheme(),
} ),
} );
history.push(
{
postType: 'page',
postId: attributes.id,
...( isPreviewingTheme() && {
wp_theme_preview: currentlyPreviewingTheme(),
} ),
},
{
backPath: getPathFromURL( location.params ),
}
);
}
},
[ history ]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,15 @@ import { unlock } from '../../lock-unlock';
const { useHistory } = unlock( routerPrivateApis );

const PageItem = ( { postType = 'page', postId, ...props } ) => {
const linkInfo = useLink( {
postType,
postId,
} );
const linkInfo = useLink(
{
postType,
postId,
},
{
backPath: '/page',
}
);
return <SidebarNavigationItem { ...linkInfo } { ...props } />;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { isRTL, __, sprintf } from '@wordpress/i18n';
import { chevronRight, chevronLeft } from '@wordpress/icons';
import { store as coreStore } from '@wordpress/core-data';
import { useSelect } from '@wordpress/data';
import { privateApis as routerPrivateApis } from '@wordpress/router';

/**
* Internal dependencies
Expand All @@ -23,6 +24,8 @@ import {
currentlyPreviewingTheme,
} from '../../utils/is-previewing-theme';

const { useLocation } = unlock( routerPrivateApis );

export default function SidebarNavigationScreen( {
isRoot,
title,
Expand All @@ -31,7 +34,7 @@ export default function SidebarNavigationScreen( {
content,
footer,
description,
backPath,
backPath: backPathProp,
} ) {
const { dashboardLink } = useSelect( ( select ) => {
const { getSettings } = unlock( select( editSiteStore ) );
Expand All @@ -40,6 +43,7 @@ export default function SidebarNavigationScreen( {
};
}, [] );
const { getTheme } = useSelect( coreStore );
const location = useLocation();
const navigator = useNavigator();
const theme = getTheme( currentlyPreviewingTheme() );
const icon = isRTL() ? chevronRight : chevronLeft;
Expand All @@ -56,30 +60,24 @@ export default function SidebarNavigationScreen( {
alignment="flex-start"
className="edit-site-sidebar-navigation-screen__title-icon"
>
{ ! isRoot && ! backPath && (
{ ! isRoot && (
<SidebarButton
onClick={ () => {
if ( navigator.location.isInitial ) {
navigator.goToParent( { replace: true } );
const backPath =
backPathProp ?? location.state?.backPath;
if ( backPath ) {
navigator.goTo( backPath, {
isBack: true,
} );
} else {
navigator.goBack();
navigator.goToParent();
}
} }
icon={ icon }
label={ __( 'Back' ) }
showTooltip={ false }
/>
) }
{ ! isRoot && backPath && (
<SidebarButton
onClick={ () =>
navigator.goTo( backPath, { isBack: true } )
}
icon={ icon }
label={ __( 'Back' ) }
showTooltip={ false }
/>
) }
{ isRoot && (
<SidebarButton
icon={ icon }
Expand Down

0 comments on commit 2a57746

Please sign in to comment.