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

Navigation component: Store navigation tree in context #25292

Closed
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 106 additions & 0 deletions packages/components/src/navigation/children-utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/**
* External dependencies
*/
import { cloneDeep } from 'lodash';

/**
* WordPress dependencies
*/
import { Children } from '@wordpress/element';

/**
* Internal dependencies
*/
import { ROOT_MENU } from './constants';
import NavigationItem from './item';
import NavigationMenu from './menu';

/**
*
* @typedef {Object} ChildWithClosestParent
* @property {WPElement} child element that matched the predicate
* @property {WPElement?} parent closest matching parent that matched the parentPredicate
*/

/**
*
* @param {WPElement} initialChildren children to iterate through recursively
* @param {Function} predicate children matching this function predicate will be returned
* @param {Function} parentPredicate closest parent will be determined by this function predicate
*
* @return {ChildWithClosestParent[]} children with their closest parent
*/
export const findChildrenWithClosestParent = (
initialChildren,
predicate,
parentPredicate
) => {
const hasPredicate = typeof predicate === 'function';
const hasParentPredicate = typeof parentPredicate === 'function';

const innerRecursion = ( children, lastParent ) => {
let items = [];

Children.forEach( children, ( child ) => {
const predicateOk = hasPredicate && predicate( child );
if ( predicateOk ) {
items.push( { child, parent: lastParent } );
}

const hasChildren = child?.props?.children;
if ( hasChildren ) {
const parentPredicateOk =
hasParentPredicate && parentPredicate( child );
if ( parentPredicateOk ) {
lastParent = child;
}

items = items.concat(
findChildrenWithClosestParent(
child.props.children,
predicate,
parentPredicate,
lastParent
)
);
}
} );

return items;
};

return innerRecursion( initialChildren, null );
};
Copy link
Contributor

Choose a reason for hiding this comment

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

I'll happily leave someone else to review the recursion. 😄


export const findNavigationItems = ( children ) => {
const items = findChildrenWithClosestParent(
children,
( { type } ) => type === NavigationItem
);

return items.reduce( ( acc, { child, parent } ) => {
const key = child.props.item;

acc[ key ] = cloneDeep( child.props );
acc[ key ].menu = parent?.props?.menu ?? ROOT_MENU;
delete acc[ key ].children;

return acc;
}, {} );
};

export const findNavigationMenus = ( children ) => {
const menus = findChildrenWithClosestParent(
children,
( { type } ) => type === NavigationMenu
);

return menus.reduce( ( acc, { child } ) => {
const key = child?.props?.menu ?? ROOT_MENU;

acc[ key ] = cloneDeep( child.props );
delete acc[ key ].children;

return acc;
}, {} );
};
2 changes: 2 additions & 0 deletions packages/components/src/navigation/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,7 @@ export const NavigationContext = createContext( {
activeMenu: ROOT_MENU,
setActiveItem: noop,
setActiveMenu: noop,
items: {},
menus: {},
} );
export const useNavigationContext = () => useContext( NavigationContext );
13 changes: 13 additions & 0 deletions packages/components/src/navigation/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import Animate from '../animate';
import { ROOT_MENU } from './constants';
import { NavigationContext } from './context';
import { NavigationUI } from './styles/navigation-styles';
import { findNavigationItems, findNavigationMenus } from './children-utils';

export default function Navigation( {
activeItem,
Expand All @@ -28,6 +29,8 @@ export default function Navigation( {
const [ item, setItem ] = useState( activeItem );
const [ menu, setMenu ] = useState( activeMenu );
const [ slideOrigin, setSlideOrigin ] = useState();
const [ navigationItems, setNavigationItems ] = useState( {} );
const [ navigationMenus, setNavigationMenus ] = useState( {} );

const setActiveItem = ( itemId ) => {
setItem( itemId );
Expand Down Expand Up @@ -57,11 +60,21 @@ export default function Navigation( {
}
}, [ activeItem, activeMenu ] );

useEffect( () => {
const items = findNavigationItems( children );
const menus = findNavigationMenus( children );

setNavigationItems( items );
setNavigationMenus( menus );
}, [] );
Copy link
Contributor

Choose a reason for hiding this comment

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

Children might change after mount (e.g. dynamically fetched menu items), so we can't use the empty dependency array here.
But children is not a primitive, so using it as a dependency would run useEffect at every render, which might be a performance problem.

I'm not sure how to solve this conundrum, though. 🤔


const context = {
activeItem: item,
activeMenu: menu,
setActiveItem,
setActiveMenu,
items: navigationItems,
menus: navigationMenus,
};

const classes = classnames( 'components-navigation', className );
Expand Down