From 97044d9d45faf1445ac7368258ce29c1d6085c28 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dami=C3=A1n=20Su=C3=A1rez?= Date: Thu, 30 Jul 2026 13:01:31 +0100 Subject: [PATCH 01/19] add icon to widget build manifest carry widget.json icon into the generated PHP registry --- packages/wp-build/CHANGELOG.md | 3 ++- packages/wp-build/lib/build.mjs | 5 ++++- packages/wp-build/lib/widget-utils.mjs | 1 + 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/packages/wp-build/CHANGELOG.md b/packages/wp-build/CHANGELOG.md index 8514bb3d5f8396..7f063d89dfddab 100644 --- a/packages/wp-build/CHANGELOG.md +++ b/packages/wp-build/CHANGELOG.md @@ -6,6 +6,8 @@ - Widgets: carry a widget's declarative `actions` from `widget.json` into the generated PHP registry ([#80363](https://github.com/WordPress/gutenberg/pull/80363)). +- Widgets: carry a widget's declarative `icon` reference from `widget.json` + into the generated PHP registry ([#80938](https://github.com/WordPress/gutenberg/issues/80938)). ### Bug Fixes @@ -14,7 +16,6 @@ - Sync the page template `/?_fields=` preload path with the `_fields` list in `@wordpress/core-data` so the preload is consumed instead of the page issuing a duplicate request ([#80648](https://github.com/WordPress/gutenberg/pull/80648)). - Scope the generated page template's critical styles to `body.js` so the no-JS notice stays visible ([#80628](https://github.com/WordPress/gutenberg/pull/80628)). - ## 0.19.0 (2026-07-14) ### Enhancements diff --git a/packages/wp-build/lib/build.mjs b/packages/wp-build/lib/build.mjs index 83abe1aece3729..62d2ddb2969829 100755 --- a/packages/wp-build/lib/build.mjs +++ b/packages/wp-build/lib/build.mjs @@ -2021,7 +2021,7 @@ async function buildAllWidgets() { * Discover all widgets and collect their registry-facing data. * Widgets without a valid widget.json are skipped. * - * @return {Array<{ name: string, dirName: string, title: string | null, description: string | null, help: import('./widget-utils.mjs').WidgetHelpMetadata | null, actions: import('./widget-utils.mjs').WidgetActionMetadata[] | null, hasRender: boolean, hasWidget: boolean, presentation: string | null, category: string | null, keywords: string[] | null, textdomain: string | null }>} Array of widget objects. + * @return {Array<{ name: string, dirName: string, title: string | null, description: string | null, help: import('./widget-utils.mjs').WidgetHelpMetadata | null, icon: string | null, actions: import('./widget-utils.mjs').WidgetActionMetadata[] | null, hasRender: boolean, hasWidget: boolean, presentation: string | null, category: string | null, keywords: string[] | null, textdomain: string | null }>} Array of widget objects. */ function collectWidgets() { return getAllWidgets( ROOT_DIR ).flatMap( ( widgetName ) => { @@ -2043,6 +2043,7 @@ function collectWidgets() { title: metadata.title ?? null, description: metadata.description ?? null, help: metadata.help ?? null, + icon: metadata.icon ?? null, actions: metadata.actions ?? null, hasRender: widgetFiles.hasRender, hasWidget: widgetFiles.hasWidget, @@ -2199,6 +2200,7 @@ async function generateWidgetRegistry( widgets, replacements ) { const titleStr = toPhpStringLiteral( widget.title ); const descriptionStr = toPhpStringLiteral( widget.description ); const helpStr = toPhpHelpLiteral( widget.help ); + const iconStr = toPhpStringLiteral( widget.icon ); const actionsStr = toPhpActionsLiteral( widget.actions ); const keywordsStr = toPhpStringArrayLiteral( widget.keywords ); const textdomainStr = toPhpStringLiteral( widget.textdomain ); @@ -2208,6 +2210,7 @@ async function generateWidgetRegistry( widgets, replacements ) { 'title' => ${ titleStr }, 'description' => ${ descriptionStr }, 'help' => ${ helpStr }, + 'icon' => ${ iconStr }, 'actions' => ${ actionsStr }, 'has_render' => ${ hasRenderStr }, 'has_widget' => ${ hasWidgetStr }, diff --git a/packages/wp-build/lib/widget-utils.mjs b/packages/wp-build/lib/widget-utils.mjs index f67814b3f42ab3..afa85950cb93f2 100644 --- a/packages/wp-build/lib/widget-utils.mjs +++ b/packages/wp-build/lib/widget-utils.mjs @@ -50,6 +50,7 @@ export function getAllWidgets( rootDir ) { * @property {string} [title] Human-readable title. * @property {string} [description] Short description. * @property {WidgetHelpMetadata} [help] Contextual help note for compact surfaces. + * @property {string} [icon] Registered icon name (`collection/icon-name`). * @property {WidgetActionMetadata[]} [actions] Declarative actions the widget exposes. * @property {string} [category] Grouping category. * @property {'framed' | 'content-bleed' | 'full-bleed'} [presentation] Authoring intent about how the widget wants to render. From 2394c05cd5a37c0ec425ab1c2954830cc776493f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dami=C3=A1n=20Su=C3=A1rez?= Date: Thu, 30 Jul 2026 13:16:58 +0100 Subject: [PATCH 02/19] add icon to widget type registry and REST validate the collection/icon-name shape at registration --- ...lass-wp-rest-widget-modules-controller.php | 11 +++++++++ .../class-wp-widget-type.php | 11 +++++++++ .../dashboard-widgets/widget-types.php | 23 +++++++++++++++++++ ...wp-rest-widget-modules-controller-test.php | 4 ++++ phpunit/experimental/widget-types-test.php | 21 +++++++++++++++++ 5 files changed, 70 insertions(+) diff --git a/lib/experimental/dashboard-widgets/class-wp-rest-widget-modules-controller.php b/lib/experimental/dashboard-widgets/class-wp-rest-widget-modules-controller.php index 46deb1ac6d8ccd..0dae1d4b387426 100644 --- a/lib/experimental/dashboard-widgets/class-wp-rest-widget-modules-controller.php +++ b/lib/experimental/dashboard-widgets/class-wp-rest-widget-modules-controller.php @@ -211,6 +211,10 @@ public function prepare_item_for_response( $item, $request ) { $data['help'] = $widget_type->help; } + if ( rest_is_field_included( 'icon', $fields ) ) { + $data['icon'] = $widget_type->icon; + } + if ( rest_is_field_included( 'actions', $fields ) ) { $data['actions'] = $widget_type->actions; } @@ -315,6 +319,13 @@ public function get_item_schema() { 'readonly' => true, ), + 'icon' => array( + 'description' => __( 'Registered icon name identifying the widget type visually.', 'gutenberg' ), + 'type' => array( 'string', 'null' ), + 'context' => array( 'view', 'edit', 'embed' ), + 'readonly' => true, + ), + 'actions' => array( 'description' => __( 'Declarative actions the widget type exposes. Labels are translatable.', 'gutenberg' ), 'type' => array( 'array', 'null' ), diff --git a/lib/experimental/dashboard-widgets/class-wp-widget-type.php b/lib/experimental/dashboard-widgets/class-wp-widget-type.php index d46ad31a740e3f..a5972c44fd7c98 100644 --- a/lib/experimental/dashboard-widgets/class-wp-widget-type.php +++ b/lib/experimental/dashboard-widgets/class-wp-widget-type.php @@ -107,6 +107,17 @@ class WP_Widget_Type { */ public $help = null; + /** + * Registered icon name identifying the widget type visually, in the + * form `collection/icon-name`. Clients resolve the name through the + * Icons API; an unregistered name degrades to no icon. + * + * Null when the widget did not declare the field. + * + * @var string|null + */ + public $icon = null; + /** * Declarative actions the widget exposes. Each entry carries `id`, * `label`, `href`, and optional `download`/`openInNewTab`. Labels are diff --git a/lib/experimental/dashboard-widgets/widget-types.php b/lib/experimental/dashboard-widgets/widget-types.php index 2df54230af70cb..8ef944277458c3 100644 --- a/lib/experimental/dashboard-widgets/widget-types.php +++ b/lib/experimental/dashboard-widgets/widget-types.php @@ -240,6 +240,28 @@ function gutenberg_sanitize_widget_actions( $actions, $dir_name = '' ) { return $sanitized ? $sanitized : null; } +/** + * Constrains a widget icon reference to a registered icon name in the + * form `collection/icon-name`, the shape the Icons API serves. Anything + * else is dropped silently, so authoring forms this gate does not accept + * yet (inline SVG markup, widget-local `.svg` files) degrade to no icon + * rather than warn on every request. + * + * @param string|null $icon Icon reference from the build manifest. + * @return string|null The icon name, or null when the shape does not match. + */ +function gutenberg_sanitize_widget_icon( $icon ) { + if ( ! is_string( $icon ) || '' === $icon ) { + return null; + } + + if ( ! preg_match( '#^[a-z0-9](?:[a-z0-9_-]*[a-z0-9])?/[a-z0-9](?:[a-z0-9_-]*[a-z0-9])?$#', $icon ) ) { + return null; + } + + return $icon; +} + /** * Hydrates the widget type registry from the build manifest. * @@ -273,6 +295,7 @@ function gutenberg_register_widget_types() { 'title' => $widget['title'] ?? null, 'description' => $widget['description'] ?? null, 'help' => gutenberg_sanitize_widget_help( $widget['help'] ?? null ), + 'icon' => gutenberg_sanitize_widget_icon( $widget['icon'] ?? null ), 'actions' => gutenberg_sanitize_widget_actions( $widget['actions'] ?? null, $widget['dir_name'] ?? '' diff --git a/phpunit/experimental/class-wp-rest-widget-modules-controller-test.php b/phpunit/experimental/class-wp-rest-widget-modules-controller-test.php index b431a25ad56304..b30bc03a3bf7b7 100644 --- a/phpunit/experimental/class-wp-rest-widget-modules-controller-test.php +++ b/phpunit/experimental/class-wp-rest-widget-modules-controller-test.php @@ -71,6 +71,7 @@ private function register_sample_widgets() { ), ), ), + 'icon' => 'core/calendar', 'actions' => array( array( 'id' => 'open-settings', @@ -159,6 +160,7 @@ public function test_get_item_returns_widget_by_name() { ), $data['help'] ); + $this->assertSame( 'core/calendar', $data['icon'] ); $this->assertSame( array( array( @@ -219,6 +221,7 @@ public function test_schema_exposes_expected_properties() { $this->assertArrayHasKey( 'title', $properties ); $this->assertArrayHasKey( 'description', $properties ); $this->assertArrayHasKey( 'help', $properties ); + $this->assertArrayHasKey( 'icon', $properties ); $this->assertArrayHasKey( 'actions', $properties ); $this->assertArrayHasKey( 'keywords', $properties ); $this->assertSame( 'string', $properties['name']['type'] ); @@ -228,6 +231,7 @@ public function test_schema_exposes_expected_properties() { $this->assertSame( array( 'string', 'null' ), $properties['title']['type'] ); $this->assertSame( array( 'string', 'null' ), $properties['description']['type'] ); $this->assertSame( array( 'object', 'null' ), $properties['help']['type'] ); + $this->assertSame( array( 'string', 'null' ), $properties['icon']['type'] ); $this->assertSame( array( 'array', 'null' ), $properties['actions']['type'] ); $this->assertSame( array( 'array', 'null' ), $properties['keywords']['type'] ); } diff --git a/phpunit/experimental/widget-types-test.php b/phpunit/experimental/widget-types-test.php index 8ebd0a4f60bbca..e3f1a250527f01 100644 --- a/phpunit/experimental/widget-types-test.php +++ b/phpunit/experimental/widget-types-test.php @@ -7,6 +7,7 @@ * @covers ::gutenberg_translate_widget_metadata * @covers ::gutenberg_get_widget_metadata_i18n_schema * @covers ::gutenberg_sanitize_widget_help + * @covers ::gutenberg_sanitize_widget_icon * @covers ::gutenberg_sanitize_widget_actions * @covers ::gutenberg_resolve_widget_action_href */ @@ -112,6 +113,26 @@ public function test_sanitize_widget_help_requires_content() { $this->assertNull( gutenberg_sanitize_widget_help( array( 'links' => array() ) ) ); } + /** + * Only names shaped `collection/icon-name` pass; the markup and file + * authoring forms are not accepted by this gate yet and normalize to + * null, as does anything malformed. + */ + public function test_sanitize_widget_icon_requires_a_registered_name_shape() { + $this->assertSame( 'core/calendar', gutenberg_sanitize_widget_icon( 'core/calendar' ) ); + $this->assertSame( 'my-plugin/arrow-left', gutenberg_sanitize_widget_icon( 'my-plugin/arrow-left' ) ); + + $this->assertNull( gutenberg_sanitize_widget_icon( null ) ); + $this->assertNull( gutenberg_sanitize_widget_icon( '' ) ); + $this->assertNull( gutenberg_sanitize_widget_icon( 'calendar' ) ); + $this->assertNull( gutenberg_sanitize_widget_icon( 'core/a/b' ) ); + $this->assertNull( gutenberg_sanitize_widget_icon( 'Core/Calendar' ) ); + $this->assertNull( gutenberg_sanitize_widget_icon( 'core/-calendar' ) ); + $this->assertNull( gutenberg_sanitize_widget_icon( 'core/calendar-' ) ); + $this->assertNull( gutenberg_sanitize_widget_icon( 'icon.svg' ) ); + $this->assertNull( gutenberg_sanitize_widget_icon( '' ) ); + } + /** * Drops unsafe or malformed actions; sanitizes download filenames. */ From a0d796bf8e2b986537dc6c1ab5b822a6f6c1fd66 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dami=C3=A1n=20Su=C3=A1rez?= Date: Thu, 30 Jul 2026 13:18:58 +0100 Subject: [PATCH 03/19] add icon resolver registry to widget primitives resolve record icon references in useWidgetTypes --- packages/widget-primitives/CHANGELOG.md | 5 ++ .../src/hooks/test/use-widget-types.test.tsx | 76 +++++++++++++++++- .../src/hooks/use-widget-types.ts | 24 +++++- .../src/icon-resolver/icon-resolver.ts | 80 +++++++++++++++++++ .../src/icon-resolver/index.ts | 7 ++ .../src/icon-resolver/test/icon-resolver.ts | 61 ++++++++++++++ packages/widget-primitives/src/index.ts | 5 ++ packages/widget-primitives/src/types.ts | 28 ++++++- 8 files changed, 281 insertions(+), 5 deletions(-) create mode 100644 packages/widget-primitives/src/icon-resolver/icon-resolver.ts create mode 100644 packages/widget-primitives/src/icon-resolver/index.ts create mode 100644 packages/widget-primitives/src/icon-resolver/test/icon-resolver.ts diff --git a/packages/widget-primitives/CHANGELOG.md b/packages/widget-primitives/CHANGELOG.md index 4e4405a15f8591..db8927c9040eb0 100644 --- a/packages/widget-primitives/CHANGELOG.md +++ b/packages/widget-primitives/CHANGELOG.md @@ -6,6 +6,11 @@ - `WidgetTypeMetadata`: add optional `actions`, a declarative list of user-triggerable links a widget exposes ([#80363](https://github.com/WordPress/gutenberg/pull/80363)). +- Widgets can reference their icon declaratively: `WidgetModuleRecord` + carries a registered icon name and `useWidgetTypes` resolves it through + the application-registered resolver (`registerIconResolver`), so + `WidgetType.icon` always reaches hosts renderable + ([#80938](https://github.com/WordPress/gutenberg/issues/80938)). ### Enhancements diff --git a/packages/widget-primitives/src/hooks/test/use-widget-types.test.tsx b/packages/widget-primitives/src/hooks/test/use-widget-types.test.tsx index f662f4703d4d4e..cfb427c149095a 100644 --- a/packages/widget-primitives/src/hooks/test/use-widget-types.test.tsx +++ b/packages/widget-primitives/src/hooks/test/use-widget-types.test.tsx @@ -3,15 +3,28 @@ */ import { renderHook, waitFor } from '@testing-library/react'; +/** + * WordPress dependencies + */ +import { createElement } from '@wordpress/element'; + /** * Internal dependencies */ import { registerFieldType, unregisterFieldType } from '../../field-types'; +import { + registerIconResolver, + unregisterIconResolver, +} from '../../icon-resolver'; import { useWidgetTypes } from '../use-widget-types'; -import type { WidgetModuleRecord } from '../../types'; +import type { WidgetIcon, WidgetModuleRecord } from '../../types'; const LocationControl = () => null; +const mockModuleIcon = createElement( 'svg', { + viewBox: '0 0 24 24', +} ) as WidgetIcon; + jest.mock( 'test-widget/widget-module', () => ( { @@ -19,6 +32,7 @@ jest.mock( default: { apiVersion: 1, title: 'Store', + icon: mockModuleIcon, attributes: [ { id: 'location', @@ -32,6 +46,18 @@ jest.mock( { virtual: true } ); +jest.mock( + 'test-widget/string-icon-module', + () => ( { + __esModule: true, + default: { + title: 'String icon', + icon: 'wordpress', + }, + } ), + { virtual: true } +); + const records: WidgetModuleRecord[] = [ { name: 'test/store', @@ -40,9 +66,22 @@ const records: WidgetModuleRecord[] = [ }, ]; +const iconReferenceRecords: WidgetModuleRecord[] = [ + { ...records[ 0 ], icon: 'core/calendar' }, +]; + +const stringIconRecords: WidgetModuleRecord[] = [ + { + name: 'test/string-icon', + widget_module: 'test-widget/string-icon-module', + render_module: 'test-widget/render-module', + }, +]; + describe( 'useWidgetTypes', () => { afterEach( () => { unregisterFieldType( 'test/location' ); + unregisterIconResolver(); } ); it( 'resolves named field-type references while building widget types', async () => { @@ -70,4 +109,39 @@ describe( 'useWidgetTypes', () => { // …while plain DataViews fields pass through unchanged. expect( label ).toMatchObject( { id: 'label', type: 'text' } ); } ); + + it( 'prefers the resolved record icon over the module element', async () => { + const resolvedIcon = createElement( 'svg', { + viewBox: '0 0 32 32', + } ) as WidgetIcon; + registerIconResolver( async () => resolvedIcon ); + + const { result } = renderHook( () => + useWidgetTypes( iconReferenceRecords ) + ); + + await waitFor( () => expect( result.current[ 1 ] ).toBe( false ) ); + + expect( result.current[ 0 ][ 0 ].icon ).toBe( resolvedIcon ); + } ); + + it( 'keeps the module element when the reference does not resolve', async () => { + const { result } = renderHook( () => + useWidgetTypes( iconReferenceRecords ) + ); + + await waitFor( () => expect( result.current[ 1 ] ).toBe( false ) ); + + expect( result.current[ 0 ][ 0 ].icon ).toBe( mockModuleIcon ); + } ); + + it( 'drops a module icon that is not an element', async () => { + const { result } = renderHook( () => + useWidgetTypes( stringIconRecords ) + ); + + await waitFor( () => expect( result.current[ 1 ] ).toBe( false ) ); + + expect( result.current[ 0 ][ 0 ].icon ).toBeUndefined(); + } ); } ); diff --git a/packages/widget-primitives/src/hooks/use-widget-types.ts b/packages/widget-primitives/src/hooks/use-widget-types.ts index dcdd0a7f03ed73..8daca5417818d2 100644 --- a/packages/widget-primitives/src/hooks/use-widget-types.ts +++ b/packages/widget-primitives/src/hooks/use-widget-types.ts @@ -1,12 +1,13 @@ /** * WordPress dependencies */ -import { useEffect, useState } from '@wordpress/element'; +import { isValidElement, useEffect, useState } from '@wordpress/element'; /** * Internal dependencies */ import { resolveFields } from '../field-types'; +import { resolveIcon } from '../icon-resolver'; import type { WidgetModuleRecord, WidgetName, WidgetType } from '../types'; /* `true` while records or their metadata imports are still resolving; hosts @@ -19,7 +20,9 @@ type UseWidgetTypesResult = readonly [ WidgetType[], boolean ]; * For each record it dynamically imports `widget_module` and merges the * module's default export with the runtime fields (`name`, `renderModule`). * Attribute schemas pass through `resolveFields`, so attributes referencing - * registered field types reach hosts as plain DataViews fields. + * registered field types reach hosts as plain DataViews fields. Icon + * references resolve through the registered icon resolver (see + * `registerIconResolver`), so hosts only receive renderable icons. * Pass `null`/`undefined` while records are still loading. * * @param records Host-supplied records, or `null`/`undefined` while loading. @@ -63,6 +66,22 @@ export function useWidgetTypes( const metadata = module.default as Partial< WidgetType >; + /* + * Icon precedence mirrors the other identity fields: + * the record's resolved reference wins, and the + * module's element stands when the record has none or + * the reference does not resolve. Non-element module + * values are dropped so hosts only receive renderable + * icons. + */ + const resolvedIcon = record.icon + ? await resolveIcon( record.icon ) + : null; + const moduleIcon = isValidElement( metadata.icon ) + ? metadata.icon + : undefined; + const icon = resolvedIcon ?? moduleIcon; + return { ...metadata, ...( metadata.attributes @@ -74,6 +93,7 @@ export function useWidgetTypes( : {} ), name: record.name as WidgetName, renderModule: record.render_module ?? '', + icon, /* * `title` is required: * - Server-side title wins diff --git a/packages/widget-primitives/src/icon-resolver/icon-resolver.ts b/packages/widget-primitives/src/icon-resolver/icon-resolver.ts new file mode 100644 index 00000000000000..597bb9c9502e60 --- /dev/null +++ b/packages/widget-primitives/src/icon-resolver/icon-resolver.ts @@ -0,0 +1,80 @@ +/** + * Icon resolver registry. + * + * A widget's `widget.json` may reference its icon by registered icon + * name (`collection/icon-name`) instead of shipping a rendered element. + * The application registers a single resolver that turns such a + * reference into a renderable `WidgetIcon`; `useWidgetTypes` invokes it + * while assembling each `WidgetType`, so hosts only ever receive + * renderable icons. + */ + +/** + * Internal dependencies + */ +import type { WidgetIcon, WidgetIconReference } from '../types'; + +/** + * Resolves an icon reference into a renderable icon, or `null` when the + * reference does not resolve. + */ +export type WidgetIconResolver = ( + reference: WidgetIconReference +) => Promise< WidgetIcon | null >; + +let iconResolver: WidgetIconResolver | undefined; + +/** + * Registers the icon resolver. + * + * First registration wins: a later call is ignored. + * + * @param resolver Resolver turning an icon reference into a renderable + * icon. + * @return The registered resolver, or `undefined` when ignored. + */ +export function registerIconResolver( + resolver: WidgetIconResolver +): WidgetIconResolver | undefined { + if ( iconResolver ) { + return undefined; + } + + iconResolver = resolver; + return resolver; +} + +/** + * Unregisters the icon resolver. + * + * @return The removed resolver, or `undefined` when none was registered. + */ +export function unregisterIconResolver(): WidgetIconResolver | undefined { + const resolver = iconResolver; + iconResolver = undefined; + + return resolver; +} + +/** + * Resolves an icon reference through the registered resolver. + * + * Returns `null` when no resolver is registered or the resolver fails: + * an unresolvable reference degrades to no icon rather than breaking. + * + * @param reference Icon reference to resolve. + * @return The renderable icon, or `null`. + */ +export async function resolveIcon( + reference: WidgetIconReference +): Promise< WidgetIcon | null > { + if ( ! iconResolver ) { + return null; + } + + try { + return await iconResolver( reference ); + } catch { + return null; + } +} diff --git a/packages/widget-primitives/src/icon-resolver/index.ts b/packages/widget-primitives/src/icon-resolver/index.ts new file mode 100644 index 00000000000000..68cc8416bfc0a9 --- /dev/null +++ b/packages/widget-primitives/src/icon-resolver/index.ts @@ -0,0 +1,7 @@ +export { + registerIconResolver, + resolveIcon, + unregisterIconResolver, +} from './icon-resolver'; + +export type { WidgetIconResolver } from './icon-resolver'; diff --git a/packages/widget-primitives/src/icon-resolver/test/icon-resolver.ts b/packages/widget-primitives/src/icon-resolver/test/icon-resolver.ts new file mode 100644 index 00000000000000..484951aac3f9f6 --- /dev/null +++ b/packages/widget-primitives/src/icon-resolver/test/icon-resolver.ts @@ -0,0 +1,61 @@ +/** + * WordPress dependencies + */ +import { createElement } from '@wordpress/element'; + +/** + * Internal dependencies + */ +import { + registerIconResolver, + resolveIcon, + unregisterIconResolver, +} from '../icon-resolver'; +import type { WidgetIcon } from '../../types'; + +const icon = createElement( 'svg', { viewBox: '0 0 24 24' } ) as WidgetIcon; + +describe( 'icon resolver registry', () => { + afterEach( () => { + unregisterIconResolver(); + } ); + + it( 'resolves to null when no resolver is registered', async () => { + await expect( resolveIcon( 'core/calendar' ) ).resolves.toBeNull(); + } ); + + it( 'resolves references through the registered resolver', async () => { + const resolver = jest.fn( async () => icon ); + expect( registerIconResolver( resolver ) ).toBe( resolver ); + + await expect( resolveIcon( 'core/calendar' ) ).resolves.toBe( icon ); + expect( resolver ).toHaveBeenCalledWith( 'core/calendar' ); + } ); + + it( 'keeps the first registered resolver', async () => { + const first = jest.fn( async () => icon ); + const second = jest.fn( async () => null ); + + registerIconResolver( first ); + expect( registerIconResolver( second ) ).toBeUndefined(); + + await expect( resolveIcon( 'core/calendar' ) ).resolves.toBe( icon ); + expect( second ).not.toHaveBeenCalled(); + } ); + + it( 'degrades to null when the resolver rejects', async () => { + registerIconResolver( () => Promise.reject( new Error( 'nope' ) ) ); + + await expect( resolveIcon( 'core/calendar' ) ).resolves.toBeNull(); + } ); + + it( 'unregisters the resolver', async () => { + const resolver = jest.fn( async () => icon ); + registerIconResolver( resolver ); + + expect( unregisterIconResolver() ).toBe( resolver ); + expect( unregisterIconResolver() ).toBeUndefined(); + + await expect( resolveIcon( 'core/calendar' ) ).resolves.toBeNull(); + } ); +} ); diff --git a/packages/widget-primitives/src/index.ts b/packages/widget-primitives/src/index.ts index e7e42bbcf56414..287d8270ae57cc 100644 --- a/packages/widget-primitives/src/index.ts +++ b/packages/widget-primitives/src/index.ts @@ -13,6 +13,11 @@ export { useWidgetTypes } from './hooks'; */ export { registerFieldType } from './field-types'; +/** + * Icon resolution + */ +export { registerIconResolver } from './icon-resolver'; + /** * Types */ diff --git a/packages/widget-primitives/src/types.ts b/packages/widget-primitives/src/types.ts index 20046952fc83ab..16d7c12400f74d 100644 --- a/packages/widget-primitives/src/types.ts +++ b/packages/widget-primitives/src/types.ts @@ -31,6 +31,14 @@ export type WidgetName = `${ string }/${ string }`; */ export type WidgetIcon = ReactElement< ComponentProps< 'svg' > >; +/** + * Declarative reference to a widget icon: the name of an icon registered + * in the site's icon registry, shaped `collection/icon-name`. Resolved + * into a `WidgetIcon` at the `useWidgetTypes` boundary through the + * application-registered resolver (see `registerIconResolver`). + */ +export type WidgetIconReference = string; + /** * A link in a widget's help note. */ @@ -159,9 +167,10 @@ export interface WidgetTypeMetadata< Item = unknown > { /** * Visual identifier for the widget type; hosts decide where, and - * whether, to render it. + * whether, to render it. `widget.json` declares a reference; a + * metadata module may declare a rendered element instead. */ - icon?: WidgetIcon; + icon?: WidgetIcon | WidgetIconReference; /** * Grouping category. Core provides `dashboard`; plugins and themes may @@ -237,6 +246,13 @@ export interface WidgetTypeMetadata< Item = unknown > { */ export interface WidgetType< Item = unknown > extends WidgetTypeMetadata< Item > { + /** + * Renderable icon only: `useWidgetTypes` resolves references and + * drops anything that is not an element before a `WidgetType` + * reaches a host. + */ + icon?: WidgetIcon; + /** * Script-module identifier resolved to a React component at render * time, produced from the conventional `render.*` entry point. @@ -317,4 +333,12 @@ export interface WidgetModuleRecord extends WidgetModuleRecordOverrides { * Script-module id dynamically imported for the widget's live metadata. */ widget_module?: string | null; + + /** + * Registered icon name (`collection/icon-name`), resolved client-side + * through the application's icon resolver. Always a string reference, + * never an element; `null`/absent means the metadata module's icon + * stands. + */ + icon?: WidgetIconReference | null; } From a0aff5f4dd8507d44ce73fc5d376ed389ef8fc05 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dami=C3=A1n=20Su=C3=A1rez?= Date: Thu, 30 Jul 2026 13:20:21 +0100 Subject: [PATCH 04/19] register dashboard icon resolver resolve references against the icon entity as elements --- routes/dashboard/icons/index.ts | 46 +++++++++++++++++++++++++++++++++ routes/dashboard/package.json | 4 ++- routes/dashboard/stage.tsx | 2 ++ 3 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 routes/dashboard/icons/index.ts diff --git a/routes/dashboard/icons/index.ts b/routes/dashboard/icons/index.ts new file mode 100644 index 00000000000000..7aff1876ea96a1 --- /dev/null +++ b/routes/dashboard/icons/index.ts @@ -0,0 +1,46 @@ +/** + * 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 { isValidElement } from '@wordpress/element'; +import { registerIconResolver } from '@wordpress/widget-primitives'; +import type { WidgetIcon } from '@wordpress/widget-primitives'; + +/** + * Registers the dashboard's icon resolver: widget icon references + * resolve against the `icon` core-data entity, and the record's SVG + * content becomes the rendered element hosts receive. Idempotent: the + * registry keeps the first registration. + */ +export function registerDashboardIconResolver() { + registerIconResolver( async ( reference ) => { + const record = ( await resolveSelect( coreStore ).getEntityRecord( + 'root', + 'icon', + reference + ) ) as { content?: string } | undefined; + + if ( ! record?.content ) { + return null; + } + + /* + * Registry-served content may carry whitespace around the root + * ``, which makes `parse()` return an array of nodes; the + * icon is the element among them. + */ + const parsed = parse( safeHTML( record.content.trim() ) ); + const element = Array.isArray( parsed ) + ? parsed.find( isValidElement ) + : parsed; + + return isValidElement( element ) ? ( element as WidgetIcon ) : null; + } ); +} diff --git a/routes/dashboard/package.json b/routes/dashboard/package.json index a9e6e32a2c5fe1..6409c588a35c57 100644 --- a/routes/dashboard/package.json +++ b/routes/dashboard/package.json @@ -15,6 +15,7 @@ "@wordpress/core-data": "file:../../packages/core-data", "@wordpress/data": "file:../../packages/data", "@wordpress/dataviews": "file:../../packages/dataviews", + "@wordpress/dom": "file:../../packages/dom", "@wordpress/element": "file:../../packages/element", "@wordpress/hooks": "file:../../packages/hooks", "@wordpress/i18n": "file:../../packages/i18n", @@ -26,7 +27,8 @@ "@wordpress/warning": "file:../../packages/warning", "@wordpress/widget-dashboard": "file:../../packages/widget-dashboard", "@wordpress/widget-primitives": "file:../../packages/widget-primitives", - "fast-deep-equal": "^3.1.3" + "fast-deep-equal": "^3.1.3", + "html-react-parser": "^5.2.11" }, "devDependencies": { "@testing-library/dom": "^10.4.1", diff --git a/routes/dashboard/stage.tsx b/routes/dashboard/stage.tsx index eeb714b74cc63f..e6cb3459ef7e3c 100644 --- a/routes/dashboard/stage.tsx +++ b/routes/dashboard/stage.tsx @@ -21,9 +21,11 @@ import { * Internal dependencies */ import { registerDashboardFieldTypes } from './field-types'; +import { registerDashboardIconResolver } from './icons'; import { useDashboardGridSettings, useDashboardLayout } from './hooks'; registerDashboardFieldTypes(); +registerDashboardIconResolver(); function Dashboard() { const [ layout, setLayout, resetLayout ] = useDashboardLayout( From cb554ef7f530f2eaa5d400f807a77bc21ae60f8d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dami=C3=A1n=20Su=C3=A1rez?= Date: Thu, 30 Jul 2026 13:20:25 +0100 Subject: [PATCH 05/19] register widget-owned icons server-side temporary dashboard-widgets collection for core-private icons --- .../dashboard-widgets/widget-icons.php | 62 +++++++++++++++++++ lib/load.php | 1 + 2 files changed, 63 insertions(+) create mode 100644 lib/experimental/dashboard-widgets/widget-icons.php diff --git a/lib/experimental/dashboard-widgets/widget-icons.php b/lib/experimental/dashboard-widgets/widget-icons.php new file mode 100644 index 00000000000000..9365f2287714b9 --- /dev/null +++ b/lib/experimental/dashboard-widgets/widget-icons.php @@ -0,0 +1,62 @@ + __( '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' ); diff --git a/lib/load.php b/lib/load.php index 9ef9e87d54988d..b22e9381f086dc 100644 --- a/lib/load.php +++ b/lib/load.php @@ -241,6 +241,7 @@ function gutenberg_is_experiment_enabled( $name ) { if ( gutenberg_is_experiment_enabled( 'gutenberg-dashboard-widgets' ) ) { require __DIR__ . '/experimental/dashboard-widgets/load.php'; require __DIR__ . '/experimental/dashboard-widgets/widget-types.php'; + require __DIR__ . '/experimental/dashboard-widgets/widget-icons.php'; require __DIR__ . '/experimental/dashboard-widgets/dashboard-layout.php'; require __DIR__ . '/experimental/dashboard-widgets/default-layout-seed.php'; } From ad4d4fc78f929db278e9f7967928aacf459523ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dami=C3=A1n=20Su=C3=A1rez?= Date: Thu, 30 Jul 2026 13:30:49 +0100 Subject: [PATCH 06/19] move widget icons to widget.json replace module icon elements with registered references --- widgets/events/widget.json | 1 + widgets/events/widget.ts | 2 -- widgets/hello-dolly/widget.json | 1 + widgets/hello-dolly/widget.ts | 8 +------- widgets/hello-world/widget.json | 1 + widgets/hello-world/widget.ts | 2 -- widgets/quick-draft/widget.json | 1 + widgets/quick-draft/widget.ts | 6 ------ widgets/site-health/widget.json | 1 + widgets/site-health/widget.ts | 6 ------ widgets/site-preview/widget.json | 1 + widgets/site-preview/widget.ts | 6 ------ widgets/welcome/widget.json | 1 + 13 files changed, 8 insertions(+), 29 deletions(-) diff --git a/widgets/events/widget.json b/widgets/events/widget.json index 7c117c2aa79126..c7fbd176484850 100644 --- a/widgets/events/widget.json +++ b/widgets/events/widget.json @@ -2,6 +2,7 @@ "name": "core/events", "title": "WordPress events", "description": "Displays upcoming WordPress community events.", + "icon": "core/calendar", "category": "dashboard", "presentation": "content-bleed", "textdomain": "default" diff --git a/widgets/events/widget.ts b/widgets/events/widget.ts index 98e1d4e278394a..ebc7a2989d34bd 100644 --- a/widgets/events/widget.ts +++ b/widgets/events/widget.ts @@ -2,7 +2,6 @@ * WordPress dependencies */ import { __ } from '@wordpress/i18n'; -import { calendar } from '@wordpress/icons'; import type { WidgetAttributeField } from '@wordpress/widget-primitives'; /** @@ -12,7 +11,6 @@ import type { EventsWidgetAttributes } from './types'; export default { name: 'core/events', - icon: calendar, attributes: [ { id: 'location', diff --git a/widgets/hello-dolly/widget.json b/widgets/hello-dolly/widget.json index e8a11d60f81c8b..3cd432b6098ab5 100644 --- a/widgets/hello-dolly/widget.json +++ b/widgets/hello-dolly/widget.json @@ -2,6 +2,7 @@ "name": "core/hello-dolly", "title": "Hello Dolly", "description": "This is not just a widget, it symbolizes the hope and enthusiasm of an entire generation summed up in two words sung most famously by Louis Armstrong: Hello, Dolly. When activated you will randomly see a lyric from Hello, Dolly in the dashboard.", + "icon": "core/audio", "category": "dashboard", "presentation": "full-bleed", "actions": [ diff --git a/widgets/hello-dolly/widget.ts b/widgets/hello-dolly/widget.ts index 3d11ac3807f4c5..492b05c064c5b8 100644 --- a/widgets/hello-dolly/widget.ts +++ b/widgets/hello-dolly/widget.ts @@ -1,12 +1,6 @@ -/** - * WordPress dependencies - */ -import { audio } from '@wordpress/icons'; - -/** +/* * Widget type definition */ export default { name: 'core/hello-dolly', - icon: audio, }; diff --git a/widgets/hello-world/widget.json b/widgets/hello-world/widget.json index 81993cc158612a..43474a74a8871b 100644 --- a/widgets/hello-world/widget.json +++ b/widgets/hello-world/widget.json @@ -2,6 +2,7 @@ "name": "core/hello-world", "title": "Hello World", "description": "A minimal example widget.", + "icon": "dashboard-widgets/wordpress", "category": "demo", "presentation": "full-bleed", "textdomain": "default" diff --git a/widgets/hello-world/widget.ts b/widgets/hello-world/widget.ts index 93e207e4bde98e..953e4c0bd13f61 100644 --- a/widgets/hello-world/widget.ts +++ b/widgets/hello-world/widget.ts @@ -1,7 +1,6 @@ /** * WordPress dependencies */ -import { wordpress } from '@wordpress/icons'; import type { WidgetAttributeField } from '@wordpress/widget-primitives'; type HelloWorldWidgetAttributes = { @@ -13,7 +12,6 @@ type HelloWorldWidgetAttributes = { */ export default { name: 'core/hello-world', - icon: wordpress, attributes: [ { id: 'message', diff --git a/widgets/quick-draft/widget.json b/widgets/quick-draft/widget.json index 8e7f923df2bb30..4b112453f9a753 100644 --- a/widgets/quick-draft/widget.json +++ b/widgets/quick-draft/widget.json @@ -5,6 +5,7 @@ "help": { "content": "Drafts saved here are private until you publish them." }, + "icon": "dashboard-widgets/drafts", "category": "dashboard", "presentation": "content-bleed", "textdomain": "default" diff --git a/widgets/quick-draft/widget.ts b/widgets/quick-draft/widget.ts index 0eccea109c2323..e10ca465de256f 100644 --- a/widgets/quick-draft/widget.ts +++ b/widgets/quick-draft/widget.ts @@ -1,12 +1,6 @@ -/** - * WordPress dependencies - */ -import { drafts } from '@wordpress/icons'; - /* * Widget type definition */ export default { name: 'core/quick-draft', - icon: drafts, }; diff --git a/widgets/site-health/widget.json b/widgets/site-health/widget.json index 9146dba6b7e206..123582abddcba6 100644 --- a/widgets/site-health/widget.json +++ b/widgets/site-health/widget.json @@ -6,6 +6,7 @@ "content": "Results come from your site's latest health checks.", "links": [ { "label": "Visit Site Health", "href": "site-health.php" } ] }, + "icon": "core/shield", "category": "site", "presentation": "framed", "textdomain": "default" diff --git a/widgets/site-health/widget.ts b/widgets/site-health/widget.ts index 1ccfddbcf3c92a..1179243f422cb2 100644 --- a/widgets/site-health/widget.ts +++ b/widgets/site-health/widget.ts @@ -1,9 +1,3 @@ -/** - * WordPress dependencies - */ -import { shield } from '@wordpress/icons'; - export default { name: 'core/site-health', - icon: shield, }; diff --git a/widgets/site-preview/widget.json b/widgets/site-preview/widget.json index 046a798fc1a94c..b7e08e6a80df72 100644 --- a/widgets/site-preview/widget.json +++ b/widgets/site-preview/widget.json @@ -2,6 +2,7 @@ "name": "core/site-preview", "title": "Site preview", "description": "Shows preview of the site homepage.", + "icon": "dashboard-widgets/site-logo", "category": "dashboard", "presentation": "full-bleed", "textdomain": "default" diff --git a/widgets/site-preview/widget.ts b/widgets/site-preview/widget.ts index a78c424f5c3d1e..bcbc7bd3595fb2 100644 --- a/widgets/site-preview/widget.ts +++ b/widgets/site-preview/widget.ts @@ -1,9 +1,3 @@ -/** - * WordPress dependencies - */ -import { siteLogo } from '@wordpress/icons'; - export default { name: 'core/site-preview', - icon: siteLogo, }; diff --git a/widgets/welcome/widget.json b/widgets/welcome/widget.json index a6500a4bc50caa..15310c2d5d74fc 100644 --- a/widgets/welcome/widget.json +++ b/widgets/welcome/widget.json @@ -2,6 +2,7 @@ "name": "core/welcome", "title": "Welcome", "description": "Displays a welcome panel to introduce users to WordPress.", + "icon": "dashboard-widgets/wordpress", "category": "dashboard", "presentation": "full-bleed", "textdomain": "default" From 2f82ef1b055f004e388348abead7ca9d1cf69045 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dami=C3=A1n=20Su=C3=A1rez?= Date: Thu, 30 Jul 2026 13:30:55 +0100 Subject: [PATCH 07/19] remove dead metadata from welcome widget module --- widgets/welcome/widget.ts | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/widgets/welcome/widget.ts b/widgets/welcome/widget.ts index ffa403078b06fd..246160c2ddf573 100644 --- a/widgets/welcome/widget.ts +++ b/widgets/welcome/widget.ts @@ -1,8 +1,3 @@ -const widget = { - apiVersion: 1, +export default { name: 'core/welcome', - icon: 'wordpress', - category: 'dashboard', }; - -export default widget; From 54c93e2f9ccf9ac3913d6f0f3d611c5141110d4d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dami=C3=A1n=20Su=C3=A1rez?= Date: Thu, 30 Jul 2026 13:30:59 +0100 Subject: [PATCH 08/19] document icon resolution in widget-primitives --- .../widget-primitives/src/stories/icons.md | 38 +++++++++++++++++++ .../widget-primitives/src/stories/icons.mdx | 6 +++ 2 files changed, 44 insertions(+) create mode 100644 packages/widget-primitives/src/stories/icons.md create mode 100644 packages/widget-primitives/src/stories/icons.mdx diff --git a/packages/widget-primitives/src/stories/icons.md b/packages/widget-primitives/src/stories/icons.md new file mode 100644 index 00000000000000..7cb05aa800703f --- /dev/null +++ b/packages/widget-primitives/src/stories/icons.md @@ -0,0 +1,38 @@ +# Icons + +
+This package is still experimental. “Experimental” means this is an early implementation subject to drastic and breaking changes. +
+ +A widget's icon is a React element in its metadata module, so any surface that wants to draw it has to import and execute the module first. The icon reference makes the declaration travel as data instead: `widget.json` names an icon registered in the site's icon registry, and the name flows through the build manifest, the server registry, and the REST record like every other identity field. + +## Referencing + +One nullable string field. A registered icon name is `collection/icon-name`: + +```json +{ "icon": "core/calendar" } +``` + +The metadata module may still declare a rendered element (`icon: calendar` from `@wordpress/icons`); a resolved reference from the record wins over it, and the element stands when the record carries none. + +## Registering the resolver + +The reference is data behind a REST entity, so what the application registers is a resolver rather than a value. It registers one resolver, before anything renders, closing over whatever data layer it owns: + +```ts +registerIconResolver( async ( reference ) => { + const icon = await fetchIconRecord( reference ); + return icon ? toElement( icon.content ) : null; +} ); +``` + +The resolver returns a renderable element or `null`. First registration wins. + +## Resolution + +`useWidgetTypes` resolves references while it assembles each `WidgetType`, at the same boundary that resolves field-type names. Hosts receive a renderable `icon` and never see a reference; anything that is not an element is dropped at the boundary. + +## When the reference does not resolve + +An unregistered name, a missing resolver, or a resolver failure degrades to no icon; the metadata module's element stands when there is one. A widget referencing an icon its application cannot resolve degrades; it does not break. diff --git a/packages/widget-primitives/src/stories/icons.mdx b/packages/widget-primitives/src/stories/icons.mdx new file mode 100644 index 00000000000000..3d6e5efa3296d9 --- /dev/null +++ b/packages/widget-primitives/src/stories/icons.mdx @@ -0,0 +1,6 @@ +import { Meta, Markdown } from '@storybook/addon-docs/blocks'; +import IconsDoc from './icons.md?raw'; + + + +{ IconsDoc } From 0685da1232cd62a92d22c5eed9a3943fd4a96e31 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dami=C3=A1n=20Su=C3=A1rez?= Date: Thu, 30 Jul 2026 14:20:24 +0100 Subject: [PATCH 09/19] shorten widget icon comments --- .../class-wp-widget-type.php | 5 ++--- .../dashboard-widgets/widget-icons.php | 11 +++------- .../dashboard-widgets/widget-types.php | 8 +++---- .../src/hooks/use-widget-types.ts | 13 +++++------ .../src/icon-resolver/icon-resolver.ts | 22 ++++++------------- packages/widget-primitives/src/types.ts | 19 ++++++---------- phpunit/experimental/widget-types-test.php | 5 ++--- routes/dashboard/icons/index.ts | 11 ++++------ 8 files changed, 33 insertions(+), 61 deletions(-) diff --git a/lib/experimental/dashboard-widgets/class-wp-widget-type.php b/lib/experimental/dashboard-widgets/class-wp-widget-type.php index a5972c44fd7c98..76dcf7327f9d08 100644 --- a/lib/experimental/dashboard-widgets/class-wp-widget-type.php +++ b/lib/experimental/dashboard-widgets/class-wp-widget-type.php @@ -108,9 +108,8 @@ class WP_Widget_Type { public $help = null; /** - * Registered icon name identifying the widget type visually, in the - * form `collection/icon-name`. Clients resolve the name through the - * Icons API; an unregistered name degrades to no icon. + * Registered icon name (`collection/icon-name`), resolved by + * clients through the Icons API. * * Null when the widget did not declare the field. * diff --git a/lib/experimental/dashboard-widgets/widget-icons.php b/lib/experimental/dashboard-widgets/widget-icons.php index 9365f2287714b9..1a85dcb4f2c069 100644 --- a/lib/experimental/dashboard-widgets/widget-icons.php +++ b/lib/experimental/dashboard-widgets/widget-icons.php @@ -2,11 +2,8 @@ /** * Widget-owned icons. * - * Icons the in-tree widgets reference declaratively but that are not - * part of the public `core` collection are registered here, under a - * collection the widgets own. `widget.json` references them by name - * (`dashboard-widgets/drafts`) and hosts resolve them through the - * Icons API like any other registered icon. + * Icons the widgets reference declaratively but that are not public in + * the `core` collection, registered under a collection the widgets own. * * @package gutenberg */ @@ -14,9 +11,7 @@ /** * Registers the dashboard widgets icon collection and its icons. * - * Icon content is sourced from the `@wordpress/icons` library files, so - * a widget's declarative reference renders the same asset its metadata - * module would import. + * Content is sourced from the `@wordpress/icons` library files. */ function gutenberg_register_dashboard_widget_icons() { if ( ! function_exists( 'wp_register_icon_collection' ) ) { diff --git a/lib/experimental/dashboard-widgets/widget-types.php b/lib/experimental/dashboard-widgets/widget-types.php index 8ef944277458c3..c8d7b5817666e6 100644 --- a/lib/experimental/dashboard-widgets/widget-types.php +++ b/lib/experimental/dashboard-widgets/widget-types.php @@ -241,11 +241,9 @@ function gutenberg_sanitize_widget_actions( $actions, $dir_name = '' ) { } /** - * Constrains a widget icon reference to a registered icon name in the - * form `collection/icon-name`, the shape the Icons API serves. Anything - * else is dropped silently, so authoring forms this gate does not accept - * yet (inline SVG markup, widget-local `.svg` files) degrade to no icon - * rather than warn on every request. + * Constrains a widget icon reference to a registered icon name + * (`collection/icon-name`). Anything else drops silently, so authoring + * forms not accepted yet degrade to no icon rather than warn. * * @param string|null $icon Icon reference from the build manifest. * @return string|null The icon name, or null when the shape does not match. diff --git a/packages/widget-primitives/src/hooks/use-widget-types.ts b/packages/widget-primitives/src/hooks/use-widget-types.ts index 8daca5417818d2..4b71e013865610 100644 --- a/packages/widget-primitives/src/hooks/use-widget-types.ts +++ b/packages/widget-primitives/src/hooks/use-widget-types.ts @@ -21,8 +21,8 @@ type UseWidgetTypesResult = readonly [ WidgetType[], boolean ]; * module's default export with the runtime fields (`name`, `renderModule`). * Attribute schemas pass through `resolveFields`, so attributes referencing * registered field types reach hosts as plain DataViews fields. Icon - * references resolve through the registered icon resolver (see - * `registerIconResolver`), so hosts only receive renderable icons. + * references resolve through the registered icon resolver, so hosts only + * receive renderable icons. * Pass `null`/`undefined` while records are still loading. * * @param records Host-supplied records, or `null`/`undefined` while loading. @@ -67,12 +67,9 @@ export function useWidgetTypes( const metadata = module.default as Partial< WidgetType >; /* - * Icon precedence mirrors the other identity fields: - * the record's resolved reference wins, and the - * module's element stands when the record has none or - * the reference does not resolve. Non-element module - * values are dropped so hosts only receive renderable - * icons. + * The record's resolved reference wins; the module's + * element stands when there is none or it does not + * resolve. Non-elements are dropped. */ const resolvedIcon = record.icon ? await resolveIcon( record.icon ) diff --git a/packages/widget-primitives/src/icon-resolver/icon-resolver.ts b/packages/widget-primitives/src/icon-resolver/icon-resolver.ts index 597bb9c9502e60..d07c770431719d 100644 --- a/packages/widget-primitives/src/icon-resolver/icon-resolver.ts +++ b/packages/widget-primitives/src/icon-resolver/icon-resolver.ts @@ -1,12 +1,9 @@ /** * Icon resolver registry. * - * A widget's `widget.json` may reference its icon by registered icon - * name (`collection/icon-name`) instead of shipping a rendered element. - * The application registers a single resolver that turns such a - * reference into a renderable `WidgetIcon`; `useWidgetTypes` invokes it - * while assembling each `WidgetType`, so hosts only ever receive - * renderable icons. + * The application registers a single resolver that turns a registered + * icon name (`collection/icon-name`) into a renderable `WidgetIcon`; + * `useWidgetTypes` invokes it while assembling each `WidgetType`. */ /** @@ -25,12 +22,9 @@ export type WidgetIconResolver = ( let iconResolver: WidgetIconResolver | undefined; /** - * Registers the icon resolver. + * Registers the icon resolver. First registration wins. * - * First registration wins: a later call is ignored. - * - * @param resolver Resolver turning an icon reference into a renderable - * icon. + * @param resolver Resolver turning a reference into a renderable icon. * @return The registered resolver, or `undefined` when ignored. */ export function registerIconResolver( @@ -57,10 +51,8 @@ export function unregisterIconResolver(): WidgetIconResolver | undefined { } /** - * Resolves an icon reference through the registered resolver. - * - * Returns `null` when no resolver is registered or the resolver fails: - * an unresolvable reference degrades to no icon rather than breaking. + * Resolves an icon reference through the registered resolver. An + * unresolvable reference degrades to `null` rather than breaking. * * @param reference Icon reference to resolve. * @return The renderable icon, or `null`. diff --git a/packages/widget-primitives/src/types.ts b/packages/widget-primitives/src/types.ts index 16d7c12400f74d..7146d3360c1bf3 100644 --- a/packages/widget-primitives/src/types.ts +++ b/packages/widget-primitives/src/types.ts @@ -32,10 +32,8 @@ export type WidgetName = `${ string }/${ string }`; export type WidgetIcon = ReactElement< ComponentProps< 'svg' > >; /** - * Declarative reference to a widget icon: the name of an icon registered - * in the site's icon registry, shaped `collection/icon-name`. Resolved - * into a `WidgetIcon` at the `useWidgetTypes` boundary through the - * application-registered resolver (see `registerIconResolver`). + * Registered icon name (`collection/icon-name`), resolved into a + * `WidgetIcon` by the application's resolver (see `registerIconResolver`). */ export type WidgetIconReference = string; @@ -167,8 +165,8 @@ export interface WidgetTypeMetadata< Item = unknown > { /** * Visual identifier for the widget type; hosts decide where, and - * whether, to render it. `widget.json` declares a reference; a - * metadata module may declare a rendered element instead. + * whether, to render it. A reference in `widget.json`, or a rendered + * element in the module. */ icon?: WidgetIcon | WidgetIconReference; @@ -247,8 +245,7 @@ export interface WidgetTypeMetadata< Item = unknown > { export interface WidgetType< Item = unknown > extends WidgetTypeMetadata< Item > { /** - * Renderable icon only: `useWidgetTypes` resolves references and - * drops anything that is not an element before a `WidgetType` + * Always renderable: references are resolved before a `WidgetType` * reaches a host. */ icon?: WidgetIcon; @@ -335,10 +332,8 @@ export interface WidgetModuleRecord extends WidgetModuleRecordOverrides { widget_module?: string | null; /** - * Registered icon name (`collection/icon-name`), resolved client-side - * through the application's icon resolver. Always a string reference, - * never an element; `null`/absent means the metadata module's icon - * stands. + * Registered icon name (`collection/icon-name`); never an element. + * `null`/absent means the module's icon stands. */ icon?: WidgetIconReference | null; } diff --git a/phpunit/experimental/widget-types-test.php b/phpunit/experimental/widget-types-test.php index e3f1a250527f01..9b52668d078717 100644 --- a/phpunit/experimental/widget-types-test.php +++ b/phpunit/experimental/widget-types-test.php @@ -114,9 +114,8 @@ public function test_sanitize_widget_help_requires_content() { } /** - * Only names shaped `collection/icon-name` pass; the markup and file - * authoring forms are not accepted by this gate yet and normalize to - * null, as does anything malformed. + * Only names shaped `collection/icon-name` pass; markup, files, and + * anything malformed normalize to null. */ public function test_sanitize_widget_icon_requires_a_registered_name_shape() { $this->assertSame( 'core/calendar', gutenberg_sanitize_widget_icon( 'core/calendar' ) ); diff --git a/routes/dashboard/icons/index.ts b/routes/dashboard/icons/index.ts index 7aff1876ea96a1..a85f691f76e46c 100644 --- a/routes/dashboard/icons/index.ts +++ b/routes/dashboard/icons/index.ts @@ -14,10 +14,8 @@ import { registerIconResolver } from '@wordpress/widget-primitives'; import type { WidgetIcon } from '@wordpress/widget-primitives'; /** - * Registers the dashboard's icon resolver: widget icon references - * resolve against the `icon` core-data entity, and the record's SVG - * content becomes the rendered element hosts receive. Idempotent: the - * registry keeps the first registration. + * 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 ) => { @@ -32,9 +30,8 @@ export function registerDashboardIconResolver() { } /* - * Registry-served content may carry whitespace around the root - * ``, which makes `parse()` return an array of nodes; the - * icon is the element among them. + * Whitespace around the root `` makes `parse()` return an + * array; take the element. */ const parsed = parse( safeHTML( record.content.trim() ) ); const element = Array.isArray( parsed ) From 157154b2afd9f87a139c61961915aa960c02bb37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dami=C3=A1n=20Su=C3=A1rez?= Date: Thu, 30 Jul 2026 14:35:23 +0100 Subject: [PATCH 10/19] update docs for declarative icon references cover icon in the architecture doc field lists and README --- .../explanations/architecture/dashboard-widgets.md | 14 ++++++++------ packages/widget-primitives/README.md | 6 +++++- packages/widget-primitives/src/stories/icons.md | 6 +++--- 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/docs/explanations/architecture/dashboard-widgets.md b/docs/explanations/architecture/dashboard-widgets.md index a26fff697874e5..6b42b12166a9f6 100644 --- a/docs/explanations/architecture/dashboard-widgets.md +++ b/docs/explanations/architecture/dashboard-widgets.md @@ -22,8 +22,8 @@ A widget is a directory under `widgets/`, discovered by convention; there is no ``` widgets/hello-world/ -├── widget.json static metadata (name, title, description, help, actions, keywords, category, presentation, textdomain) -├── widget.ts metadata module: default-exports icon, attributes, example +├── widget.json static metadata (name, title, description, help, icon, actions, keywords, category, presentation, textdomain) +├── widget.ts metadata module: default-exports attributes, example ├── render.tsx render module: default-exports the React component ├── style.module.css optional, injected at runtime by the build └── report.csv optional static asset linked from an action `href` @@ -35,7 +35,9 @@ Unlike the other translatable strings, `help` is an object: `content` plus optio `actions`: declarative links (`id`, `label`, `href`, optional `download` / `openInNewTab`). Hosts place them; the dashboard uses a "More" menu. A relative `href` that exists under `widgets/{name}/` becomes a plugin URL at registration; missing relative non-admin files are dropped. `data:` and `javascript:` hrefs are rejected. Prefer absolute URLs for assets that must work in the plugin zip (which does not ship `widgets/`). -`widget.ts` is the live half of the metadata: values that only exist in JavaScript, such as the icon element or the `attributes` field schema (including optional `relevance` hints) that hosts feed into `DataForm`. +`icon`: a registered icon name (`collection/icon-name`), resolved client-side through the site's Icons API. Malformed names are dropped at registration. + +`widget.ts` is the live half of the metadata: values that only exist in JavaScript, such as the `attributes` field schema (including optional `relevance` hints) that hosts feed into `DataForm`. Its render component receives the widget's `attributes` and, optionally, `setAttributes`: @@ -54,13 +56,13 @@ export default function HelloWorld( { attributes } ) { ... } ## The server registry -`WP_Widget_Type_Registry` (`lib/experimental/dashboard-widgets/`) is a singleton, hydrated at `init` from the manifest. Each entry becomes a `WP_Widget_Type` with `name`, `render_module`, `widget_module`, `presentation`, `category`, and the translatable `title`, `description`, `help`, `actions`, and `keywords` (localized at registration time using the widget's `textdomain`). +`WP_Widget_Type_Registry` (`lib/experimental/dashboard-widgets/`) is a singleton, hydrated at `init` from the manifest. Each entry becomes a `WP_Widget_Type` with `name`, `render_module`, `widget_module`, `presentation`, `category`, `icon`, and the translatable `title`, `description`, `help`, `actions`, and `keywords` (localized at registration time using the widget's `textdomain`). The hydration is a deterministic copy, with no filters in between. The `widgets/` folder is the single source of widget authorship in this codebase. The registry is the server's authoritative list of widget types for the site. Two consumers read it: -- The REST controller (`WP_REST_Widget_Modules_Controller`) exposes it at `/wp/v2/widget-modules`, returning `{ name, render_module, widget_module, presentation, category, title, description, help, actions, keywords }` per record. +- The REST controller (`WP_REST_Widget_Modules_Controller`) exposes it at `/wp/v2/widget-modules`, returning `{ name, render_module, widget_module, presentation, category, title, description, help, icon, actions, keywords }` per record. - The dashboard page hooks its `dashboard-wp-admin_boot_dependencies` filter, a per-page instance of the generic `{page-slug}-wp-admin_boot_dependencies`, and adds every registered module to its import map as a `dynamic` dependency. A dynamic dependency is reachable by `import()` but never executed eagerly. Registration only makes the modules known to WordPress; loading them is a separate, per-host decision. Dynamic `import()` against the import map is how the dashboard loads widgets today. A host can load them another way: enqueue a module eagerly (`wp_enqueue_script_module()`), declare it as a `static` dependency of its own module, or, outside WordPress, skip the import map and resolve modules through its own `ResolveWidgetModule`. @@ -71,7 +73,7 @@ The registry exists as a class, rather than having REST read the manifest direct Everything after the REST record is the job of [`@wordpress/widget-primitives`](https://github.com/WordPress/gutenberg/tree/HEAD/packages/widget-primitives), the contract both widget authors and hosts share. Its full surface (the contract types, the discovery hook, and the render component) is covered in the _Widget Primitives / Introduction_ story. In the pipeline it does two things. -`useWidgetTypes( records )` takes the host-supplied records, imports each record's `widget_module` for the live metadata, and merges it with the record into `WidgetType[]`. The record's `presentation`, `category`, `title`, `description`, `help`, `actions`, and `keywords`, all sourced from `widget.json` (with `title`, `description`, `help`, `actions`, and `keywords` localized server-side), win over the module's value. The hook reaches for no store or endpoint; a host such as the dashboard reads its own `widgetModule` core-data entity (backed by `/wp/v2/widget-modules`) and passes the records in. +`useWidgetTypes( records )` takes the host-supplied records, imports each record's `widget_module` for the live metadata, and merges it with the record into `WidgetType[]`. The record's `presentation`, `category`, `title`, `description`, `help`, `actions`, and `keywords`, all sourced from `widget.json` (with `title`, `description`, `help`, `actions`, and `keywords` localized server-side), win over the module's value. The record's `icon` is a registered icon name: the hook resolves it through the application-registered resolver (`registerIconResolver`), and the resolved element wins over a module's element. The hook reaches for no store or endpoint; a host such as the dashboard reads its own `widgetModule` core-data entity (backed by `/wp/v2/widget-modules`) and passes the records in. A module's `attributes` may also reference field types by name (`type: 'location'`). The application registers those definitions up front through `registerFieldType()` (the dashboard route registers its own on boot), and `useWidgetTypes` resolves every named reference through that registry while building each `WidgetType`: the registered definition supplies the field's behavior on top of its DataViews `baseType`, and hosts receive plain DataViews fields. The widget declaration stays serializable; resolution happens once, at this boundary. diff --git a/packages/widget-primitives/README.md b/packages/widget-primitives/README.md index ac1b4fab8f2d1c..c683e280af505e 100644 --- a/packages/widget-primitives/README.md +++ b/packages/widget-primitives/README.md @@ -55,7 +55,7 @@ It takes host-supplied records (`WidgetModuleRecord[]`, or `null` while loading) ### Contract types -`WidgetType`, `WidgetName`, `WidgetIcon`, `WidgetRenderProps`, `ResolveWidgetModule`, and `WidgetModuleRecord`. `WidgetIcon` is a rendered SVG element that hosts the pass to its icon primitive as-is. +`WidgetType`, `WidgetName`, `WidgetIcon`, `WidgetRenderProps`, `ResolveWidgetModule`, and `WidgetModuleRecord`. `WidgetIcon` is a rendered SVG element that hosts pass to their icon primitive as-is; in `widget.json` a widget declares a registered icon name instead, resolved before it reaches hosts. ### `WidgetAttributeField< Item >` @@ -76,6 +76,10 @@ The widget names the intent and a link target; the host renders it as an anchor `useWidgetTypes` resolves those references into the plain per-field `Field` props DataViews understands, inheriting the rest from `baseType`. +### Icons + +`registerIconResolver( resolver )` registers how a registered icon name (`"icon": "core/calendar"` in `widget.json`) becomes a renderable element. The application registers it once; `useWidgetTypes` resolves references while assembling each `WidgetType`, so hosts only receive renderable icons. An unresolvable reference degrades to no icon. + ## Architecture For how the full pipeline fits together (authoring, build, server registry, and diff --git a/packages/widget-primitives/src/stories/icons.md b/packages/widget-primitives/src/stories/icons.md index 7cb05aa800703f..53d17383e9508d 100644 --- a/packages/widget-primitives/src/stories/icons.md +++ b/packages/widget-primitives/src/stories/icons.md @@ -4,7 +4,7 @@ This package is still experimental. “Experimental” means this is an early implementation subject to drastic and breaking changes. -A widget's icon is a React element in its metadata module, so any surface that wants to draw it has to import and execute the module first. The icon reference makes the declaration travel as data instead: `widget.json` names an icon registered in the site's icon registry, and the name flows through the build manifest, the server registry, and the REST record like every other identity field. +An icon authored as a React element can only travel inside its metadata module, so any surface that wants to draw it has to import and execute the module first. The icon reference makes the declaration travel as data instead: `widget.json` names an icon registered in the site's icon registry, and the name flows through the build manifest, the server registry, and the REST record like every other identity field. ## Referencing @@ -14,7 +14,7 @@ One nullable string field. A registered icon name is `collection/icon-name`: { "icon": "core/calendar" } ``` -The metadata module may still declare a rendered element (`icon: calendar` from `@wordpress/icons`); a resolved reference from the record wins over it, and the element stands when the record carries none. +A metadata module may still declare a rendered element; the resolved reference wins over it, and the element stands when the record carries none. The widgets in this repository all declare references. ## Registering the resolver @@ -31,7 +31,7 @@ The resolver returns a renderable element or `null`. First registration wins. ## Resolution -`useWidgetTypes` resolves references while it assembles each `WidgetType`, at the same boundary that resolves field-type names. Hosts receive a renderable `icon` and never see a reference; anything that is not an element is dropped at the boundary. +`useWidgetTypes` resolves references while it assembles each `WidgetType`, at the same boundary that resolves field-type names. Hosts receive a renderable `icon` and never see a reference; anything that is not an element is dropped at the boundary. The _WidgetRender / With Icon Reference_ story shows the seam in isolation. ## When the reference does not resolve From f7f559b0e171c0ad7b2504da5290f08dbeba5505 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dami=C3=A1n=20Su=C3=A1rez?= Date: Thu, 30 Jul 2026 14:35:25 +0100 Subject: [PATCH 11/19] add icon reference story demonstrate registerIconResolver in the WidgetRender stories --- packages/widget-primitives/CHANGELOG.md | 4 + .../widget-render/stories/index.story.tsx | 98 ++++++++++++++++++- 2 files changed, 101 insertions(+), 1 deletion(-) diff --git a/packages/widget-primitives/CHANGELOG.md b/packages/widget-primitives/CHANGELOG.md index db8927c9040eb0..05c0594aa9b244 100644 --- a/packages/widget-primitives/CHANGELOG.md +++ b/packages/widget-primitives/CHANGELOG.md @@ -19,6 +19,10 @@ ### Documentation +- Add an Icons doc page and a `WithIconReference` story, cover + `registerIconResolver` in the README, and update the architecture doc + for declarative icon references + ([#80938](https://github.com/WordPress/gutenberg/issues/80938)). - Prefer widget-local files over `data:` URLs for action downloads ([#80510](https://github.com/WordPress/gutenberg/pull/80510)). - Add an Actions doc page and a `WithActions` story, and cover `actions` in the widget anatomy doc ([#80363](https://github.com/WordPress/gutenberg/pull/80363)). diff --git a/packages/widget-primitives/src/components/widget-render/stories/index.story.tsx b/packages/widget-primitives/src/components/widget-render/stories/index.story.tsx index 49eecc7638667b..fb03e428294dc0 100644 --- a/packages/widget-primitives/src/components/widget-render/stories/index.story.tsx +++ b/packages/widget-primitives/src/components/widget-render/stories/index.story.tsx @@ -14,7 +14,13 @@ import '@wordpress/components/build-style/style.css'; import '@wordpress/dataviews/build-style/style.css'; import { DataForm, useFormValidity } from '@wordpress/dataviews'; import type { DataFormControlProps, Field, Form } from '@wordpress/dataviews'; -import { Suspense, useId, useMemo, useState } from '@wordpress/element'; +import { + Suspense, + useEffect, + useId, + useMemo, + useState, +} from '@wordpress/element'; import { globe, starFilled } from '@wordpress/icons'; // `IconButton` is not on the recommended list yet. /* eslint-disable @wordpress/use-recommended-components */ @@ -26,9 +32,11 @@ import { Card, Icon, IconButton, Link, Stack } from '@wordpress/ui'; */ import { WidgetRender } from '..'; import { registerFieldType, resolveFields } from '../../../field-types'; +import { registerIconResolver, resolveIcon } from '../../../icon-resolver'; import type { WidgetAction, WidgetAttributeField, + WidgetIcon, WidgetRenderProps, WidgetType, } from '../../../types'; @@ -842,3 +850,91 @@ The widget names the intent and a link target; navigation and download are the b }, }, }; + +/* + * The application's icon vocabulary: one resolver, registered once. + * Records reference icons by name; the resolver returns elements. + */ +const STORY_ICONS: Record< string, WidgetIcon > = { + 'demo/planet': globe, +}; + +registerIconResolver( async ( reference ) => STORY_ICONS[ reference ] ?? null ); + +const referencedIconWidgetType: WidgetType< DemoAttributes > = { + ...demoWidgetType, + icon: undefined, +}; + +function WidgetWithIconReference() { + const titleId = useId(); + const [ attributes ] = useState< DemoAttributes >( { + ...referencedIconWidgetType.example?.attributes, + } ); + const [ icon, setIcon ] = useState< WidgetIcon | null >( null ); + + // Hosts get the resolved icon through `useWidgetTypes`; the story + // resolves the reference itself. + useEffect( () => { + let cancelled = false; + void resolveIcon( 'demo/planet' ).then( ( resolved ) => { + if ( ! cancelled ) { + setIcon( resolved ); + } + } ); + return () => { + cancelled = true; + }; + }, [] ); + + return ( +
+ } aria-labelledby={ titleId }> + + + { icon && ( + + ) } + } + style={ { flexGrow: 1 } } + > + { referencedIconWidgetType.title } + + + + + + + widgetType={ referencedIconWidgetType } + attributes={ attributes } + resolveWidgetModule={ resolveDemoModule } + /> + + + +
+ ); +} + +export const WithIconReference: StoryObj = { + render: () => , + parameters: { + docs: { + description: { + story: ` +The widget declares its icon as a **registered icon name** instead of a rendered element: + +1. The application registers one **icon resolver** (\`registerIconResolver\`), closing over its icon source. On WordPress that source is the \`icon\` REST entity; here, a local catalog. +2. The widget declares \`"icon": "demo/planet"\` in \`widget.json\`. Pure data: no imports, no elements. +3. \`useWidgetTypes\` resolves the reference while assembling each \`WidgetType\` (the story calls the resolver itself), so the host receives a renderable element and never sees the name. + +An unresolvable reference degrades to no icon. See the **Icons** doc for the full pipeline. +`, + }, + }, + }, +}; From 19d7eef8f3219e970169a631f29244fda706e093 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dami=C3=A1n=20Su=C3=A1rez?= Date: Thu, 30 Jul 2026 14:52:41 +0100 Subject: [PATCH 12/19] link changelog entries to the PR --- packages/widget-primitives/CHANGELOG.md | 4 ++-- packages/wp-build/CHANGELOG.md | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/widget-primitives/CHANGELOG.md b/packages/widget-primitives/CHANGELOG.md index 05c0594aa9b244..5e4dccc31e35d6 100644 --- a/packages/widget-primitives/CHANGELOG.md +++ b/packages/widget-primitives/CHANGELOG.md @@ -10,7 +10,7 @@ carries a registered icon name and `useWidgetTypes` resolves it through the application-registered resolver (`registerIconResolver`), so `WidgetType.icon` always reaches hosts renderable - ([#80938](https://github.com/WordPress/gutenberg/issues/80938)). + ([#80969](https://github.com/WordPress/gutenberg/pull/80969)). ### Enhancements @@ -22,7 +22,7 @@ - Add an Icons doc page and a `WithIconReference` story, cover `registerIconResolver` in the README, and update the architecture doc for declarative icon references - ([#80938](https://github.com/WordPress/gutenberg/issues/80938)). + ([#80969](https://github.com/WordPress/gutenberg/pull/80969)). - Prefer widget-local files over `data:` URLs for action downloads ([#80510](https://github.com/WordPress/gutenberg/pull/80510)). - Add an Actions doc page and a `WithActions` story, and cover `actions` in the widget anatomy doc ([#80363](https://github.com/WordPress/gutenberg/pull/80363)). diff --git a/packages/wp-build/CHANGELOG.md b/packages/wp-build/CHANGELOG.md index 7f063d89dfddab..88a6d2ed3b9c45 100644 --- a/packages/wp-build/CHANGELOG.md +++ b/packages/wp-build/CHANGELOG.md @@ -7,7 +7,7 @@ - Widgets: carry a widget's declarative `actions` from `widget.json` into the generated PHP registry ([#80363](https://github.com/WordPress/gutenberg/pull/80363)). - Widgets: carry a widget's declarative `icon` reference from `widget.json` - into the generated PHP registry ([#80938](https://github.com/WordPress/gutenberg/issues/80938)). + into the generated PHP registry ([#80969](https://github.com/WordPress/gutenberg/pull/80969)). ### Bug Fixes From 8f3238313af1ff2cb244d9ece1a7e15c9b48cdff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dami=C3=A1n=20Su=C3=A1rez?= Date: Thu, 30 Jul 2026 15:19:19 +0100 Subject: [PATCH 13/19] update package lock file --- package-lock.json | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/package-lock.json b/package-lock.json index 80a498fcf92b2a..2378d06253c665 100644 --- a/package-lock.json +++ b/package-lock.json @@ -53110,6 +53110,7 @@ "@wordpress/core-data": "file:../../packages/core-data", "@wordpress/data": "file:../../packages/data", "@wordpress/dataviews": "file:../../packages/dataviews", + "@wordpress/dom": "file:../../packages/dom", "@wordpress/element": "file:../../packages/element", "@wordpress/hooks": "file:../../packages/hooks", "@wordpress/i18n": "file:../../packages/i18n", @@ -53121,7 +53122,8 @@ "@wordpress/warning": "file:../../packages/warning", "@wordpress/widget-dashboard": "file:../../packages/widget-dashboard", "@wordpress/widget-primitives": "file:../../packages/widget-primitives", - "fast-deep-equal": "^3.1.3" + "fast-deep-equal": "^3.1.3", + "html-react-parser": "^5.2.11" }, "devDependencies": { "@testing-library/dom": "^10.4.1", From a4a5e35f78186be43976b3e145da93db79c3bc4c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dami=C3=A1n=20Su=C3=A1rez?= Date: Fri, 31 Jul 2026 08:48:47 +0100 Subject: [PATCH 14/19] narrow metadata icon back to an element the wire reference lives on the record only --- packages/widget-primitives/src/types.ts | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/packages/widget-primitives/src/types.ts b/packages/widget-primitives/src/types.ts index 7146d3360c1bf3..5eed392e1ecf3a 100644 --- a/packages/widget-primitives/src/types.ts +++ b/packages/widget-primitives/src/types.ts @@ -165,10 +165,9 @@ export interface WidgetTypeMetadata< Item = unknown > { /** * Visual identifier for the widget type; hosts decide where, and - * whether, to render it. A reference in `widget.json`, or a rendered - * element in the module. + * whether, to render it. */ - icon?: WidgetIcon | WidgetIconReference; + icon?: WidgetIcon; /** * Grouping category. Core provides `dashboard`; plugins and themes may @@ -244,12 +243,6 @@ export interface WidgetTypeMetadata< Item = unknown > { */ export interface WidgetType< Item = unknown > extends WidgetTypeMetadata< Item > { - /** - * Always renderable: references are resolved before a `WidgetType` - * reaches a host. - */ - icon?: WidgetIcon; - /** * Script-module identifier resolved to a React component at render * time, produced from the conventional `render.*` entry point. From 2f7e6288f3a8077e1c77356c3c9a4da9042d4ebe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dami=C3=A1n=20Su=C3=A1rez?= Date: Fri, 31 Jul 2026 08:48:50 +0100 Subject: [PATCH 15/19] resolve icons off the loading gate widget types emit when modules land; icons patch in --- .../src/hooks/test/use-widget-types.test.tsx | 18 +++++++- .../src/hooks/use-widget-types.ts | 41 ++++++++++++++----- 2 files changed, 48 insertions(+), 11 deletions(-) diff --git a/packages/widget-primitives/src/hooks/test/use-widget-types.test.tsx b/packages/widget-primitives/src/hooks/test/use-widget-types.test.tsx index cfb427c149095a..d579327c266ac1 100644 --- a/packages/widget-primitives/src/hooks/test/use-widget-types.test.tsx +++ b/packages/widget-primitives/src/hooks/test/use-widget-types.test.tsx @@ -121,8 +121,24 @@ describe( 'useWidgetTypes', () => { ); await waitFor( () => expect( result.current[ 1 ] ).toBe( false ) ); + await waitFor( () => + expect( result.current[ 0 ][ 0 ].icon ).toBe( resolvedIcon ) + ); + } ); + + it( 'emits widget types before icon references settle', async () => { + registerIconResolver( + () => new Promise< WidgetIcon | null >( () => {} ) + ); - expect( result.current[ 0 ][ 0 ].icon ).toBe( resolvedIcon ); + const { result } = renderHook( () => + useWidgetTypes( iconReferenceRecords ) + ); + + await waitFor( () => expect( result.current[ 1 ] ).toBe( false ) ); + + expect( result.current[ 0 ] ).toHaveLength( 1 ); + expect( result.current[ 0 ][ 0 ].icon ).toBe( mockModuleIcon ); } ); it( 'keeps the module element when the reference does not resolve', async () => { diff --git a/packages/widget-primitives/src/hooks/use-widget-types.ts b/packages/widget-primitives/src/hooks/use-widget-types.ts index 4b71e013865610..abe0abb3fe994a 100644 --- a/packages/widget-primitives/src/hooks/use-widget-types.ts +++ b/packages/widget-primitives/src/hooks/use-widget-types.ts @@ -21,8 +21,9 @@ type UseWidgetTypesResult = readonly [ WidgetType[], boolean ]; * module's default export with the runtime fields (`name`, `renderModule`). * Attribute schemas pass through `resolveFields`, so attributes referencing * registered field types reach hosts as plain DataViews fields. Icon - * references resolve through the registered icon resolver, so hosts only - * receive renderable icons. + * references resolve through the registered icon resolver, off the loading + * flag: widget types emit as soon as their modules land, and each resolved + * icon patches in afterwards. * Pass `null`/`undefined` while records are still loading. * * @param records Host-supplied records, or `null`/`undefined` while loading. @@ -67,17 +68,12 @@ export function useWidgetTypes( const metadata = module.default as Partial< WidgetType >; /* - * The record's resolved reference wins; the module's - * element stands when there is none or it does not - * resolve. Non-elements are dropped. + * Only a renderable element may enter; the record's + * reference resolves after the gate and patches in. */ - const resolvedIcon = record.icon - ? await resolveIcon( record.icon ) - : null; const moduleIcon = isValidElement( metadata.icon ) ? metadata.icon : undefined; - const icon = resolvedIcon ?? moduleIcon; return { ...metadata, @@ -90,7 +86,7 @@ export function useWidgetTypes( : {} ), name: record.name as WidgetName, renderModule: record.render_module ?? '', - icon, + icon: moduleIcon, /* * `title` is required: * - Server-side title wins @@ -128,6 +124,31 @@ export function useWidgetTypes( results.filter( ( t ): t is WidgetType => t !== null ) ); setIsResolvingWidgetTypes( false ); + + /* + * Icons resolve off the loading gate. The resolved reference + * wins; the module's element stands when the reference does + * not resolve. + */ + for ( const record of records ) { + if ( ! record.icon ) { + continue; + } + + void resolveIcon( record.icon ).then( ( resolved ) => { + if ( cancelled || ! resolved ) { + return; + } + + setWidgetTypes( ( prev ) => + prev.map( ( widgetType ) => + widgetType.name === record.name + ? { ...widgetType, icon: resolved } + : widgetType + ) + ); + } ); + } } ); return () => { From ef50796d9714ebf638ad6283c17219beb7aa502c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dami=C3=A1n=20Su=C3=A1rez?= Date: Fri, 31 Jul 2026 08:48:53 +0100 Subject: [PATCH 16/19] restore currentColor on resolved icons the registry sanitizer strips fill from the svg root --- routes/dashboard/icons/index.ts | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/routes/dashboard/icons/index.ts b/routes/dashboard/icons/index.ts index a85f691f76e46c..ab61dc31bfdd26 100644 --- a/routes/dashboard/icons/index.ts +++ b/routes/dashboard/icons/index.ts @@ -9,7 +9,7 @@ import parse from 'html-react-parser'; import { store as coreStore } from '@wordpress/core-data'; import { resolveSelect } from '@wordpress/data'; import { safeHTML } from '@wordpress/dom'; -import { isValidElement } from '@wordpress/element'; +import { cloneElement, isValidElement } from '@wordpress/element'; import { registerIconResolver } from '@wordpress/widget-primitives'; import type { WidgetIcon } from '@wordpress/widget-primitives'; @@ -34,10 +34,22 @@ export function registerDashboardIconResolver() { * array; take the element. */ const parsed = parse( safeHTML( record.content.trim() ) ); - const element = Array.isArray( parsed ) + const found = Array.isArray( parsed ) ? parsed.find( isValidElement ) : parsed; - return isValidElement( element ) ? ( element as WidgetIcon ) : null; + if ( ! isValidElement( found ) ) { + return null; + } + + const element = found as WidgetIcon; + + /* + * The registry sanitizer strips `fill` from the root ``; + * restore inheritance so icons follow the surrounding color. + */ + return element.props.fill + ? element + : cloneElement( element, { fill: 'currentColor' } ); } ); } From 4ae8c4a87d89b40482c70c0fdf76cae3932a7cb8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dami=C3=A1n=20Su=C3=A1rez?= Date: Fri, 31 Jul 2026 12:49:28 +0100 Subject: [PATCH 17/19] hold the icon slot while references resolve a transparent stand-in prevents the title shift --- .../src/hooks/test/use-widget-types.test.tsx | 31 ++++++++++- .../src/hooks/use-widget-types.ts | 55 ++++++++++++++----- 2 files changed, 72 insertions(+), 14 deletions(-) diff --git a/packages/widget-primitives/src/hooks/test/use-widget-types.test.tsx b/packages/widget-primitives/src/hooks/test/use-widget-types.test.tsx index d579327c266ac1..8d557daae21cc3 100644 --- a/packages/widget-primitives/src/hooks/test/use-widget-types.test.tsx +++ b/packages/widget-primitives/src/hooks/test/use-widget-types.test.tsx @@ -6,7 +6,7 @@ import { renderHook, waitFor } from '@testing-library/react'; /** * WordPress dependencies */ -import { createElement } from '@wordpress/element'; +import { createElement, isValidElement } from '@wordpress/element'; /** * Internal dependencies @@ -78,6 +78,10 @@ const stringIconRecords: WidgetModuleRecord[] = [ }, ]; +const pendingIconRecords: WidgetModuleRecord[] = [ + { ...stringIconRecords[ 0 ], icon: 'core/pending' }, +]; + describe( 'useWidgetTypes', () => { afterEach( () => { unregisterFieldType( 'test/location' ); @@ -151,6 +155,31 @@ describe( 'useWidgetTypes', () => { expect( result.current[ 0 ][ 0 ].icon ).toBe( mockModuleIcon ); } ); + it( 'holds the icon slot with a stand-in while the reference resolves', async () => { + registerIconResolver( + () => new Promise< WidgetIcon | null >( () => {} ) + ); + + const { result } = renderHook( () => + useWidgetTypes( pendingIconRecords ) + ); + + await waitFor( () => expect( result.current[ 1 ] ).toBe( false ) ); + + expect( isValidElement( result.current[ 0 ][ 0 ].icon ) ).toBe( true ); + } ); + + it( 'clears the stand-in when the reference does not resolve', async () => { + const { result } = renderHook( () => + useWidgetTypes( pendingIconRecords ) + ); + + await waitFor( () => expect( result.current[ 1 ] ).toBe( false ) ); + await waitFor( () => + expect( result.current[ 0 ][ 0 ].icon ).toBeUndefined() + ); + } ); + it( 'drops a module icon that is not an element', async () => { const { result } = renderHook( () => useWidgetTypes( stringIconRecords ) diff --git a/packages/widget-primitives/src/hooks/use-widget-types.ts b/packages/widget-primitives/src/hooks/use-widget-types.ts index abe0abb3fe994a..d023809eba2408 100644 --- a/packages/widget-primitives/src/hooks/use-widget-types.ts +++ b/packages/widget-primitives/src/hooks/use-widget-types.ts @@ -1,14 +1,32 @@ /** * WordPress dependencies */ -import { isValidElement, useEffect, useState } from '@wordpress/element'; +import { + createElement, + isValidElement, + useEffect, + useState, +} from '@wordpress/element'; /** * Internal dependencies */ import { resolveFields } from '../field-types'; import { resolveIcon } from '../icon-resolver'; -import type { WidgetModuleRecord, WidgetName, WidgetType } from '../types'; +import type { + WidgetIcon, + WidgetModuleRecord, + WidgetName, + WidgetType, +} from '../types'; + +/* + * Transparent stand-in for an icon reference that has not resolved yet: + * it holds the icon slot so titles do not shift when the icon lands. + */ +const pendingIcon: WidgetIcon = createElement( 'svg', { + viewBox: '0 0 24 24', +} ); /* `true` while records or their metadata imports are still resolving; hosts must not treat a widget instance as missing until it is `false`. */ @@ -68,12 +86,15 @@ export function useWidgetTypes( const metadata = module.default as Partial< WidgetType >; /* - * Only a renderable element may enter; the record's - * reference resolves after the gate and patches in. + * Only a renderable element may enter; a pending + * reference holds the slot with the stand-in until + * it resolves after the gate. */ const moduleIcon = isValidElement( metadata.icon ) ? metadata.icon : undefined; + const icon = + moduleIcon ?? ( record.icon ? pendingIcon : undefined ); return { ...metadata, @@ -86,7 +107,7 @@ export function useWidgetTypes( : {} ), name: record.name as WidgetName, renderModule: record.render_module ?? '', - icon: moduleIcon, + icon, /* * `title` is required: * - Server-side title wins @@ -127,8 +148,8 @@ export function useWidgetTypes( /* * Icons resolve off the loading gate. The resolved reference - * wins; the module's element stands when the reference does - * not resolve. + * wins; when it does not resolve, the module's element + * stands and the stand-in clears. */ for ( const record of records ) { if ( ! record.icon ) { @@ -136,16 +157,24 @@ export function useWidgetTypes( } void resolveIcon( record.icon ).then( ( resolved ) => { - if ( cancelled || ! resolved ) { + if ( cancelled ) { return; } setWidgetTypes( ( prev ) => - prev.map( ( widgetType ) => - widgetType.name === record.name - ? { ...widgetType, icon: resolved } - : widgetType - ) + prev.map( ( widgetType ) => { + if ( widgetType.name !== record.name ) { + return widgetType; + } + + if ( resolved ) { + return { ...widgetType, icon: resolved }; + } + + return widgetType.icon === pendingIcon + ? { ...widgetType, icon: undefined } + : widgetType; + } ) ); } ); } From fd1e5c42cabb6cb263ab65a39211c376e47ccc03 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dami=C3=A1n=20Su=C3=A1rez?= Date: Fri, 31 Jul 2026 13:38:56 +0100 Subject: [PATCH 18/19] register the icon resolver at dashboard init init modules run before the page renders --- packages/dashboard-init/CHANGELOG.md | 2 ++ packages/dashboard-init/package.json | 6 +++++- .../dashboard-init/src}/icons/index.ts | 0 packages/dashboard-init/src/index.ts | 13 ++++++++++--- routes/dashboard/package.json | 4 +--- routes/dashboard/stage.tsx | 2 -- 6 files changed, 18 insertions(+), 9 deletions(-) rename {routes/dashboard => packages/dashboard-init/src}/icons/index.ts (100%) diff --git a/packages/dashboard-init/CHANGELOG.md b/packages/dashboard-init/CHANGELOG.md index 2e271ef03a604b..481d96810ecd52 100644 --- a/packages/dashboard-init/CHANGELOG.md +++ b/packages/dashboard-init/CHANGELOG.md @@ -2,4 +2,6 @@ ## Unreleased +- Register the dashboard's widget icon resolver at init, before the page + renders ([#80969](https://github.com/WordPress/gutenberg/pull/80969)). - Initial version of the package. diff --git a/packages/dashboard-init/package.json b/packages/dashboard-init/package.json index 803548455b6b6d..d188bce1cbcae7 100644 --- a/packages/dashboard-init/package.json +++ b/packages/dashboard-init/package.json @@ -34,7 +34,11 @@ "dependencies": { "@wordpress/core-data": "file:../core-data", "@wordpress/data": "file:../data", - "@wordpress/i18n": "file:../i18n" + "@wordpress/dom": "file:../dom", + "@wordpress/element": "file:../element", + "@wordpress/i18n": "file:../i18n", + "@wordpress/widget-primitives": "file:../widget-primitives", + "html-react-parser": "^5.2.11" }, "publishConfig": { "access": "public" diff --git a/routes/dashboard/icons/index.ts b/packages/dashboard-init/src/icons/index.ts similarity index 100% rename from routes/dashboard/icons/index.ts rename to packages/dashboard-init/src/icons/index.ts diff --git a/packages/dashboard-init/src/index.ts b/packages/dashboard-init/src/index.ts index 13497cac64c921..cf8eea106aa35d 100644 --- a/packages/dashboard-init/src/index.ts +++ b/packages/dashboard-init/src/index.ts @@ -6,9 +6,14 @@ import { store as coreStore } from '@wordpress/core-data'; import { __ } from '@wordpress/i18n'; /** - * Register the widget-modules discovery entity before the dashboard renders, - * so the stage's `getEntityRecords` read resolves and feeds the records to - * `useWidgetTypes`. + * Internal dependencies + */ +import { registerDashboardIconResolver } from './icons'; + +/** + * Register the widget-modules discovery entity and the icon resolver + * before the dashboard renders, so the stage's `getEntityRecords` read + * resolves and feeds the records to `useWidgetTypes`. * * This function is mandatory - all init modules must export 'init'. */ @@ -17,6 +22,8 @@ export async function init() { return; } + registerDashboardIconResolver(); + dispatch( coreStore ).addEntities( [ { name: 'widgetModule', diff --git a/routes/dashboard/package.json b/routes/dashboard/package.json index 6409c588a35c57..a9e6e32a2c5fe1 100644 --- a/routes/dashboard/package.json +++ b/routes/dashboard/package.json @@ -15,7 +15,6 @@ "@wordpress/core-data": "file:../../packages/core-data", "@wordpress/data": "file:../../packages/data", "@wordpress/dataviews": "file:../../packages/dataviews", - "@wordpress/dom": "file:../../packages/dom", "@wordpress/element": "file:../../packages/element", "@wordpress/hooks": "file:../../packages/hooks", "@wordpress/i18n": "file:../../packages/i18n", @@ -27,8 +26,7 @@ "@wordpress/warning": "file:../../packages/warning", "@wordpress/widget-dashboard": "file:../../packages/widget-dashboard", "@wordpress/widget-primitives": "file:../../packages/widget-primitives", - "fast-deep-equal": "^3.1.3", - "html-react-parser": "^5.2.11" + "fast-deep-equal": "^3.1.3" }, "devDependencies": { "@testing-library/dom": "^10.4.1", diff --git a/routes/dashboard/stage.tsx b/routes/dashboard/stage.tsx index e6cb3459ef7e3c..eeb714b74cc63f 100644 --- a/routes/dashboard/stage.tsx +++ b/routes/dashboard/stage.tsx @@ -21,11 +21,9 @@ import { * Internal dependencies */ import { registerDashboardFieldTypes } from './field-types'; -import { registerDashboardIconResolver } from './icons'; import { useDashboardGridSettings, useDashboardLayout } from './hooks'; registerDashboardFieldTypes(); -registerDashboardIconResolver(); function Dashboard() { const [ layout, setLayout, resetLayout ] = useDashboardLayout( From f8a7e4354db5835a820e463f24bb6bac1c63bf2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dami=C3=A1n=20Su=C3=A1rez?= Date: Fri, 31 Jul 2026 14:47:12 +0100 Subject: [PATCH 19/19] udpate pnpm lock file --- package-lock.json | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/package-lock.json b/package-lock.json index 71b9e407040787..0a78f0b330e4dd 100644 --- a/package-lock.json +++ b/package-lock.json @@ -51367,7 +51367,11 @@ "dependencies": { "@wordpress/core-data": "file:../core-data", "@wordpress/data": "file:../data", - "@wordpress/i18n": "file:../i18n" + "@wordpress/dom": "file:../dom", + "@wordpress/element": "file:../element", + "@wordpress/i18n": "file:../i18n", + "@wordpress/widget-primitives": "file:../widget-primitives", + "html-react-parser": "^5.2.11" }, "engines": { "node": ">=20.10.0", @@ -54723,7 +54727,6 @@ "@wordpress/core-data": "file:../../packages/core-data", "@wordpress/data": "file:../../packages/data", "@wordpress/dataviews": "file:../../packages/dataviews", - "@wordpress/dom": "file:../../packages/dom", "@wordpress/element": "file:../../packages/element", "@wordpress/hooks": "file:../../packages/hooks", "@wordpress/i18n": "file:../../packages/i18n", @@ -54735,8 +54738,7 @@ "@wordpress/warning": "file:../../packages/warning", "@wordpress/widget-dashboard": "file:../../packages/widget-dashboard", "@wordpress/widget-primitives": "file:../../packages/widget-primitives", - "fast-deep-equal": "^3.1.3", - "html-react-parser": "^5.2.11" + "fast-deep-equal": "^3.1.3" }, "devDependencies": { "@testing-library/dom": "^10.4.1",