-
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 animations to navigation component (#24771)
* Navigation Component: Remove setActiveLevel child function arg (#24704) * remove setActiveLevel from public api * change to NavigationBackButton * return null for backButton if no parentLevel * Navigation Component: Expose __experimentalNavigation component (#24706) * Expose __experimentalNavigation component * fix typo * expose menut and menu-item as well * Loop over navigation levels and wrap with animate * Use map and set to organize items and levels * Simplify level and item logic * Remove unnecessary key prop * Fix parent level assignment * Add back in key prop to force animation * Use useMemo to map item data Co-authored-by: Paul Sealock <[email protected]>
- Loading branch information
Showing
5 changed files
with
120 additions
and
41 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,115 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import classnames from 'classnames'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useEffect, useState } from '@wordpress/element'; | ||
import { useEffect, useMemo, useState } from '@wordpress/element'; | ||
import { usePrevious } from '@wordpress/compose'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import Animate from '../animate'; | ||
import { Root } from './styles/navigation-styles'; | ||
import Button from '../button'; | ||
|
||
const Navigation = ( { activeItemId, children, data, rootTitle } ) => { | ||
const [ activeLevel, setActiveLevel ] = useState( 'root' ); | ||
|
||
const mapItemData = ( items ) => { | ||
return items.map( ( item ) => { | ||
const itemChildren = data.filter( ( i ) => i.parent === item.id ); | ||
return { | ||
...item, | ||
children: itemChildren, | ||
parent: item.parent || 'root', | ||
isActive: item.id === activeItemId, | ||
hasChildren: itemChildren.length > 0, | ||
}; | ||
const [ activeLevelId, setActiveLevelId ] = useState( 'root' ); | ||
|
||
const appendItemData = ( item ) => { | ||
return { | ||
...item, | ||
children: [], | ||
parent: item.id === 'root' ? null : item.parent || 'root', | ||
isActive: item.id === activeItemId, | ||
setActiveLevelId, | ||
}; | ||
}; | ||
|
||
const mapItems = ( itemData ) => { | ||
const items = new Map( | ||
[ | ||
{ id: 'root', parent: null, title: rootTitle }, | ||
...itemData, | ||
].map( ( item ) => [ item.id, appendItemData( item ) ] ) | ||
); | ||
|
||
items.forEach( ( item ) => { | ||
const parentItem = items.get( item.parent ); | ||
if ( parentItem ) { | ||
parentItem.children.push( item ); | ||
parentItem.hasChildren = true; | ||
} | ||
} ); | ||
|
||
return items; | ||
}; | ||
const items = [ { id: 'root', title: rootTitle }, ...mapItemData( data ) ]; | ||
|
||
const activeItem = items.find( ( item ) => item.id === activeItemId ); | ||
const level = items.find( ( item ) => item.id === activeLevel ); | ||
const levelItems = items.filter( ( item ) => item.parent === level.id ); | ||
const parentLevel = | ||
level.id === 'root' | ||
? null | ||
: items.find( ( item ) => item.id === level.parent ); | ||
const items = useMemo( () => mapItems( data ), [ | ||
data, | ||
activeItemId, | ||
rootTitle, | ||
] ); | ||
const activeItem = items.get( activeItemId ); | ||
const previousActiveLevelId = usePrevious( activeLevelId ); | ||
const level = items.get( activeLevelId ); | ||
const parentLevel = level && items.get( level.parent ); | ||
const isNavigatingBack = | ||
previousActiveLevelId && | ||
items.get( previousActiveLevelId ).parent === activeLevelId; | ||
|
||
useEffect( () => { | ||
if ( activeItem ) { | ||
setActiveLevel( activeItem.parent ); | ||
setActiveLevelId( activeItem.parent ); | ||
} | ||
}, [] ); | ||
|
||
const NavigationBackButton = ( { children: backButtonChildren } ) => { | ||
if ( ! parentLevel ) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<Button | ||
isPrimary | ||
onClick={ () => setActiveLevelId( parentLevel.id ) } | ||
> | ||
{ backButtonChildren } | ||
</Button> | ||
); | ||
}; | ||
|
||
return ( | ||
<Root className="components-navigation"> | ||
{ children( { | ||
level, | ||
levelItems, | ||
parentLevel, | ||
setActiveLevel, | ||
} ) } | ||
<Animate | ||
key={ level.id } | ||
type="slide-in" | ||
options={ { | ||
origin: isNavigatingBack ? 'right' : 'left', | ||
} } | ||
> | ||
{ ( { className: animateClassName } ) => ( | ||
<div | ||
className={ classnames( | ||
'components-navigation__level', | ||
animateClassName | ||
) } | ||
> | ||
{ children( { | ||
level, | ||
NavigationBackButton, | ||
parentLevel, | ||
} ) } | ||
</div> | ||
) } | ||
</Animate> | ||
</Root> | ||
); | ||
}; | ||
|
||
export default Navigation; | ||
export { default as NavigationMenu } from './menu'; | ||
export { default as NavigationMenuItem } from './menu-item'; |
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