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

Add navigation back button as separate component #25520

Merged
merged 2 commits into from
Sep 22, 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
1 change: 1 addition & 0 deletions packages/components/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ export { default as Modal } from './modal';
export { default as ScrollLock } from './scroll-lock';
export { NavigableMenu, TabbableContainer } from './navigable-container';
export { default as __experimentalNavigation } from './navigation';
export { default as __experimentalNavigationBackButton } from './navigation/back-button';
export { default as __experimentalNavigationGroup } from './navigation/group';
export { default as __experimentalNavigationItem } from './navigation/item';
export { default as __experimentalNavigationMenu } from './navigation/menu';
Expand Down
47 changes: 47 additions & 0 deletions packages/components/src/navigation/back-button/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* External dependencies
*/
import classnames from 'classnames';

/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { Icon, chevronLeft } from '@wordpress/icons';

/**
* Internal dependencies
*/
import { useNavigationContext } from '../context';
import { MenuBackButtonUI } from '../styles/navigation-styles';

export default function NavigationMenu( {
backButtonLabel,
className,
href,
onClick,
parentMenu,
} ) {
const { setActiveMenu, navigationTree } = useNavigationContext();

const classes = classnames(
'components-navigation__back-button',
className
);

const parentMenuTitle = navigationTree.getMenu( parentMenu )?.title;

return (
<MenuBackButtonUI
className={ classes }
isTertiary
href={ href }
onClick={ () =>
parentMenu ? setActiveMenu( parentMenu, 'right' ) : onClick
}
>
<Icon icon={ chevronLeft } />
{ backButtonLabel || parentMenuTitle || __( 'Back' ) }
</MenuBackButtonUI>
);
}
33 changes: 7 additions & 26 deletions packages/components/src/navigation/menu/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,13 @@
*/
import classnames from 'classnames';

/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { Icon, chevronLeft } from '@wordpress/icons';

/**
* Internal dependencies
*/
import { ROOT_MENU } from '../constants';
import { useNavigationContext } from '../context';
import {
MenuBackButtonUI,
MenuTitleUI,
MenuUI,
} from '../styles/navigation-styles';
import { MenuTitleUI, MenuUI } from '../styles/navigation-styles';
import NavigationBackButton from '../back-button';
import { NavigationMenuContext } from './context';
import { useNavigationTreeMenu } from './use-navigation-tree-menu';

Expand All @@ -32,11 +23,7 @@ export default function NavigationMenu( props ) {
title,
} = props;
useNavigationTreeMenu( props );
const {
activeMenu,
setActiveMenu,
navigationTree,
} = useNavigationContext();
const { activeMenu } = useNavigationContext();
const isActive = activeMenu === menu;

const classes = classnames( 'components-navigation__menu', className );
Expand All @@ -55,20 +42,14 @@ export default function NavigationMenu( props ) {
);
}

const parentMenuTitle = navigationTree.getMenu( parentMenu )?.title;

return (
<NavigationMenuContext.Provider value={ context }>
<MenuUI className={ classes }>
{ parentMenu && (
<MenuBackButtonUI
className="components-navigation__back-button"
isTertiary
onClick={ () => setActiveMenu( parentMenu, 'right' ) }
>
<Icon icon={ chevronLeft } />
{ backButtonLabel || parentMenuTitle || __( 'Back' ) }
</MenuBackButtonUI>
<NavigationBackButton
backButtonLabel={ backButtonLabel }
parentMenu={ parentMenu }
/>
) }
{ title && (
<MenuTitleUI
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export const NavigationUI = styled.div`
width: 100%;
background-color: ${ G2.darkGray.primary };
color: #f0f0f0;
padding: 8px;
padding: 0 8px;
overflow: hidden;
`;

Expand All @@ -29,6 +29,9 @@ export const MenuUI = styled.div`
margin: 0;
list-style: none;
}
.components-navigation__back-button {
margin-bottom: 24px;
}
`;

export const MenuBackButtonUI = styled( Button )`
Expand Down