Repository navigation
Widgets: carry a declarative icon through the widget pipeline
#80969
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
Merged
Merged
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
97044d9
add icon to widget build manifest
retrofox 2394c05
add icon to widget type registry and REST
retrofox a0d796b
add icon resolver registry to widget primitives
retrofox a0aff5f
register dashboard icon resolver
retrofox cb554ef
register widget-owned icons server-side
retrofox ad4d4fc
move widget icons to widget.json
retrofox 2f82ef1
remove dead metadata from welcome widget module
retrofox 54c93e2
document icon resolution in widget-primitives
retrofox 0685da1
shorten widget icon comments
retrofox 157154b
update docs for declarative icon references
retrofox f7f559b
add icon reference story
retrofox 19d7eef
link changelog entries to the PR
retrofox cb0f68a
Merge remote-tracking branch 'origin/trunk' into update/widget-declar…
retrofox 8f32383
update package lock file
retrofox 9cf4c2c
Merge branch 'trunk' into update/widget-declarative-icon
retrofox a4a5e35
narrow metadata icon back to an element
retrofox 2f7e628
resolve icons off the loading gate
retrofox ef50796
restore currentColor on resolved icons
retrofox bb38b38
Merge remote-tracking branch 'origin/trunk' into update/widget-declar…
retrofox f9022d7
Merge remote-tracking branch 'origin/trunk' into update/widget-declar…
retrofox 4ae8c4a
hold the icon slot while references resolve
retrofox fd1e5c4
register the icon resolver at dashboard init
retrofox 44a41d4
Merge remote-tracking branch 'origin/trunk' into update/widget-declar…
retrofox f8a7e43
udpate pnpm lock file
retrofox File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| <?php | ||
| /** | ||
| * Widget-owned icons. | ||
| * | ||
| * Icons the widgets reference declaratively but that are not public in | ||
| * the `core` collection, registered under a collection the widgets own. | ||
| * | ||
| * @package gutenberg | ||
| */ | ||
|
|
||
| /** | ||
| * Registers the dashboard widgets icon collection and its icons. | ||
| * | ||
| * Content is sourced from the `@wordpress/icons` library files. | ||
| */ | ||
| function gutenberg_register_dashboard_widget_icons() { | ||
| if ( ! function_exists( 'wp_register_icon_collection' ) ) { | ||
| return; | ||
| } | ||
|
|
||
| // Register widget-dashboard icon collection | ||
| wp_register_icon_collection( | ||
| 'dashboard-widgets', | ||
| array( | ||
| 'label' => __( 'Dashboard Widgets', 'gutenberg' ), | ||
| 'description' => __( 'Icons owned by the dashboard widgets.', 'gutenberg' ), | ||
| ) | ||
| ); | ||
|
|
||
| // Register temporary draft icon since it's still a core private icon | ||
| wp_register_icon( | ||
| 'dashboard-widgets/drafts', | ||
| array( | ||
| 'label' => __( 'Drafts', 'gutenberg' ), | ||
| 'file_path' => gutenberg_dir_path() . 'packages/icons/src/library/drafts.svg', | ||
| ) | ||
| ); | ||
|
|
||
| // Register temporary WordPress icon since it's still a core private icon | ||
| wp_register_icon( | ||
| 'dashboard-widgets/wordpress', | ||
| array( | ||
| 'label' => __( 'WordPress', 'gutenberg' ), | ||
| 'file_path' => gutenberg_dir_path() . 'packages/icons/src/library/wordpress.svg', | ||
| ) | ||
| ); | ||
|
|
||
| // Register temporary site logo icon since it's still a core private icon | ||
| wp_register_icon( | ||
| 'dashboard-widgets/site-logo', | ||
| array( | ||
| 'label' => __( 'Site Logo', 'gutenberg' ), | ||
| 'file_path' => gutenberg_dir_path() . 'packages/icons/src/library/site-logo.svg', | ||
| ) | ||
| ); | ||
| } | ||
| add_action( 'init', 'gutenberg_register_dashboard_widget_icons' ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| /** | ||
| * External dependencies | ||
| */ | ||
| import parse from 'html-react-parser'; | ||
|
|
||
| /** | ||
| * WordPress dependencies | ||
| */ | ||
| import { store as coreStore } from '@wordpress/core-data'; | ||
| import { resolveSelect } from '@wordpress/data'; | ||
| import { safeHTML } from '@wordpress/dom'; | ||
| import { cloneElement, isValidElement } from '@wordpress/element'; | ||
| import { registerIconResolver } from '@wordpress/widget-primitives'; | ||
| import type { WidgetIcon } from '@wordpress/widget-primitives'; | ||
|
|
||
| /** | ||
| * Registers the dashboard's icon resolver: references resolve against | ||
| * the `icon` entity, and the record's SVG content becomes the element. | ||
| */ | ||
| export function registerDashboardIconResolver() { | ||
| registerIconResolver( async ( reference ) => { | ||
| const record = ( await resolveSelect( coreStore ).getEntityRecord( | ||
| 'root', | ||
| 'icon', | ||
| reference | ||
| ) ) as { content?: string } | undefined; | ||
|
|
||
| if ( ! record?.content ) { | ||
| return null; | ||
| } | ||
|
|
||
| /* | ||
| * Whitespace around the root `<svg>` makes `parse()` return an | ||
| * array; take the element. | ||
| */ | ||
| const parsed = parse( safeHTML( record.content.trim() ) ); | ||
| const found = Array.isArray( parsed ) | ||
| ? parsed.find( isValidElement ) | ||
| : parsed; | ||
|
|
||
| if ( ! isValidElement( found ) ) { | ||
| return null; | ||
| } | ||
|
|
||
| const element = found as WidgetIcon; | ||
|
|
||
| /* | ||
| * The registry sanitizer strips `fill` from the root `<svg>`; | ||
| * restore inheritance so icons follow the surrounding color. | ||
| */ | ||
| return element.props.fill | ||
| ? element | ||
| : cloneElement( element, { fill: 'currentColor' } ); | ||
| } ); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 found that
<svg>element now doesn't havefill="currentColor"anymore. It's near-invisible on white (#1e1e1evs#000000), but I believe the icons have stopped responding to the token entirely.I think the underlying fix is to allow
fillon<svg>.Before:
After:
Uh oh!
There was an error while loading. Please reload this page.
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.
Good catch.
The sanitizer only allows
fillonpath/polygon, and on WP >= 7.0 it's Core's copy (class_existsguard), so allowing it on<svg>takes a Core patch; worth raising with #75715.Meanwhile, the resolver restores it client-side: parsed roots without
fillare cloned withfill="currentColor"(ef50796).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.
#75715 (comment) and #75715 (comment) for more context