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

Site Editor: Move page switcher to navigation panel #25620

Merged
merged 22 commits into from
Oct 14, 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
27 changes: 2 additions & 25 deletions packages/edit-site/src/components/header/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import { Button } from '@wordpress/components';
* Internal dependencies
*/
import MoreMenu from './more-menu';
import PageSwitcher from '../page-switcher';
import SaveButton from '../save-button';
import UndoButton from './undo-redo/undo';
import RedoButton from './undo-redo/redo';
Expand All @@ -35,27 +34,15 @@ export default function Header( {
isNavigationOpen,
onToggleNavigation,
} ) {
const {
deviceType,
hasFixedToolbar,
template,
page,
showOnFront,
} = useSelect( ( select ) => {
const { deviceType, hasFixedToolbar, template } = useSelect( ( select ) => {
const {
__experimentalGetPreviewDeviceType,
isFeatureActive,
getTemplateId,
getTemplatePartId,
getTemplateType,
getPage,
} = select( 'core/edit-site' );

const { getEntityRecord, getEditedEntityRecord } = select( 'core' );
const { show_on_front: _showOnFront } = getEditedEntityRecord(
'root',
'site'
);
Copy link
Contributor

Choose a reason for hiding this comment

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

I've just noticed that we are losing all the "show on front" logic from the page switcher. 🤔

if ( showOnFront === 'posts' )
pageGroups.posts.unshift( {
label: (
<>
{ __( 'All Posts' ) }
<Tooltip text={ __( 'Home' ) }>
<div>
<Icon icon={ home } />
</div>
</Tooltip>
</>
),
value: '/',
context: {
query: { categoryIds: [] },
queryContext: { page: 1 },
},
} );
return pageGroups;
},

Am I missing something?

Copy link
Member Author

Choose a reason for hiding this comment

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

Oooops 😄 Pushed a commit which re-adds it.

const { getEntityRecord } = select( 'core' );

const _templateId = getTemplateId();
return {
Expand All @@ -65,14 +52,11 @@ export default function Header( {
template: getEntityRecord( 'postType', 'wp_template', _templateId ),
templatePartId: getTemplatePartId(),
templateType: getTemplateType(),
page: getPage(),
showOnFront: _showOnFront,
};
}, [] );

const {
__experimentalSetPreviewDeviceType: setPreviewDeviceType,
setPage,
} = useDispatch( 'core/edit-site' );

const isLargeViewport = useViewportMatch( 'medium' );
Expand Down Expand Up @@ -109,13 +93,6 @@ export default function Header( {
<BlockToolbar hideDragHandle />
</div>
) }
<div className="edit-site-header__toolbar-switchers">
<PageSwitcher
showOnFront={ showOnFront }
activePage={ page }
onActivePageChange={ setPage }
/>
</div>
</div>
</div>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ export const TEMPLATES_GENERAL = [
export const TEMPLATES_POSTS = [ 'home', 'single' ];

export const MENU_ROOT = 'root';
export const MENU_CONTENT_CATEGORIES = 'content-categories';
export const MENU_CONTENT_PAGES = 'content-pages';
export const MENU_CONTENT_POSTS = 'content-posts';
export const MENU_TEMPLATE_PARTS = 'template-parts';
export const MENU_TEMPLATES = 'templates';
export const MENU_TEMPLATES_ALL = 'templates-all';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/**
* WordPress dependencies
*/
import { useState } from '@wordpress/element';
import {
__experimentalNavigation as Navigation,
__experimentalNavigationMenu as NavigationMenu,
__experimentalNavigationItem as NavigationItem,
} from '@wordpress/components';
import { useSelect } from '@wordpress/data';
import { __ } from '@wordpress/i18n';

/**
* Internal dependencies
*/
import ContentPagesMenu from './menus/content-pages';
import ContentCategoriesMenu from './menus/content-categories';
import ContentPostsMenu from './menus/content-posts';
import {
MENU_CONTENT_CATEGORIES,
MENU_CONTENT_PAGES,
MENU_CONTENT_POSTS,
} from './constants';

export default function ContentNavigation( { onActivateMenu } ) {
const [ activeMenu, setActiveMenu ] = useState( 'root' );
Copy link
Member Author

Choose a reason for hiding this comment

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

Having two Navigation components make things interesting. Should we store this in the store as well?

Copy link
Member

Choose a reason for hiding this comment

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

cc @Copons on this since he was advertising the ability to have multiple navigations in one view 😄

Copy link
Member

Choose a reason for hiding this comment

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

If it's working fine for now I'd defer the store migration for a separate PR.

Copy link
Contributor

Choose a reason for hiding this comment

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

advertising

😆
Well, I mean, the Navigation component is one part of the sidebar, so there are literally no reasons why a sidebar can't have more than one!

Also, IIRC the component's storybook used to have multiple Navigation in the same example (but this might have been just some WIP code on my machine, never released to the general public).

Copy link
Contributor

Choose a reason for hiding this comment

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

About the migration to Redux: let's do it in a follow up once #26003 is merged, so that we can nest it inside navigationPanel.


const page = useSelect(
( select ) => select( 'core/edit-site' ).getPage(),
[]
);

const handleActivateMenu = ( menu ) => {
setActiveMenu( menu );
onActivateMenu( menu );
};

return (
<Navigation
activeItem={ page && `content-${ page.path }` }
activeMenu={ activeMenu }
onActivateMenu={ handleActivateMenu }
>
<NavigationMenu title={ __( 'Content' ) }>
<NavigationItem
title={ __( 'Pages' ) }
navigateToMenu={ MENU_CONTENT_PAGES }
/>

<NavigationItem
title={ __( 'Categories' ) }
navigateToMenu={ MENU_CONTENT_CATEGORIES }
/>

<NavigationItem
title={ __( 'Posts' ) }
navigateToMenu={ MENU_CONTENT_POSTS }
/>
</NavigationMenu>

<ContentPagesMenu />
<ContentCategoriesMenu />
<ContentPostsMenu />
</Navigation>
);
}
Original file line number Diff line number Diff line change
@@ -1,99 +1,38 @@
/**
* WordPress dependencies
*/
import { useEffect, useRef } from '@wordpress/element';
import {
__experimentalNavigation as Navigation,
__experimentalNavigationMenu as NavigationMenu,
__experimentalNavigationItem as NavigationItem,
__experimentalNavigationBackButton as NavigationBackButton,
createSlotFill,
} from '@wordpress/components';
import { useDispatch, useSelect } from '@wordpress/data';
import { __ } from '@wordpress/i18n';
import { useState } from '@wordpress/element';
import { createSlotFill } from '@wordpress/components';

/**
* Internal dependencies
*/
import TemplatesMenu from './menus/templates';
import TemplatePartsMenu from './menus/template-parts';
import { MENU_ROOT, MENU_TEMPLATE_PARTS, MENU_TEMPLATES } from './constants';
import ContentNavigation from './content-navigation';
import TemplatesNavigation from './templates-navigation';
import { useSelect } from '@wordpress/data';
import { MENU_ROOT } from './constants';

export const {
Fill: NavigationPanelPreviewFill,
Slot: NavigationPanelPreviewSlot,
} = createSlotFill( 'EditSiteNavigationPanelPreview' );

const NavigationPanel = () => {
const ref = useRef();

useEffect( () => {
if ( ref.current ) {
ref.current.focus();
}
}, [ ref ] );

const { templateId, templatePartId, templateType, activeMenu } = useSelect(
( select ) => {
const {
getTemplateId,
getTemplatePartId,
getTemplateType,
getNavigationPanelActiveMenu,
} = select( 'core/edit-site' );

return {
templateId: getTemplateId(),
templatePartId: getTemplatePartId(),
templateType: getTemplateType(),
activeMenu: getNavigationPanelActiveMenu(),
};
},
const [ contentActiveMenu, setContentActiveMenu ] = useState( MENU_ROOT );
const templatesActiveMenu = useSelect(
( select ) => select( 'core/edit-site' ).getNavigationPanelActiveMenu(),
[]
);

const {
setTemplate,
setTemplatePart,
setNavigationPanelActiveMenu,
} = useDispatch( 'core/edit-site' );

return (
<div className="edit-site-navigation-panel">
<Navigation
activeItem={
'wp_template' === templateType
? `${ templateType }-${ templateId }`
: `${ templateType }-${ templatePartId }`
}
activeMenu={ activeMenu }
onActivateMenu={ setNavigationPanelActiveMenu }
>
{ activeMenu === MENU_ROOT && (
<NavigationBackButton
backButtonLabel={ __( 'Dashboard' ) }
className="edit-site-navigation-panel__back-to-dashboard"
href="index.php"
ref={ ref }
/>
) }

<NavigationMenu title={ __( 'Theme' ) }>
<NavigationItem
title={ __( 'Templates' ) }
navigateToMenu={ MENU_TEMPLATES }
/>

<NavigationItem
title={ __( 'Template Parts' ) }
navigateToMenu={ MENU_TEMPLATE_PARTS }
/>

<TemplatesMenu onActivateItem={ setTemplate } />
{ ( contentActiveMenu === MENU_ROOT ||
templatesActiveMenu !== MENU_ROOT ) && <TemplatesNavigation /> }

<TemplatePartsMenu onActivateItem={ setTemplatePart } />
</NavigationMenu>
</Navigation>
{ ( templatesActiveMenu === MENU_ROOT ||
contentActiveMenu !== MENU_ROOT ) && (
<ContentNavigation onActivateMenu={ setContentActiveMenu } />
) }

<NavigationPanelPreviewSlot />
</div>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* WordPress dependencies
*/
import { __experimentalNavigationMenu as NavigationMenu } from '@wordpress/components';
import { __ } from '@wordpress/i18n';

/**
* Internal dependencies
*/
import NavigationEntityItems from '../navigation-entity-items';
import { MENU_CONTENT_CATEGORIES, MENU_ROOT } from '../constants';

export default function ContentCategoriesMenu() {
return (
<NavigationMenu
menu={ MENU_CONTENT_CATEGORIES }
title={ __( 'Categories' ) }
parentMenu={ MENU_ROOT }
>
<NavigationEntityItems kind="taxonomy" name="category" />
</NavigationMenu>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* WordPress dependencies
*/
import { __experimentalNavigationMenu as NavigationMenu } from '@wordpress/components';
import { __ } from '@wordpress/i18n';

/**
* Internal dependencies
*/
import NavigationEntityItems from '../navigation-entity-items';
import { MENU_CONTENT_PAGES, MENU_ROOT } from '../constants';

export default function ContentPagesMenu() {
return (
<NavigationMenu
menu={ MENU_CONTENT_PAGES }
title={ __( 'Pages' ) }
parentMenu={ MENU_ROOT }
>
<NavigationEntityItems kind="postType" name="page" />
</NavigationMenu>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/**
* WordPress dependencies
*/
import {
__experimentalNavigationMenu as NavigationMenu,
__experimentalNavigationItem as NavigationItem,
} from '@wordpress/components';
import { useDispatch, useSelect } from '@wordpress/data';
import { __ } from '@wordpress/i18n';

/**
* Internal dependencies
*/
import NavigationEntityItems from '../navigation-entity-items';
import { MENU_CONTENT_POSTS, MENU_ROOT } from '../constants';

export default function ContentPostsMenu() {
const showOnFront = useSelect(
( select ) =>
select( 'core' ).getEditedEntityRecord( 'root', 'site' )
.show_on_front,
[]
);

const { setPage } = useDispatch( 'core/edit-site' );

const onActivateFrontItem = () => {
setPage( {
type: 'page',
path: '/',
context: {
query: { categoryIds: [] },
queryContext: { page: 1 },
},
} );
};

return (
<NavigationMenu
menu={ MENU_CONTENT_POSTS }
title={ __( 'Posts' ) }
parentMenu={ MENU_ROOT }
>
{ showOnFront === 'posts' && (
<NavigationItem
item={ 'content-/' }
title={ __( 'All Posts' ) }
onClick={ onActivateFrontItem }
/>
) }
<NavigationEntityItems kind="postType" name="post" />
</NavigationMenu>
);
}
Loading