Skip to content

Navigation: Next steps for supporting non-links in Navigation block and screen #24364

Closed
@noisysocks

Description

A complementary feature to Full Site Editing is that we want to allow users to add blocks to their navigation menus and widget areas even if they are not using a block-based theme. Exploration of how to support non-link blocks in a navigation menu began in #22656 and was further discussed in #22096 and #23915.

In this issue, I'll attempt to provide a complete overview of this feature and go over the next steps for building it. This supersedes the above linked issues.

Overview

Navigation block

The Navigation block allows non-link blocks to be added alongside Link blocks. To begin, we can support useful blocks like Search, Social Icons, Image, Separator and Spacer. In the medium term, we can look at supporting nearly all non-layout blocks as well as blocks for building complex mega menus such as Pages (#23689) and Submenu (#23745 (comment)).

It is a "tailored group" block (#23207 (comment)). Unlike Group, it allows the user to customise whether to show these blocks horizontally, vertically, spaced evenly, etc. Also, unlike Group, it uses (<nav> and <ul>) markup to denote the special semantic meaning of its content.

Navigation is a static block. Features such as background colour, foreground colour, and whether or not to display submenu indicators should be implemented using cascading CSS or block context. The Link block should be mostly static but with dynamic PHP code that adds the current-menu-item class when the user is currently on the page represented by that link.

Children of the Navigation block are automatically wrapped in a <li> element using the API described in #23915 (comment). In the medium term, a more flexible API (#23915 (comment)) should be used. This more flexible API would allow us to support blocks like Pages and Submenu inside Navigation while still retaining semantic list markup.

Navigation screen

The Navigation screen displays a single Navigation block. Each item in the Navigation is saved to the database as a menu item CPT. By default, only Link blocks may be added to the Navigation block inside the Navigation screen. This ensures that wp_nav_menu() works as it always has done and that existing themes do not need to be changed. It also means that plugins which use menu items for their functionality will continue to work.

Themes may opt in to allowing more blocks to be inserted the Navigation screen by declaring add_theme_support( 'block-navigation' ).

Non-link blocks that the user adds to the Navigation screen are then saved to the database as a menu item with type set to 'html'. Alternatively, the type can be 'blocks' which mirrors how block widgets are stored per #24267.

Opting in causes wp_nav_menu() to output a complete Navigation block using render_block() instead of Nav_Menu_Walker. This significantly changes the outputted markup. By declaring add_theme_support( 'block-navigation' ), a theme author is indicating that they have tested their theme with the Navigation block markup. Customisations such as mega menus will work as they do when using the Navigation block in a block-based theme. Note that, in a typical case, opting in allows a theme author to delete CSS because wp-block-library already contains Navigation block styling.

If a user switches from a theme that supports block navigation to one that doesn't, non-link blocks will no longer render on the frontend. This prevents the site's navigation from appearing visually broken as it is not common that a theme's existing styling will correctly style block markup. Existing non-link blocks in the Navigation screen will continue to display but are marked as invalid so that the user is able to replace them with Link blocks.

If a user disables the block-based Navigation screen using a plugin, non-link blocks will, no longer render on the frontend. Again, this prevents a site's navigation from appearing visually broken. Existing non-link blocks in the Navigation screen will display in nav-menus.php as a HTML menu item so that the user is able to replace them with a menu item.

Next steps

I'll be working through the list below in the coming weeks. Ping me if you'd like to help out with any of the items!

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

Labels

[Block] NavigationAffects the Navigation Block[Type] OverviewComprehensive, high level view of an area of focus often with multiple tracking issues

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions