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 Site Editor’s reset of navigation panel menu #32011

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion packages/components/src/navigation/index.js
Expand Up @@ -24,6 +24,7 @@ export default function Navigation( {
activeMenu = ROOT_MENU,
children,
className,
skipAnimation,
onActivateMenu = noop,
} ) {
const [ menu, setMenu ] = useState( activeMenu );
Expand Down Expand Up @@ -73,7 +74,8 @@ export default function Navigation( {
<div
key={ menu }
className={ classnames( {
[ animateClassName ]: isMounted.current && slideOrigin,
[ animateClassName ]:
isMounted.current && ! skipAnimation && slideOrigin,
} ) }
>
<NavigationContext.Provider value={ context }>
Expand Down
Expand Up @@ -10,6 +10,7 @@ import {
__experimentalNavigation as Navigation,
__experimentalNavigationBackButton as NavigationBackButton,
} from '@wordpress/components';
import { usePrevious } from '@wordpress/compose';
import { store as coreDataStore } from '@wordpress/core-data';
import { useSelect, useDispatch } from '@wordpress/data';
import { useEffect, useRef } from '@wordpress/element';
Expand Down Expand Up @@ -76,6 +77,17 @@ const NavigationPanel = ( { isOpen } ) => {
}
}, [ activeMenu, isOpen ] );

// Sets active menu to root after the closing transition ends
const onTransitionEnd = ( event ) => {
if ( event.target === panelRef.current && ! isOpen ) {
setActive( MENU_ROOT );
}
};

// Skips menu slide animation if the panel has just opened or is closed
const wasOpen = usePrevious( isOpen );
const skipAnimation = ( isOpen && ! wasOpen ) || ! isOpen;

const closeOnEscape = ( event ) => {
if ( event.keyCode === ESCAPE && ! event.defaultPrevented ) {
event.preventDefault();
Expand All @@ -92,6 +104,7 @@ const NavigationPanel = ( { isOpen } ) => {
ref={ panelRef }
tabIndex="-1"
onKeyDown={ closeOnEscape }
onTransitionEnd={ onTransitionEnd }
>
<div className="edit-site-navigation-panel__inner">
<div className="edit-site-navigation-panel__site-title-container">
Expand All @@ -104,6 +117,7 @@ const NavigationPanel = ( { isOpen } ) => {
activeItem={ activeItem }
activeMenu={ activeMenu }
onActivateMenu={ setActive }
skipAnimation={ skipAnimation }
>
{ activeMenu === MENU_ROOT && (
<MainDashboardButton.Slot>
Expand Down
3 changes: 0 additions & 3 deletions packages/edit-site/src/store/reducer.js
Expand Up @@ -142,19 +142,16 @@ export function navigationPanel(
case 'SET_IS_NAVIGATION_PANEL_OPENED':
return {
...state,
menu: ! action.isOpen ? MENU_ROOT : state.menu, // Set menu to root when closing panel.
isOpen: action.isOpen,
};
case 'SET_IS_LIST_VIEW_OPENED':
return {
...state,
menu: state.isOpen && action.isOpen ? MENU_ROOT : state.menu, // Set menu to root when closing panel.
isOpen: action.isOpen ? false : state.isOpen,
};
case 'SET_IS_INSERTER_OPENED':
return {
...state,
menu: state.isOpen && action.value ? MENU_ROOT : state.menu, // Set menu to root when closing panel.
isOpen: action.value ? false : state.isOpen,
};
}
Expand Down
23 changes: 4 additions & 19 deletions packages/edit-site/src/store/test/reducer.js
Expand Up @@ -180,22 +180,7 @@ describe( 'state', () => {
} );
} );

it( 'should change the menu to root when closing the panel', () => {
const state = navigationPanel(
undefined,
openNavigationPanelToMenu( 'test-menu' )
);

expect( state.menu ).toEqual( 'test-menu' );
expect(
navigationPanel( state, setIsNavigationPanelOpened( false ) )
).toEqual( {
isOpen: false,
menu: 'root',
} );
} );

it( 'should close the navigation panel when opening the inserter and change the menu to root', () => {
it( 'should close the navigation panel when opening the inserter', () => {
const state = navigationPanel(
undefined,
openNavigationPanelToMenu( 'test-menu' )
Expand All @@ -206,11 +191,11 @@ describe( 'state', () => {
navigationPanel( state, setIsInserterOpened( true ) )
).toEqual( {
isOpen: false,
menu: 'root',
menu: 'test-menu',
} );
} );

it( 'should close the navigation panel when opening the list view and change the menu to root', () => {
it( 'should close the navigation panel when opening the list view', () => {
const state = navigationPanel(
undefined,
openNavigationPanelToMenu( 'test-menu' )
Expand All @@ -221,7 +206,7 @@ describe( 'state', () => {
navigationPanel( state, setIsListViewOpened( true ) )
).toEqual( {
isOpen: false,
menu: 'root',
menu: 'test-menu',
} );
} );

Expand Down