Skip to content

Commit

Permalink
Navigation Component: Apply effect on activeItem change (#24958)
Browse files Browse the repository at this point in the history
* apply effect on activeItem change

* Update packages/components/src/navigation/stories/index.js

Co-authored-by: Joshua T Flowers <joshuatf@gmail.com>

Co-authored-by: Joshua T Flowers <joshuatf@gmail.com>
  • Loading branch information
psealock and joshuatf authored Sep 1, 2020
1 parent 5897bfa commit e652c32
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 32 deletions.
2 changes: 1 addition & 1 deletion packages/components/src/navigation/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ const Navigation = ( { activeItemId, children, data, rootTitle } ) => {
if ( activeItem ) {
setActiveLevelId( activeItem.parent );
}
}, [] );
}, [ activeItem ] );

const NavigationBackButton = ( { children: backButtonChildren } ) => {
if ( ! parentLevel ) {
Expand Down
74 changes: 43 additions & 31 deletions packages/components/src/navigation/stories/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,37 +83,49 @@ function Example() {
const [ active, setActive ] = useState( 'item-1' );

return (
<Navigation activeItemId={ active } data={ data } rootTitle="Home">
{ ( { level, parentLevel, NavigationBackButton } ) => {
return (
<>
{ parentLevel && (
<NavigationBackButton>
<Icon icon={ arrowLeft } />
{ parentLevel.title }
</NavigationBackButton>
) }
<h1>{ level.title }</h1>
<NavigationMenu>
{ level.children.map( ( item ) => {
return (
<NavigationMenuItem
{ ...item }
key={ item.id }
onClick={
! item.href
? ( selected ) =>
setActive( selected.id )
: null
}
/>
);
} ) }
</NavigationMenu>
</>
);
} }
</Navigation>
<>
{ active !== 'child-2' ? (
<Button
style={ { position: 'absolute', bottom: 0 } }
onClick={ () => setActive( 'child-2' ) }
>
Non-navigation link to Child 2
</Button>
) : null }
<Navigation activeItemId={ active } data={ data } rootTitle="Home">
{ ( { level, parentLevel, NavigationBackButton } ) => {
return (
<>
{ parentLevel && (
<NavigationBackButton>
<Icon icon={ arrowLeft } />
{ parentLevel.title }
</NavigationBackButton>
) }
<h1>{ level.title }</h1>
<NavigationMenu>
{ level.children.map( ( item ) => {
return (
<NavigationMenuItem
{ ...item }
key={ item.id }
onClick={
! item.href
? ( selected ) =>
setActive(
selected.id
)
: null
}
/>
);
} ) }
</NavigationMenu>
</>
);
} }
</Navigation>
</>
);
}

Expand Down

0 comments on commit e652c32

Please sign in to comment.