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

[Draft] Use template parts to preserve navigation between theme switches #36117

Closed
wants to merge 10 commits into from

Conversation

adamziel
Copy link
Contributor

@adamziel adamziel commented Nov 1, 2021

This PR preserves the navigation menu between theme switches as described in #36087.

Here's a demo:

CleanShot.2021-11-01.at.15.21.47.mp4

Upsides:

This PR reuses the template parts system so users don't need to learn new mental models. The PR doesn't change the user interface in any way. Anything supported by template parts just works, e.g. the isolated template part editor. Also, the change is relatively succinct and self-contained. It introduces the concept of "applying attributes on theme switch," but not much besides that.

Downsides:

  • Each navigation template part becomes a database entry on theme switch. This can be mitigated by removing unchanged template parts on theme switch.
  • The navigation template parts are not technically restricted just to the navigation blocks. This can be mitigated by introducing more specialized “Slots” restricted to specific blocks.
  • There is a naming convention for the navigation-related template parts (<name>-menu.html). This can be mitigated by listing these template parts explicitly in theme.json.

Missing:

  • Support for classic theme -> Block theme transition and vice versa (easy to add)
  • Guards against corner cases like an empty template part file and such

Test plan:

  1. Have two block themes. I installed Mayland (blocks) and cloned it as Mayland (blocks) 2
  2. Update them to include a primary-menu template part in header.html (e.g. <!-- wp:template-part {"slug":"primary-menu","tagName":"primary-menu","className":"primary-menu","layout":{"inherit":true}} /-->)
  3. Add a default implementation of that template part by creating a primary-menu.html file in block-template-parts of each theme. Add a navigation block to each: <!-- wp:navigation { "openSubmenusOnClick": false, "orientation": "horizontal", "overlayMenu": "mobile", "showSubmenuIcon": false } -->
  4. Add this template part to theme.json of both themes, use "area": "primary-menu"
  5. Go through the steps from the video above, confirm it all worked as expected.

@adamziel adamziel self-assigned this Nov 1, 2021
@adamziel adamziel added the [Block] Navigation Affects the Navigation Block label Nov 1, 2021
@adamziel adamziel changed the title An attempt at migrating navigation menus on theme switch via rewriting the navigationMenuId attribute from the old menu to the new one [Try] Use template parts to preserve navigation between theme switches Nov 1, 2021
@adamziel adamziel changed the title [Try] Use template parts to preserve navigation between theme switches [Draft] Use template parts to preserve navigation between theme switches Nov 1, 2021
@draganescu
Copy link
Contributor

This looks very smooth to me! 👏🏻

@@ -189,6 +192,16 @@ function gutenberg_get_allowed_template_part_areas() {
'icon' => 'header',
'area_tag' => 'header',
),
array(
'area' => WP_TEMPLATE_PART_AREA_PRIMARY_MENU,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

There could be an area per semantic menu type, or it could be more of a new, free field that can take any value..

@draganescu
Copy link
Contributor

Lovely that using semanting template parts (via areas) we can get rid of the naming convention. With the get_navigation_template_part_names approach we just filter all template parts.

The UX problem here is: how do I figure out that it's not the "header" itself that is "primary navigation" but the navigation template part inside that?

@adamziel
Copy link
Contributor Author

adamziel commented Nov 2, 2021

The UX problem here is: how do I figure out that it's not the "header" itself that is "primary navigation" but the navigation template part inside that?

I'm not sure I follow. What are the steps from "I open site editor" to "ughh I'm confused" here?

@adamziel
Copy link
Contributor Author

adamziel commented Nov 2, 2021

I added an alternative idea to this PR where "injections" are used.

In short, this would be the template part file:

<!-- wp:navigation { "initialNavigationMenuArea": "primary-menu" } -->

Saving it stores {"primary-menu": "<navigationMenuId>"} in the database. Rendering it backfills the navigationMenuId property with the stored data related to the primary-menu.

@@ -242,6 +242,16 @@ function render_block_core_navigation( $attributes, $content, $block ) {
$inner_blocks = new WP_Block_List( $parsed_blocks, $attributes );
}

if (
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This could use an inline comment to explain what it does.

@@ -11,6 +11,10 @@
],
"textdomain": "default",
"attributes": {
"initialNavigationMenuArea": {
"type": "string",
"default": ""
Copy link
Contributor Author

Choose a reason for hiding this comment

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

maybe we don't need the default here?

Comment on lines +669 to +679
/**
* TODO: Make changing nested entities affect parent entities in Gutenberg
* example:
* <!-- wp:template-part {"slug":"header"} -->
* <!-- wp:template-part {"slug":"primary-menu","tagName":"primary-menu","className":"primary-menu","layout":{"inherit":true}} -->
* <!-- wp:navigation { "navigationMenuId": {primary-menu} } -->
* <!-- /wp:template-part -->
* <!-- /wp:template-part -->
*
* If I set navigationMenuId to something else, it should save the primary-menu template part. Currently it doesn't and it seems like a bug.
*/
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This should be an issue, not a @todo comment

@adamziel
Copy link
Contributor Author

adamziel commented Nov 5, 2021

This ended up being implemented as a Navigation Area block in #36178, so I'm closing this PR.

@adamziel adamziel closed this Nov 5, 2021
@youknowriad youknowriad deleted the try/migrate-nav-on-theme-switch branch November 5, 2021 10:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Block] Navigation Affects the Navigation Block
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants