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

Refactor the preferences modal to use the new Navigator components #35142

Merged
merged 8 commits into from
Sep 29, 2021
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
117 changes: 85 additions & 32 deletions packages/edit-post/src/components/preferences-modal/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,21 @@ import { get } from 'lodash';
* WordPress dependencies
*/
import {
__experimentalNavigation as Navigation,
__experimentalNavigationMenu as NavigationMenu,
__experimentalNavigationItem as NavigationItem,
__experimentalNavigatorProvider as NavigatorProvider,
__experimentalNavigatorScreen as NavigatorScreen,
__experimentalUseNavigator as useNavigator,
__experimentalItemGroup as ItemGroup,
__experimentalItem as Item,
__experimentalHStack as HStack,
__experimentalTruncate as Truncate,
FlexItem,
Modal,
TabPanel,
Button,
Card,
CardBody,
} from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { isRTL, __ } from '@wordpress/i18n';
import { useViewportMatch } from '@wordpress/compose';
import { useSelect, useDispatch } from '@wordpress/data';
import { useMemo, useCallback, useState } from '@wordpress/element';
Expand All @@ -26,6 +34,7 @@ import {
store as editorStore,
} from '@wordpress/editor';
import { store as coreStore } from '@wordpress/core-data';
import { chevronLeft, chevronRight, Icon } from '@wordpress/icons';

/**
* Internal dependencies
Expand All @@ -44,6 +53,21 @@ import BlockManager from '../block-manager';
const MODAL_NAME = 'edit-post/preferences';
const PREFERENCES_MENU = 'preferences-menu';

function NavigationButton( {
as: Tag = Button,
youknowriad marked this conversation as resolved.
Show resolved Hide resolved
path,
isBack = false,
...props
} ) {
const navigator = useNavigator();
return (
<Tag
onClick={ () => navigator.push( path, { isBack } ) }
{ ...props }
/>
);
}

export default function PreferencesModal() {
const isLargeViewport = useViewportMatch( 'medium' );
const { closeModal } = useDispatch( editPostStore );
Expand Down Expand Up @@ -301,34 +325,63 @@ export default function PreferencesModal() {
);
} else {
modalContent = (
<Navigation
activeMenu={ activeMenu }
onActivateMenu={ setActiveMenu }
>
<NavigationMenu menu={ PREFERENCES_MENU }>
{ tabs.map( ( tab ) => {
return (
<NavigationItem
key={ tab.name }
title={ tab.title }
navigateToMenu={ tab.name }
/>
);
} ) }
</NavigationMenu>
{ sections.map( ( section ) => {
return (
<NavigationMenu
key={ `${ section.name }-menu` }
menu={ section.name }
title={ section.tabLabel }
parentMenu={ PREFERENCES_MENU }
>
<NavigationItem>{ section.content }</NavigationItem>
</NavigationMenu>
);
} ) }
</Navigation>
<Card isBorderless>
<CardBody>
<NavigatorProvider initialPath="/">
<NavigatorScreen path="/">
<ItemGroup>
{ tabs.map( ( tab ) => {
return (
<NavigationButton
key={ tab.name }
path={ tab.name }
as={ Item }
isAction
>
<HStack justify="space-between">
youknowriad marked this conversation as resolved.
Show resolved Hide resolved
<FlexItem>
<Truncate>
{ tab.title }
</Truncate>
</FlexItem>
<FlexItem>
<Icon
icon={
isRTL()
? chevronLeft
: chevronRight
}
/>
</FlexItem>
</HStack>
</NavigationButton>
);
} ) }
</ItemGroup>
</NavigatorScreen>
{ sections.map( ( section ) => {
return (
<NavigatorScreen
key={ `${ section.name }-menu` }
path={ section.name }
>
<NavigationButton
path="/"
icon={
isRTL() ? chevronRight : chevronLeft
}
isBack
>
{ __( 'Back' ) }
</NavigationButton>
<h2>{ section.tabLabel }</h2>
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't we add a css class here and style better as in trunk?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I personally don't feel like the styles in trunk (the ones inherited from the menus UI) work well here. I prefer the default h2 styles, that said, I'm happy to update if y'all feel otherwise.

{ section.content }
</NavigatorScreen>
);
} ) }
</NavigatorProvider>
</CardBody>
</Card>
);
}
return (
Expand Down
Loading