diff --git a/packages/edit-site/src/components/navigation-sidebar/navigation-panel/index.js b/packages/edit-site/src/components/navigation-sidebar/navigation-panel/index.js index 88f470ca25e879..4ff4c225a23970 100644 --- a/packages/edit-site/src/components/navigation-sidebar/navigation-panel/index.js +++ b/packages/edit-site/src/components/navigation-sidebar/navigation-panel/index.js @@ -6,9 +6,11 @@ import classnames from 'classnames'; /** * WordPress dependencies */ +import { usePrevious } from '@wordpress/compose'; +import { store as coreDataStore } from '@wordpress/core-data'; +import { useSelect, useDispatch } from '@wordpress/data'; import { useState, useEffect, useRef } from '@wordpress/element'; import { ESCAPE } from '@wordpress/keycodes'; -import { useSelect, useDispatch } from '@wordpress/data'; /** * Internal dependencies @@ -22,7 +24,7 @@ const NavigationPanel = ( { isOpen } ) => { const [ contentActiveMenu, setContentActiveMenu ] = useState( MENU_ROOT ); const { templatesActiveMenu, siteTitle } = useSelect( ( select ) => { const { getNavigationPanelActiveMenu } = select( editSiteStore ); - const { getEntityRecord } = select( 'core' ); + const { getEntityRecord } = select( coreDataStore ); const siteData = getEntityRecord( 'root', '__unstableBase', undefined ) || {}; @@ -42,6 +44,15 @@ const NavigationPanel = ( { isOpen } ) => { } }, [ templatesActiveMenu ] ); + // Resets the content menu to its root whenever the navigation opens to avoid + // having it stuck on a sub-menu, interfering with the normal navigation behavior. + const prevIsOpen = usePrevious( isOpen ); + useEffect( () => { + if ( contentActiveMenu !== MENU_ROOT && isOpen && ! prevIsOpen ) { + setContentActiveMenu( MENU_ROOT ); + } + }, [ contentActiveMenu, isOpen ] ); + const { setIsNavigationPanelOpened } = useDispatch( editSiteStore ); const closeOnEscape = ( event ) => { @@ -69,12 +80,10 @@ const NavigationPanel = ( { isOpen } ) => {
- { ( contentActiveMenu === MENU_ROOT || - templatesActiveMenu !== MENU_ROOT ) && ( + { contentActiveMenu === MENU_ROOT && ( ) } - { ( templatesActiveMenu === MENU_ROOT || - contentActiveMenu !== MENU_ROOT ) && ( + { templatesActiveMenu === MENU_ROOT && ( diff --git a/packages/edit-site/src/components/navigation-sidebar/navigation-toggle/index.js b/packages/edit-site/src/components/navigation-sidebar/navigation-toggle/index.js index 7264f6cdc78a7f..6134af8565f838 100644 --- a/packages/edit-site/src/components/navigation-sidebar/navigation-toggle/index.js +++ b/packages/edit-site/src/components/navigation-sidebar/navigation-toggle/index.js @@ -5,6 +5,7 @@ import { useDispatch, useSelect } from '@wordpress/data'; import { Button, Icon } from '@wordpress/components'; import { __ } from '@wordpress/i18n'; import { wordpress } from '@wordpress/icons'; +import { store as coreDataStore } from '@wordpress/core-data'; /** * Internal dependencies @@ -12,9 +13,15 @@ import { wordpress } from '@wordpress/icons'; import { store as editSiteStore } from '../../../store'; function NavigationToggle( { icon, isOpen } ) { - const { isRequestingSiteIcon, siteIconUrl } = useSelect( ( select ) => { - const { getEntityRecord } = select( 'core' ); - const { isResolving } = select( 'core/data' ); + const { + isRequestingSiteIcon, + navigationPanelMenu, + siteIconUrl, + } = useSelect( ( select ) => { + const { getCurrentTemplateNavigationPanelSubMenu } = select( + editSiteStore + ); + const { getEntityRecord, isResolving } = select( coreDataStore ); const siteData = getEntityRecord( 'root', '__unstableBase', undefined ) || {}; @@ -24,11 +31,23 @@ function NavigationToggle( { icon, isOpen } ) { '__unstableBase', undefined, ] ), + navigationPanelMenu: getCurrentTemplateNavigationPanelSubMenu(), siteIconUrl: siteData.site_icon_url, }; }, [] ); - const { setIsNavigationPanelOpened } = useDispatch( editSiteStore ); + const { + openNavigationPanelToMenu, + setIsNavigationPanelOpened, + } = useDispatch( editSiteStore ); + + const toggleNavigationPanel = () => { + if ( isOpen ) { + setIsNavigationPanelOpened( false ); + return; + } + openNavigationPanelToMenu( navigationPanelMenu ); + }; let buttonIcon = ; @@ -55,7 +74,7 @@ function NavigationToggle( { icon, isOpen } ) {