-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Navigation: Try adding navigation link variants via server #29095
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
Changes from all commits
2a2b518
fb39f6b
6e1a3fc
9925d70
ad227f6
6533ea2
6b07ad6
2a65e2e
9191e5e
7123e64
5247f56
b293fe6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -28,6 +28,9 @@ | |
| }, | ||
| "title": { | ||
| "type": "string" | ||
| }, | ||
| "kind": { | ||
| "type": "string" | ||
| } | ||
| }, | ||
| "usesContext": [ | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
| @@ -0,0 +1,75 @@ | ||||
| /** | ||||
| * WordPress dependencies | ||||
| */ | ||||
| import { addFilter } from '@wordpress/hooks'; | ||||
| import { | ||||
| category, | ||||
| page, | ||||
| postTitle, | ||||
| tag, | ||||
| customPostType, | ||||
| } from '@wordpress/icons'; | ||||
|
|
||||
| /** | ||||
| * Internal dependencies | ||||
| */ | ||||
| import fallbackVariations from './fallback-variations'; | ||||
|
|
||||
| function getIcon( variationName ) { | ||||
| switch ( variationName ) { | ||||
| case 'post': | ||||
| return postTitle; | ||||
| case 'page': | ||||
| return page; | ||||
| case 'tag': | ||||
| return tag; | ||||
| case 'category': | ||||
| return category; | ||||
| default: | ||||
| return customPostType; | ||||
| } | ||||
| } | ||||
|
|
||||
| function enhanceNavigationLinkVariations( settings, name ) { | ||||
| if ( name !== 'core/navigation-link' ) { | ||||
| return settings; | ||||
| } | ||||
|
|
||||
| // Fallback handling may be deleted after supported WP ranges understand the `variations` | ||||
| // property when passed to register_block_type_from_metadata in index.php | ||||
| if ( ! settings.variations ) { | ||||
| return { | ||||
| ...settings, | ||||
| variations: fallbackVariations, | ||||
| }; | ||||
| } | ||||
|
|
||||
| // Otherwise decorate server passed variations with an icon and isActive function | ||||
| if ( settings.variations ) { | ||||
| const isActive = ( blockAttributes, variationAttributes ) => { | ||||
| return blockAttributes.type === variationAttributes.type; | ||||
| }; | ||||
| const variations = settings.variations.map( ( variation ) => { | ||||
| return { | ||||
| ...variation, | ||||
| ...( ! variation.icon && { | ||||
| icon: getIcon( variation.name ), | ||||
| } ), | ||||
| ...( ! variation.isActive && { | ||||
| isActive, | ||||
| } ), | ||||
| }; | ||||
| } ); | ||||
| return { | ||||
| ...settings, | ||||
| variations, | ||||
| }; | ||||
| } | ||||
| return settings; | ||||
| } | ||||
|
|
||||
| addFilter( | ||||
|
||||
| export const __experimentalRegisterExperimentalCoreBlocks = |
Although it creates some indirection. The benefit would be that this code would be eliminated if the block isn't bundled. However, I assume that this block is going to be promoted to stable in WordPress 5.8 so 🤷🏻
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🤔 we might be able to avoid the icon handling if we say added string enum support for the @wordpress/icons package.
isActive is a bit trickier. Have we in practice seen different isActive functions for variant X and variant Y in the same variants list? We might be able to pull this up a level.
Although it creates some indirection. The benefit would be that this code would be eliminated if the block isn't bundled.
Oh interesting. I'd figure that the block already being behind the enableFSEBlocks flag would keep it out of the bundle. I'm open to experimenting with placement, though what it adds is probably pretty small.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's inevitable to use the filter on the client. I raised it mostly so you were aware of the implications it creates.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@gziolo Do folks mind if we keep this as is for now? The filter does run if we exclude the navigation link from the block library loader, but the alternative of adding it in __experimentalRegisterExperimentalCoreBlocks gets relatively messy.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@gwwar, I was thinking about how we could eventually get rid of the filter that has to inject
isActiveto every variation defined on the server. I share my thoughts in #30739.