-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add classic menus to menu switcher (#38168)
* Add classic menus to switch menu dropdown * Preload entities * Hide classic menus when there are none * Memoize callback
- Loading branch information
Showing
4 changed files
with
141 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
packages/block-library/src/navigation/use-convert-classic-menu.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useCallback, useState, useEffect } from '@wordpress/element'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import useNavigationEntities from './use-navigation-entities'; | ||
import menuItemsToBlocks from './menu-items-to-blocks'; | ||
|
||
export default function useConvertClassicMenu( onFinish ) { | ||
const [ selectedMenu, setSelectedMenu ] = useState(); | ||
const [ | ||
isAwaitingMenuItemResolution, | ||
setIsAwaitingMenuItemResolution, | ||
] = useState( false ); | ||
const [ menuName, setMenuName ] = useState( '' ); | ||
|
||
const { menuItems, hasResolvedMenuItems } = useNavigationEntities( | ||
selectedMenu | ||
); | ||
|
||
const createFromMenu = useCallback( | ||
( name ) => { | ||
const { innerBlocks: blocks } = menuItemsToBlocks( menuItems ); | ||
onFinish( blocks, name ); | ||
}, | ||
[ menuItems, menuItemsToBlocks, onFinish ] | ||
); | ||
|
||
useEffect( () => { | ||
// If the user selected a menu but we had to wait for menu items to | ||
// finish resolving, then create the block once resolution finishes. | ||
if ( isAwaitingMenuItemResolution && hasResolvedMenuItems ) { | ||
createFromMenu( menuName ); | ||
setIsAwaitingMenuItemResolution( false ); | ||
} | ||
}, [ isAwaitingMenuItemResolution, hasResolvedMenuItems, menuName ] ); | ||
|
||
return useCallback( | ||
( id, name ) => { | ||
setSelectedMenu( id ); | ||
|
||
// If we have menu items, create the block right away. | ||
if ( hasResolvedMenuItems ) { | ||
createFromMenu( name ); | ||
return; | ||
} | ||
|
||
// Otherwise, create the block when resolution finishes. | ||
setIsAwaitingMenuItemResolution( true ); | ||
// Store the name to use later. | ||
setMenuName( name ); | ||
}, | ||
[ hasResolvedMenuItems, createFromMenu ] | ||
); | ||
} |