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: Add template switcher to navigation panel #25615

Merged
merged 31 commits into from
Sep 29, 2020

Conversation

david-szabo97
Copy link
Member

@david-szabo97 david-szabo97 commented Sep 24, 2020

Related: #25262

Notes about code

  • template-switcher is a copy of the original template-switcher found in the header.
  • template-switcher/index.js is overhauled to work with the NavigationPanel
  • Changes in template-switcher/template-preview.js and template-switcher/theme-preview.js are minimal. Only changed the container's (very-first-div) className. Feel free to ignore these during review
  • reused as much as possible from the original template-switcher
  • original styles are used and added some overrides in the navigation-panel/styles.scss

Description

This change replicates the template switcher from the header in the Navigation sidebar.

How has this been tested?

  • yarn wp-env start
  • Enable FSE
  • Open Site Editor
  • Click site logo to toggle the navigation sidebar

Screenshots

Types of changes

New feature (non-breaking change which adds functionality)

Checklist:

  • My code is tested.
  • My code follows the WordPress code style.
  • My code follows the accessibility standards.
  • My code has proper inline documentation.
  • I've included developer documentation if appropriate.
  • I've updated all React Native files affected by any refactorings/renamings in this PR.

@david-szabo97 david-szabo97 added [Feature] Full Site Editing [Feature] Navigation Component A navigational waterfall component for hierarchy of items. labels Sep 24, 2020
@david-szabo97
Copy link
Member Author

  1. Should we remove the original template switcher from the header in this PR? I'm happy to do it but I wasn't sure how far we should go in one PR. Happy to do it and refactor all the styling and code.
  2. Since Template switcher is not a nested menu right now, we can't have multiple active items in the root menu

@github-actions
Copy link

github-actions bot commented Sep 24, 2020

Size Change: -56 B (0%)

Total Size: 1.17 MB

Filename Size Change
build/edit-site/index.js 20.4 kB -98 B (0%)
build/edit-site/style-rtl.css 3.76 kB +21 B (0%)
build/edit-site/style.css 3.77 kB +21 B (0%)
ℹ️ View Unchanged
Filename Size Change
build/a11y/index.js 1.14 kB 0 B
build/annotations/index.js 3.52 kB 0 B
build/api-fetch/index.js 3.35 kB 0 B
build/autop/index.js 2.72 kB 0 B
build/blob/index.js 620 B 0 B
build/block-directory/index.js 8.61 kB 0 B
build/block-directory/style-rtl.css 943 B 0 B
build/block-directory/style.css 942 B 0 B
build/block-editor/index.js 129 kB 0 B
build/block-editor/style-rtl.css 11.1 kB 0 B
build/block-editor/style.css 11.1 kB 0 B
build/block-library/editor-rtl.css 8.6 kB 0 B
build/block-library/editor.css 8.6 kB 0 B
build/block-library/index.js 135 kB 0 B
build/block-library/style-rtl.css 7.61 kB 0 B
build/block-library/style.css 7.6 kB 0 B
build/block-library/theme-rtl.css 741 B 0 B
build/block-library/theme.css 741 B 0 B
build/block-serialization-default-parser/index.js 1.78 kB 0 B
build/block-serialization-spec-parser/index.js 3.1 kB 0 B
build/blocks/index.js 47.5 kB 0 B
build/components/index.js 168 kB 0 B
build/components/style-rtl.css 15.4 kB 0 B
build/components/style.css 15.4 kB 0 B
build/compose/index.js 9.42 kB 0 B
build/core-data/index.js 12 kB 0 B
build/data-controls/index.js 1.27 kB 0 B
build/data/index.js 8.42 kB 0 B
build/date/index.js 31.9 kB 0 B
build/deprecated/index.js 772 B 0 B
build/dom-ready/index.js 568 B 0 B
build/dom/index.js 4.42 kB 0 B
build/edit-navigation/index.js 10.7 kB 0 B
build/edit-navigation/style-rtl.css 868 B 0 B
build/edit-navigation/style.css 871 B 0 B
build/edit-post/index.js 306 kB 0 B
build/edit-post/style-rtl.css 6.25 kB 0 B
build/edit-post/style.css 6.24 kB 0 B
build/edit-widgets/index.js 18.1 kB 0 B
build/edit-widgets/style-rtl.css 2.81 kB 0 B
build/edit-widgets/style.css 2.81 kB 0 B
build/editor/editor-styles-rtl.css 492 B 0 B
build/editor/editor-styles.css 493 B 0 B
build/editor/index.js 45.4 kB 0 B
build/editor/style-rtl.css 3.83 kB 0 B
build/editor/style.css 3.82 kB 0 B
build/element/index.js 4.44 kB 0 B
build/escape-html/index.js 733 B 0 B
build/format-library/index.js 7.49 kB 0 B
build/format-library/style-rtl.css 547 B 0 B
build/format-library/style.css 548 B 0 B
build/hooks/index.js 1.74 kB 0 B
build/html-entities/index.js 621 B 0 B
build/i18n/index.js 3.55 kB 0 B
build/is-shallow-equal/index.js 709 B 0 B
build/keyboard-shortcuts/index.js 2.39 kB 0 B
build/keycodes/index.js 1.85 kB 0 B
build/list-reusable-blocks/index.js 3.02 kB 0 B
build/list-reusable-blocks/style-rtl.css 476 B 0 B
build/list-reusable-blocks/style.css 476 B 0 B
build/media-utils/index.js 5.12 kB 0 B
build/notices/index.js 1.69 kB 0 B
build/nux/index.js 3.27 kB 0 B
build/nux/style-rtl.css 671 B 0 B
build/nux/style.css 668 B 0 B
build/plugins/index.js 2.44 kB 0 B
build/primitives/index.js 1.34 kB 0 B
build/priority-queue/index.js 789 B 0 B
build/redux-routine/index.js 2.85 kB 0 B
build/rich-text/index.js 13.5 kB 0 B
build/server-side-render/index.js 2.6 kB 0 B
build/shortcode/index.js 1.7 kB 0 B
build/token-list/index.js 1.24 kB 0 B
build/url/index.js 4.06 kB 0 B
build/viewport/index.js 1.74 kB 0 B
build/warning/index.js 1.13 kB 0 B
build/wordcount/index.js 1.17 kB 0 B

compressed-size-action

) }
</>
);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

This looks very similar to NavigationItemWithIcon, could we "merge" the 2 components?

Copy link
Member Author

Choose a reason for hiding this comment

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

Kinda different 🤔 I unified them but I'm not sure if it's any better this way. Both way seems a little bit confusing. Maybe it's too smart now.

{ icon && (
<div className="edit-site-template-switcher__label-home-icon">
<Icon icon={ icon } />
</div>
Copy link
Contributor

Choose a reason for hiding this comment

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

TemplateLabel wrapped the icon in Tooltip.
I think it's a nice and easy a11y win if we brought the same feature here.
We could add an iconLabel prop for the tooltip, that would default to title if not provided.

Copy link
Member Author

Choose a reason for hiding this comment

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

Done!

);
const blocks = useMemo(
() => ( template ? parse( template?.content?.raw || '' ) : [] ),
[ template ]
Copy link
Contributor

Choose a reason for hiding this comment

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

template is an object, are we sure this useMemo is only updating when template changes instead of every render?
Same goes for the item dependency of the useSelect above.

Do you think it would make more sense to destructure the item ID and use that as dependency?
e.g.

function Template Preview( { item } ) {
  const { id: itemId } = item;

  const template = useSelect( () => {}, [ itemId ] );
  const blocks = useMemo( () => {}, [ itemId ] );
}

Copy link
Member Author

Choose a reason for hiding this comment

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

How about using template?.content?.raw || '' as a dependency? Probably itemId is safe too.

/>
<div className="edit-site-template-switcher__theme-preview-description">
{ truncate(
/* Not using description.rendered here, as we might contain after an opening HTML tag. */
Copy link
Contributor

Choose a reason for hiding this comment

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

We should probably clarify this comment 😄

Copy link
Member Author

Choose a reason for hiding this comment

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

Done!

label: <TemplateLabel template={ templatePart } />,
value: templatePart.id,
slug: templatePart.slug,
} ) );
Copy link
Contributor

Choose a reason for hiding this comment

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

We probably don't need this map here, as we can just as easily do it once in the render below.
E.g.

{ templateParts?.map( ( templatePart ) => (
	<NavigationItem
		key={ `template-part-${ templatePart.id }` }
		item={ templatePart.slug }
		title={ <TemplateLabel template={ templatePart } /> }
		onClick={ () => onActiveTemplatePartIdChange( templatePart.id ) }
		onMouseEnter={ () => onHoverTemplatePart( templatePart.id ) }
		onMouseLeave={ () => onHoverTemplatePart( null ) }
	/>
) ) }

Note also that while there are no checks, title is supposed to be a string.
I guess that if we unify TemplateLabel and NavigationItemWithIcon this inconsistency wouldn't happen anymore.

Copy link
Member Author

Choose a reason for hiding this comment

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

🤔 If we are supposed to use string as title, then I need to copy-paste the whole part from the NavigationItem to get the same styling with a custom icon.

@Copons
Copy link
Contributor

Copons commented Sep 25, 2020

@david-szabo97 I totally failed to convey what I was thinking for the NavigationItemWithIcon.
My idea was to have them render a custom component, e.g.

	return (
		<NavigationItem item={ item } title={ title }>
			<Button { ...props }>
				{ title }
				{ icon && (
					<Tooltip text={ iconLabel || title }>
						<div className="edit-site-template-switcher__label-home-icon">
							<Icon icon={ icon } />
						</div>
					</Tooltip>
				) }
			</Button>
		</NavigationItem>
	);

Note that NavigationItem keeps the title (to be searchable), and gain an item prop, so that it can be activated.
All other props (e.g. onClick) are passed directly to the Button inside.

@Copons
Copy link
Contributor

Copons commented Sep 25, 2020

  1. Should we remove the original template switcher from the header in this PR? I'm happy to do it but I wasn't sure how far we should go in one PR. Happy to do it and refactor all the styling and code.

Yeah I guess there shouldn't be any harm here in removing the header's TemplateSwitcher (let's keep the PageSwitcher though!!). 🙂

  1. Since Template switcher is not a nested menu right now, we can't have multiple active items in the root menu

🤔 Do we have a case of multiple active items?
AFAICS we can either activate a template or a template part. Am I missing something?

If we need multiple active items, we should have multiple Navigation component, with their own states, btw.

activeItem={
templateType === 'wp_template_part' &&
`template-part-${ templatePartId }`
}
Copy link
Contributor

Choose a reason for hiding this comment

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

How do you feel if we use this to also also activate the template?

If we add the template ID as item to its own NavigationItem (e.g. item={ 'template-' + templateId }), then here we could do something like this:

<Navigation activeItem={ 'wp_template' === templateType
  ? `template-${ templateId }`
  : `template-part-${ templatePartId }`
} />

@david-szabo97
Copy link
Member Author

@Copons

  • Removed original template switcher (with the source as well, I'm not sure if we want to keep it, WDYT?)
  • Cleaned up CSS
  • Used custom children to avoid setting a component for title property (I was driven away by the fact we are losing some styling due to skipping the title prop, but it looks alright)
  • Template is now an actual item and can be active

🤔 Do we have a case of multiple active items?
AFAICS we can either activate a template or a template part. Am I missing something?

I was thinking about how we are going to put the template switcher and the page switcher on the same navigation. I guess they are going to be nested so we won't have multiple active items.

@vindl
Copy link
Member

vindl commented Sep 28, 2020

Removed original template switcher (with the source as well, I'm not sure if we want to keep it, WDYT?)

No need to keep the source, we can always bring it back from git history if needed.

I was thinking about how we are going to put the template switcher and the page switcher on the same navigation. I guess they are going to be nested so we won't have multiple active items.

We don't have to worry about pages for now, just templates.

@david-szabo97 david-szabo97 changed the title [WIP] Site Editor: Add template switcher to navigation panel Site Editor: Add template switcher to navigation panel Sep 29, 2020
Copy link
Member

@vindl vindl left a comment

Choose a reason for hiding this comment

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

This tests well for me but we'll need to update the failing e2e tests to account for template switcher changes. We also need to display all templates and not just the ones related to current page content selection, but that can also be done in a follow-up PR.

@david-szabo97 david-szabo97 force-pushed the add/navigation-panel-template-switcher branch from d0fa016 to ec18fc1 Compare September 29, 2020 13:36
@david-szabo97
Copy link
Member Author

david-szabo97 commented Sep 29, 2020

Rebased because of #25622. It added a slide-in animation to the navigation which seems to also occur when we are switching between template and template part 🤔

We are merging this and we will fix the animation in a separate PR.

@david-szabo97 david-szabo97 merged commit d3b04b8 into master Sep 29, 2020
@david-szabo97 david-szabo97 deleted the add/navigation-panel-template-switcher branch September 29, 2020 14:13
@github-actions github-actions bot added this to the Gutenberg 9.2 milestone Sep 29, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Navigation Component A navigational waterfall component for hierarchy of items.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants