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

Update nav styles to match core designs #24987

Merged
merged 6 commits into from
Sep 3, 2020
Merged
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
11 changes: 6 additions & 5 deletions packages/components/src/navigation/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ import classnames from 'classnames';
/**
* WordPress dependencies
*/
import { Icon, chevronLeft } from '@wordpress/icons';
import { useEffect, useMemo, useState, useRef } from '@wordpress/element';
import { usePrevious } from '@wordpress/compose';

/**
* Internal dependencies
*/
import Animate from '../animate';
import { Root } from './styles/navigation-styles';
import Button from '../button';
import { BackButtonUI, Root } from './styles/navigation-styles';

const Navigation = ( { activeItemId, children, data, rootTitle } ) => {
const [ activeLevelId, setActiveLevelId ] = useState( 'root' );
Expand Down Expand Up @@ -81,13 +81,14 @@ const Navigation = ( { activeItemId, children, data, rootTitle } ) => {
}

return (
<Button
<BackButtonUI
className="components-navigation__back-button"
isPrimary
isTertiary
onClick={ () => setActiveLevelId( parentLevel.id ) }
>
<Icon icon={ chevronLeft } />
{ backButtonChildren }
</Button>
</BackButtonUI>
);
};

Expand Down
11 changes: 7 additions & 4 deletions packages/components/src/navigation/menu-item.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@ import { Icon, chevronRight } from '@wordpress/icons';
* Internal dependencies
*/
import Button from '../button';
import { MenuItemUI, BadgeUI } from './styles/navigation-styles';
import Text from '../text';
import {
MenuItemUI,
MenuItemTitleUI,
BadgeUI,
} from './styles/navigation-styles';

const NavigationMenuItem = ( props ) => {
const {
Expand Down Expand Up @@ -54,13 +57,13 @@ const NavigationMenuItem = ( props ) => {
onClick={ handleClick }
{ ...linkProps }
>
<Text
<MenuItemTitleUI
className="components-navigation__menu-item-title"
variant="body.small"
as="span"
>
{ title }
</Text>
</MenuItemTitleUI>
{ badge && (
<BadgeUI className="components-navigation__menu-item-badge">
{ badge }
Expand Down
20 changes: 17 additions & 3 deletions packages/components/src/navigation/menu.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,25 @@
/**
* Internal dependencies
*/
import { MenuUI } from './styles/navigation-styles';
import { MenuUI, MenuTitleUI } from './styles/navigation-styles';

const NavigationMenu = ( { children, title } ) => {
if ( ! children.length ) {
return null;
}

const NavigationMenu = ( { children } ) => {
return (
<MenuUI className="components-navigation__menu">{ children }</MenuUI>
<MenuUI className="components-navigation__menu">
{ title && (
<MenuTitleUI
variant="subtitle"
className="components-navigation__menu-title"
>
{ title }
</MenuTitleUI>
) }
<ul>{ children }</ul>
</MenuUI>
);
};

Expand Down
116 changes: 82 additions & 34 deletions packages/components/src/navigation/stories/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
*/
import { Button } from '@wordpress/components';
import { useState } from '@wordpress/element';
import { Icon, arrowLeft } from '@wordpress/icons';

/**
* External dependencies
*/
import styled from '@emotion/styled';

/**
* Internal dependencies
Expand Down Expand Up @@ -77,11 +81,43 @@ const data = [
id: 'item-5',
LinkComponent: CustomRouterLink,
},
{
title: 'Secondary Item 1',
id: 'secondary-item-1',
isSecondary: true,
},
{
title: 'Secondary Item 2',
id: 'secondary-item-2',
isSecondary: true,
},
{
title: 'Secondary Child 1',
id: 'secondary-child-1',
parent: 'secondary-item-1',
isSecondary: true,
},
{
title: 'Secondary Child 2',
id: 'secondary-child-2',
parent: 'secondary-item-1',
isSecondary: true,
},
];

function Example() {
const [ active, setActive ] = useState( 'item-1' );

const renderMenuItem = ( item ) => (
<NavigationMenuItem
{ ...item }
key={ item.id }
onClick={
! item.href ? ( selected ) => setActive( selected.id ) : null
}
/>
);

return (
<>
{ active !== 'child-2' ? (
Expand All @@ -92,43 +128,55 @@ function Example() {
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>
<Container>
<Navigation
activeItemId={ active }
data={ data }
rootTitle="Home"
>
{ ( { level, parentLevel, NavigationBackButton } ) => {
return (
<>
{ parentLevel && (
<NavigationBackButton>
{ parentLevel.title }
</NavigationBackButton>
) }
<NavigationMenu title={ level.title }>
{ level.children
.filter(
( item ) => ! item.isSecondary
)
.map( ( item ) =>
renderMenuItem( item )
) }
</NavigationMenu>
<NavigationMenu
title={
level.id === 'root'
? 'Secondary Menu'
: level.title
}
>
{ level.children
.filter( ( item ) => item.isSecondary )
.map( ( item ) =>
renderMenuItem( item )
) }
</NavigationMenu>
Copy link
Contributor

Choose a reason for hiding this comment

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

The consumer can decide if there is more than one. In the example, I just added isSecondary but this could be a slug or any other property to discern various menus. The title can also be dynamic here- also up to the consumer.

This is true, and this block is a nice example of the consumer having control. What if the entire navigation has 10 of these submenus throughout the hierarchy? Looking at the designs for FSE, its possible that may be the case. You'd need a block like this for every single submenu. level.children is getting filtered with each block. I think a better way exists and worth exploring.

</>
);
} }
</Navigation>
</Container>
</>
);
}

const Container = styled.div`
max-width: 246px;
`;

export const _default = () => {
return <Example />;
};
75 changes: 67 additions & 8 deletions packages/components/src/navigation/styles/navigation-styles.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,91 @@ import styled from '@emotion/styled';
/**
* Internal dependencies
*/
import { LIGHT_GRAY } from '../../utils/colors-values';
import { BASE, G2 } from '../../utils/colors-values';
import Button from '../../button';
import Text from '../../text';

export const Root = styled.div`
width: 100%;
background-color: ${ G2.darkGray.primary };
color: #f0f0f0;
padding: 8px;
overflow: hidden;
`;

export const MenuUI = styled.ul`
padding: 0;
margin: 0 0 32px 0;
export const MenuUI = styled.div`
margin-top: 24px;
margin-bottom: 24px;
display: flex;
flex-direction: column;
ul {
padding: 0;
margin: 0;
list-style: none;
}
`;

export const MenuTitleUI = styled( Text )`
padding: 4px 0 4px 16px;
margin-bottom: 8px;
`;

export const MenuItemUI = styled.li`
button {
border-radius: 2px;
color: ${ G2.lightGray.ui };

button,
a {
padding-left: 16px;
padding-right: 16px;
width: 100%;
color: ${ G2.lightGray.ui };

&:hover,
&:focus:not( [aria-disabled='true'] ):active,
&:active:not( [aria-disabled='true'] ):active {
color: #ddd;
}
}
&.is-active span {
border-bottom: 2px solid var( --wp-admin-theme-color );

&.is-active {
background-color: ${ BASE.black };
color: ${ G2.lightGray.secondary };

button,
a {
color: ${ G2.lightGray.secondary };
}
}

svg path {
color: ${ G2.lightGray.ui };
}
`;

export const BackButtonUI = styled( Button )`
&.is-tertiary {
color: ${ G2.lightGray.ui };

&:hover:not( :disabled ) {
color: #ddd;
box-shadow: none;
}

&:active:not( :disabled ) {
background: transparent;
color: #ddd;
}
}
`;

export const MenuItemTitleUI = styled( Text )`
margin-right: auto;
`;

export const BadgeUI = styled.span`
margin-left: 8px;
display: inline-flex;
padding: 4px 12px;
border-radius: 2px;
background-color: ${ LIGHT_GRAY[ 300 ] };
`;
18 changes: 17 additions & 1 deletion packages/components/src/navigation/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ const renderPane = () => ( { level, NavigationBackButton } ) => {
<>
<h2>{ level.title }</h2>
<NavigationBackButton>Back</NavigationBackButton>
<NavigationMenu>
<NavigationMenu title="Menu title">
{ level.children.map( ( item ) => {
return <NavigationMenuItem { ...item } key={ item.id } />;
} ) }
Expand Down Expand Up @@ -154,6 +154,22 @@ describe( 'Navigation', () => {
expect( menuItem.textContent ).toBe( 'Item 1' + '21' );
} );

it( 'should render menu titles when items exist', async () => {
const { rerender } = render(
<Navigation data={ [] }>{ renderPane() }</Navigation>
);

const emptyMenu = screen.queryByText( 'Menu title' );
expect( emptyMenu ).toBeNull();

rerender(
<Navigation data={ sampleData }>{ renderPane() }</Navigation>
);

const menuTitle = screen.queryByText( 'Menu title' );
expect( menuTitle ).not.toBeNull();
} );

it( 'should navigate up a level when clicking the back button', async () => {
render(
<Navigation data={ sampleData } rootTitle="Home">
Expand Down