-
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
Make nav components more atomic #24266
Changes from 11 commits
43c18f2
a4bbf76
ee380c5
f93fceb
9b7bc2f
937c3c7
3444b29
f93d0ad
e7c0011
53b85b2
7b68ca6
aca45b5
a5c4416
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,74 +1,48 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useState } from '@wordpress/element'; | ||
import { Icon, arrowLeft } from '@wordpress/icons'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import Button from '../button'; | ||
import Text from '../text'; | ||
import Item from './item'; | ||
|
||
const Navigation = ( { data, initial } ) => { | ||
const initialActive = data.find( ( item ) => item.slug === initial ); | ||
const [ active, setActive ] = useState( initialActive ); | ||
const parent = data.find( ( item ) => item.slug === active.parent ); | ||
const items = data.filter( ( item ) => item.parent === active.parent ); | ||
|
||
const goBack = () => { | ||
if ( ! parent.parent ) { | ||
// We are at top level, will need to handle this case. | ||
return; | ||
} | ||
const parentalSiblings = data.filter( | ||
( item ) => item.parent === parent.parent | ||
); | ||
if ( parentalSiblings.length ) { | ||
setActive( parentalSiblings[ 0 ] ); | ||
} | ||
const Navigation = ( { activeId, children, items: rawItems, rootTitle } ) => { | ||
const mapItemData = ( items ) => { | ||
return items.map( ( item ) => { | ||
const itemChildren = rawItems.filter( | ||
( i ) => i.parent === item.id | ||
); | ||
return { | ||
...item, | ||
children: itemChildren, | ||
parent: item.parent || 'root', | ||
isActive: item.id === activeId, | ||
hasChildren: itemChildren.length > 0, | ||
}; | ||
} ); | ||
}; | ||
const items = [ | ||
{ id: 'root', title: rootTitle }, | ||
...mapItemData( rawItems ), | ||
]; | ||
|
||
const activeItem = items.find( ( item ) => item.id === activeId ); | ||
const currentLevel = activeItem | ||
? items.find( ( item ) => item.id === activeItem.parent ) | ||
: { id: 'root', title: rootTitle }; | ||
const parentLevel = currentLevel | ||
? items.find( ( item ) => item.id === currentLevel.parent ) | ||
: null; | ||
const currentItems = currentLevel | ||
? items.filter( ( item ) => item.parent === currentLevel.id ) | ||
: items.filter( ( item ) => item.parent === 'root' ); | ||
const parentItems = parentLevel | ||
? items.filter( ( item ) => item.parent === parentLevel.id ) | ||
: []; | ||
const backItem = parentItems.length ? parentItems[ 0 ] : null; | ||
|
||
return ( | ||
<div className="components-navigation"> | ||
<Button | ||
isSecondary | ||
className="components-navigation__back" | ||
onClick={ goBack } | ||
> | ||
<Icon icon={ arrowLeft } /> | ||
{ parent.back } | ||
</Button> | ||
<div className="components-navigation__title"> | ||
<Text variant="title.medium">{ parent.title }</Text> | ||
</div> | ||
<div className="components-navigation__menu-items"> | ||
{ items.map( ( item ) => | ||
item.menu !== 'secondary' ? ( | ||
<Item | ||
key={ item.slug } | ||
data={ data } | ||
item={ item } | ||
setActive={ setActive } | ||
isActive={ item.slug === active.slug } | ||
/> | ||
) : null | ||
) } | ||
</div> | ||
<div className="components-navigation__menu-items is-secondary"> | ||
{ items.map( ( item ) => | ||
item.menu === 'secondary' ? ( | ||
<Item | ||
key={ item.slug } | ||
data={ data } | ||
item={ item } | ||
setActive={ setActive } | ||
isActive={ item.slug === active.slug } | ||
/> | ||
) : null | ||
) } | ||
</div> | ||
{ children( { | ||
activeId, | ||
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. Lets omit 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. Good call. Removed. |
||
backItem, | ||
currentItems, | ||
currentLevel, | ||
parentItems, | ||
parentLevel, | ||
} ) } | ||
</div> | ||
); | ||
}; | ||
|
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import classnames from 'classnames'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { Icon, chevronRight } from '@wordpress/icons'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import Button from '../button'; | ||
import Text from '../text'; | ||
|
||
const NavigationMenuItem = ( props ) => { | ||
const { children, hasChildren, isActive, onClick, title } = props; | ||
const classes = classnames( 'components-navigation__menu-item', { | ||
'is-active': isActive, | ||
} ); | ||
|
||
const handleClick = () => { | ||
onClick( children.length ? children[ 0 ] : props ); | ||
}; | ||
|
||
return ( | ||
<li className={ classes }> | ||
<Button className={ classes } onClick={ handleClick }> | ||
<Text variant="body.small"> | ||
<span>{ title }</span> | ||
</Text> | ||
{ hasChildren ? <Icon icon={ chevronRight } /> : null } | ||
</Button> | ||
</li> | ||
); | ||
}; | ||
|
||
export default NavigationMenuItem; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
const NavigationMenu = ( { children } ) => { | ||
return <ul className="components-navigation__menu">{ children }</ul>; | ||
}; | ||
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. Can the 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. I don't think we can. If 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. Right, good point. |
||
|
||
export default NavigationMenu; |
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.
How about calling the items prop
data
?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.
data
is much more generic than items. Do you consider the given data to not just be "items?"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.
No, this is why you have to rename them
rawItems
. MayberawItems
should bedata
if you are firm on keeping the external propsitems
.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.
Ah, I see your point. Updated this to
data
.