Block API: Support file: paths for block icons in block.json#79658
Block API: Support file: paths for block icons in block.json#79658mgiannopoulos24 wants to merge 8 commits into
Conversation
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
|
Warning: Type of PR label mismatch To merge this PR, it requires exactly 1 label indicating the type of PR. Other labels are optional and not being checked here.
Read more about Type labels in Gutenberg. Don't worry if you don't have the required permissions to add labels; the PR reviewer should be able to help with the task. |
|
Warning: Type of PR label mismatch To merge this PR, it requires exactly 1 label indicating the type of PR. Other labels are optional and not being checked here.
Read more about Type labels in Gutenberg. Don't worry if you don't have the required permissions to add labels; the PR reviewer should be able to help with the task. |
|
👋 Thanks for your first Pull Request and for helping build the future of Gutenberg and WordPress, @mgiannopoulos24! In case you missed it, we'd love to have you join us in our Slack community. If you want to learn more about WordPress development in general, check out the Core Handbook full of helpful information. |
ciampo
left a comment
There was a problem hiding this comment.
I don't think that supporting inline SVG code for the Icon is the right choice here, it's hacky and potentially insecure.
A better approach:
- keep Icon unchanged,
- resolve file:./icon.svg in the block metadata pipeline,
- validate/sanitize the SVG,
- pass a block-specific safe representation to BlockIcon.
The existing WP_Icons_Registry sanitization model is a much better precedent than dangerouslySetInnerHTML in @wordpress/components
…n block registration pipeline
f42c224 to
f7e78dd
Compare
The PR now doesn't dangerously parse an SVG string into the Icon component.
I will let other folks that are more experienced with the block API review the PR going forward.
|
I dismissed my blocking review, and I'll let folks that are more experienced with the block API review this PR, once marked as ready for review. |
What?
Support
file:paths for block icons inblock.json, allowing block authors to use"icon": "file:./assets/icon.svg"instead of being forced to set custom SVG icons via JavaScript only.Also updates the JSON Schema to accept icon descriptor objects (
src,background,foreground,shadowColor) in addition to the existing string form.Why?
Currently, custom SVG icons can only be set via
registerBlockTypein JavaScript. Theblock.jsoniconfield only accepts Dashicon slugs. This means block authors who want a custom SVG icon must either:block.jsonand JS registrationblock.json(server-side registration, REST API exposure, etc.)This PR closes that gap by bringing feature parity between
block.jsonand JS-based icon registration, following the establishedfile:prefix pattern already used bystyle,editorScript,render, andvariationsfields.How?
Four layers of changes:
schemas/json/block.json): Updatediconfrom{ "type": "string" }to aoneOfaccepting either a string or an object withsrc,background,foreground,shadowColor. Variation icon reuses via$ref.lib/blocks.php): Newgutenberg_resolve_block_icon_path()hooked intoblock_type_metadata. When the icon starts withfile:, it usesremove_block_asset_path_prefix()(the same utility used by scripts/styles/render) to resolve the path relative toblock.json, reads the SVG file, and inlines the content. Handles both string and object forms.packages/scripts/config/webpack.config.js): The CopyWebpackPlugintransform()now detectsfile:icons and inlines the SVG content at build time, so the builtblock.jsonis self-contained.packages/components/src/icon/index.tsx): The<Icon>component now detects SVG markup strings (starting with<svgor<?xml) and renders them directly with properwidth/heightfrom thesizeprop, rather than passing them through as Dashicon names.Testing Instructions
1. Schema validation
All 126
block.jsonfiles should pass.2. Unit tests
All 13 tests should pass (10 existing + 3 new for SVG string rendering).
3. Manual test with a custom block
src/assets/icon.svg)"icon": "file:./assets/icon.svg"inblock.jsonnpm run build(ornpx wp-scripts build)4. Object form test
{ "icon": { "src": "file:./assets/icon.svg", "background": "#ff0000", "foreground": "#ffffff" } }Verify the icon renders with colored background in the inserter.
5. PHP-only registration (no build step)
For blocks not using
@wordpress/scripts, verify the icon resolves correctly via server-side registration withregister_block_type_from_metadata().Use of AI Tools
This pull request was authored with the assistance of AI tooling (Deepseek via opencode). All AI-generated code has been reviewed and tested for correctness. See the WordPress AI Guidelines: https://make.wordpress.org/ai/handbook/ai-guidelines/.