-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Add animations to navigation component #24771
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
a98302b
Navigation Component: Remove setActiveLevel child function arg (#24704)
psealock ad6fdfe
Navigation Component: Expose __experimentalNavigation component (#24706)
psealock 89c0890
Loop over navigation levels and wrap with animate
joshuatf 6b4af35
Use map and set to organize items and levels
joshuatf cc35cb3
Simplify level and item logic
joshuatf 07aee41
Remove unnecessary key prop
joshuatf 93cdf76
Fix parent level assignment
joshuatf e9a8492
Add back in key prop to force animation
joshuatf 65ca0de
Use useMemo to map item data
joshuatf File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
*/ | ||
import { Button } from '@wordpress/components'; | ||
import { useState } from '@wordpress/element'; | ||
import { Icon, arrowLeft } from '@wordpress/icons'; | ||
|
||
/** | ||
* Internal dependencies | ||
|
@@ -48,6 +49,21 @@ const data = [ | |
id: 'child-2', | ||
parent: 'item-3', | ||
}, | ||
{ | ||
title: 'Nested Category', | ||
id: 'child-3', | ||
parent: 'item-3', | ||
}, | ||
{ | ||
title: 'Sub Child 1', | ||
id: 'sub-child-1', | ||
parent: 'child-3', | ||
}, | ||
{ | ||
title: 'Sub Child 2', | ||
id: 'sub-child-2', | ||
parent: 'child-3', | ||
}, | ||
{ | ||
title: 'External link', | ||
id: 'item-4', | ||
|
@@ -68,30 +84,25 @@ function Example() { | |
|
||
return ( | ||
<Navigation activeItemId={ active } data={ data } rootTitle="Home"> | ||
{ ( { level, levelItems, parentLevel, setActiveLevel } ) => { | ||
{ ( { level, parentLevel, NavigationBackButton } ) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. More simplification, nice! |
||
return ( | ||
<> | ||
{ parentLevel && ( | ||
<Button | ||
isPrimary | ||
onClick={ () => | ||
setActiveLevel( parentLevel.id ) | ||
} | ||
> | ||
Back | ||
</Button> | ||
<NavigationBackButton> | ||
<Icon icon={ arrowLeft } /> | ||
{ parentLevel.title } | ||
</NavigationBackButton> | ||
) } | ||
<h1>{ level.title }</h1> | ||
<NavigationMenu> | ||
{ levelItems.map( ( item ) => { | ||
{ level.children.map( ( item ) => { | ||
return ( | ||
<NavigationMenuItem | ||
{ ...item } | ||
key={ item.id } | ||
onClick={ ( selected ) => | ||
setActive( selected.id ) | ||
} | ||
setActiveLevel={ setActiveLevel } | ||
/> | ||
); | ||
} ) } | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍