SVG Icon API: Iteration for WordPress 7.1
- Core ticket: https://core.trac.wordpress.org/ticket/64847
- Important Issues and PRs:
- https://github.com/WordPress/gutenberg/issues/16484
- https://github.com/WordPress/gutenberg/pull/72215
- https://github.com/WordPress/gutenberg/pull/71227
We have stabilized three core features and shipped them in 7.0 Beta1:
WP_Icons_Registryclass: The singleton class that loads core SVG icons from a manifest file, stores them, and exposes methods to check registration and retrieve icon data and content. There is currently no way to register a custom icon.WP_REST_Icons_Controller: The REST API controller that exposes the icon registry overwp/v2/icons, letting the editor list icons and fetch a single icon by name.- Icon block (
core/icon): The core block that retrieves a list of icons from the endpoint and lists them in a modal. The selected icon name is saved as an attribute, and the content is rendered server-side via the icon registry.
To make the Icon API more robust and extensible, I believe the following features are necessary:
Code quality
- [x] Use snake_case "file_path" key: https://github.com/WordPress/gutenberg/pull/79100
- [ ] SVG sanitization (ref): There is no core function to properly sanitize SVGs, so a class method currently does the job, but it is not perfect, and there is room for improvement. See #75550 for more details. Maybe we could create a core function like
sanitize_svg()instead. https://github.com/WordPress/gutenberg/pull/75550
✅ Icon registry enhancements
- [x] Icon namespace validation (ref): Currently, custom icon registration is not allowed, and the namespace is hardcoded as
core/. If we allow custom icon registration in the future, we will need to strictly check that the icon name follows the{namespace/icon-name}format. https://github.com/WordPress/gutenberg/pull/76079
✅ REST API enhancements
- [x] Search based on keywords: The current endpoint allows us to filter icons by the
searchparameter, but only thenamefield is referenced. We could also reference thelabelfield and an optionalkeywordsfield. https://github.com/WordPress/gutenberg/pull/75878 - [x] Create public APIs (ref): I think we need APIs like the one below to allow consumers to filter the core icons and submit custom icons.
register_icon()functionunregister_icon()functioncore-iconstheme support: This is similar to thecore-block-patternstheme support: if this support is disabled, all core icons will not be registered.- https://github.com/WordPress/gutenberg/pull/77260
- https://github.com/WordPress/gutenberg/pull/78332
✅ Collection Support
Similar to Font Library, it should be possible to support collection: consumers will want to group icon sets based on their own brand.
- [x] Create a registry like
WP_Icon_Colloections_Registry - [x] Create an endpoint like
WP_REST_Icons_Colloections_Controller - [x] Create public APIs:
wp_register_icon_collection()functionwp_unregister_icon_collection()function_wp_register_default_icon_collectionsfunction
- https://github.com/WordPress/gutenberg/pull/77260
- https://github.com/WordPress/gutenberg/pull/79681
Create Icon Picker Modal component
- [ ] To avoid increasing bundle size, the initial implementation built a custom picker without using
DataViewPicker. (ref) Perhaps it should be possible to add a component likeIconPickerModalto the@wordpress/block-editorpackage and load it from a block. This icon picker should be useful for blocks other than the Icon block as well.DataViewsPickerbased UI- Filter icons by category
- https://github.com/WordPress/gutenberg/pull/76787
Icon block enhancements
For example, the early prototype of the Icon Block had the following features, which we can consider as enhancements:
- [x] Flip and rotate icon https://github.com/WordPress/gutenberg/pull/77017
- [ ] Should we allow icons to have links? https://github.com/WordPress/gutenberg/issues/79057
Customizable icon support for other blocks
Some icons render their own hard-coded SVG content or symbols. Make those icons selectable via the Icon API.
- [ ] Navigation block: Toggle button, Overlay Close button
- https://github.com/WordPress/gutenberg/issues/61755
- [ ] Breadcrumbs: Separator (The text can be changed, but the icon cannot be used)
- https://github.com/WordPress/gutenberg/pull/77059
- [ ] Navigation Overly Close block: Close icon
- [ ] Accordion Heading lock: Toggle icon (
+symbol)- https://github.com/WordPress/gutenberg/issues/73214
- [ ] Details block: Browser default triangle symbol
- https://github.com/WordPress/gutenberg/issues/51019
Inline SVG icon (Inline format)
- [ ] At the moment I don't know how to implement this. Perhaps it will be based on "BITS". (ref)
- https://github.com/WordPress/gutenberg/issues/79057#issuecomment-4722874149
@ryanwelcher @mcsf @tyxla @fabiankaegy, If there's anything I've missed or got wrong, feel free to update the task.
Some ideas for performance improvements for the Icons API and block; I think these can go in WP 7.0.
- Icon Block: Fetch only selected icon, fetch collection on demand. Currently, this is the largest request on the page when an icon block is present. See the screenshot.
- REST Controller: Fix collection params schema and support the same arguments in the
getIconsselector. The schema states the default per_page arg is 10, but it returns all the results. You can check defaults viahttps://example.test/wp-json/wp/v2/icons?_method=OPTIONS.
Screenshot
Rather than suggesting ideas for areas where icons are already being used, I would like to propose an additional feature for blocks.
I would like the Button block to support setting an icon. If implemented, I believe this would become a representative and practical use case for the icon registry.
Specifically, I would like:
- The ability to select an icon that appears before the Button block content
- The ability to select an icon that appears after the Button block content
For icon selection, please use the newly added Icon Picker Modal component.
The repository created by @ndiego — https://github.com/ndiego/enable-button-icons is extremely helpful and very close to what I have in mind. Please check the behavior in the Playground linked in the README.
Additionally, I have some feedback regarding the icon and the icon picker:
It would be beneficial to support something like keywords, separate from categories. For example, a chevron-shaped icon (such as "<") should also appear when searching for a keyword like "arrow."
If keyword functionality has already been implemented or is currently under consideration, please disregard this suggestion.
Hi, thanks for this work on the new Icon API, it's great to see this moving forward! 🙂
I've been following the discussions for a while, and here are a couple of ideas that might be worth considering:
- Icon Collections @t-hamano mentions Category support, I'd suggest also introducing collections, aligned with the Font Collections concept already in core. In the Font Library, a collection is one "source" (e.g. Google Fonts) containing many font families, with categories (Sans Serif, Display, etc.) as semantic grouping. For icons, the same split would work:
- Collection: one icon set/source (e.g. "WordPress Icons", "Heroicons", "Theme Icons").
- Category: semantic grouping (e.g. "Arrows", "Social", "UI") that can span collections.
- Icon Library Core already has a Font Library with a dedicated UI. I'd find it valuable to eventually have an Icon Library in the same spirit: a dedicated place to browse and manage icon sets (core, theme, plugins, and maybe user-uploaded later).
Thanks for creating this list :)
In regards to the icon picker and performance improvements. The Icon Picker we've built at 10up years ago has done quite a bit of that work already: https://github.com/10up/block-components/tree/develop/components/icon-picker With icons we encountered large icon systems like all of googles material icons library which then meant we had to look at react virtualization libraries etc to make the scrolling etc performant :) Happy for any of that work to help in moving the core implementation here forward. All the code is fully open source 👍
Thanks everyone for the feedback!
Some ideas for performance improvements for the Icons API and block; I think these can go in WP 7.0.
I'll try to fix this during the 7.0 release cycle.
- The ability to select an icon that appears before the Button block content
- The ability to select an icon that appears after the Button block content
This will be part of our implementation of inline SVG icons, which should allow us to insert icons anywhere in any block that supports RichText.
It would be beneficial to support something like keywords, separate from categories. For example, a chevron-shaped icon (such as "<") should also appear when searching for a keyword like "arrow."
Just as blocks support keyword search, let's implement this for icons as well.
- Collection: one icon set/source (e.g. "WordPress Icons", "Heroicons", "Theme Icons").
- Category: semantic grouping (e.g. "Arrows", "Social", "UI") that can span collections.
I initially planned to only support categories, but this is a very good idea. The code might look like this:
register_icon_collection(
'my-icons',
array(
'label' => __( 'My Icons', 'my-plugin' ),
'categories' => array(
array(
'slug' => 'arrows',
'title' => __( 'Arrows', 'my-plugin' ),
),
array(
'slug' => 'Social',
'title' => __( 'Social', 'my-plugin' ),
),
),
)
);
register_icon(
'my-plugin/arrow-up',
array(
'label' => __( 'Arrow Up', 'my-plugin' ),
'filePath' => __DIR__ . '/arrow-up.svg',
'collection' => 'my-icons',
'categories' => array(
'arrows',
),
)
);
register_icon(
'my-plugin/arrow-down',
array(
'label' => __( 'X', 'gutenberg' ),
'filePath' => __DIR__ . '/arrow-down.svg',
'collection' => 'my-icons',
'categories' => array(
'social',
),
)
);
Thanks for everyones work on this so far! I agree with the others, the ability to register a custom icon set will be critical. Builders and agencies will want to ship custom icon sets for their clients and product creators will want to ship custom icon sets for their user base.
I was rallying for this to make v1 of the icon block, but looks like it might have to wait?
Lets use these for the admin icons
Just echoing @mikemcalister a little more bluntly: I am super excited for this block, but it won't be very much use to me until I can register my own icons.
I like the thought of a manifest file as a means of registering them. Other ideas for custom icon registering can be found in the Icon Block plugin's repo.
Most of those include ways of defining an icon's category and keywords/synonyms in the registration function or registry file.
...it won't be very much use to me until I can register my own icons.
Very much agree with this. Love that we have an icon block, but it's critical that I can add my own.
I would like the Button block to support setting an icon.
This would also be a huge value add for us and our clients. I'll add that being able to use icons in place of the markers for list items would be amazing too.
With WordPress 7.1 Beta1 about a month away, I personally aim to achieve at least the following with 7.1:
- Foundation for custom icon registration
- Public function for rendering icons
Here is a summary of the PRs to achieve these two goals.
| Summary | Gutenberg PR | WordPress PR |
|---|---|---|
Use snake_case file_path key in icon registry |
✅ https://github.com/WordPress/gutenberg/pull/79100 | ✅ https://github.com/WordPress/wordpress-develop/pull/12149 |
| ~Ship all SVG icons to WordPress core~ | ❌ https://github.com/WordPress/gutenberg/pull/79102 | ❌ https://github.com/WordPress/wordpress-develop/pull/12151 |
| Add APIs for collection and icon registration | ✅ https://github.com/WordPress/gutenberg/pull/77260 ✅ https://github.com/WordPress/gutenberg/pull/79623 | ⚒️ https://github.com/WordPress/wordpress-develop/pull/11559 |
| Add PHP method(s) for rendering inline SVG icons from the registry | ✅ https://github.com/WordPress/gutenberg/pull/78332 | ⚒️ https://github.com/WordPress/wordpress-develop/pull/12010 |
In parallel with this, we are also undertaking an initiative to recreate several icons with a stroke-based design. https://github.com/WordPress/gutenberg/pull/78808/
@WordPress/gutenberg-core, I would appreciate any assistance from anyone who has bandwidth to help complete these tasks 🙇♂️
Update: I have created a test plugin to register three icon collections to verify the usage of the icon API in the real world.
- GitHub: https://github.com/t-hamano/icon-collections
- Zip download: https://github.com/t-hamano/icon-collections/archive/refs/heads/main.zip
This plugin registers nearly 9,000 icons 😄
⚠️ Unfortunately, this places an excessive load on the icon picker within the Icon block, causing it to become very sluggish.
https://github.com/user-attachments/assets/0796d8c8-c28a-413f-9726-67fff3fc93f5
We likely need to implement some measures to improve performance.
- Grouping by collection. This was a feature improvement I was originally considering.
- Implement pagination or render icons using DataView.
- Some form of lazy loading strategy
cc @mcsf @tyxla @Mamaduka
- Grouping by collection. This was a feature improvement I was originally considering.
- Implement pagination or render icons using DataView.
- Some form of lazy loading strategy
Thanks for stress-testing this! All three sound worthwhile as mutually complementary improvements.
Nice. It definitely needs some pagination and infinite scrolling.
Implemented a PR that filters icons by collection and asynchronously loads icons for performance improvement. https://github.com/WordPress/gutenberg/pull/79681
I believe the foundation for the icon API in the 7.1 release is largely in place. Thank you all for your cooperation! Let's continue to focus on enhancing functionality and improving quality. I plan to summarize the API overview in a Dev Note.
While wiring widget icons to the registry (#80969) we hit a sanitization gap: WP_Icons_Registry::sanitize_icon_content() does not allow fill on the root <svg>, so registered content loses fill="currentColor" and stops inheriting the surrounding color. Every icon in packages/icons declares it on the root; screenshots and details in this thread: https://github.com/WordPress/gutenberg/pull/80969#discussion_r3688317678
Consumers can re-add the attribute client-side (that PR's resolver does), but since the class ships in Core as of 7.0, the allow-list fix belongs there. Would it make sense to allow fill on svg as part of this iteration, and mirror it in the compat copy?
While wiring widget icons to the registry (#80969) we hit a sanitization gap:
WP_Icons_Registry::sanitize_icon_content()does not allowfillon the root<svg>, so registered content losesfill="currentColor"and stops inheriting the surrounding color. Every icon inpackages/iconsdeclares it on the root; screenshots and details in this thread: #80969 (comment)Consumers can re-add the attribute client-side (that PR's resolver does), but since the class ships in Core as of 7.0, the allow-list fix belongs there. Would it make sense to allow
fillonsvgas part of this iteration, and mirror it in the compat copy?
We discussed that gap with fill and stroke on the root element with @t-hamano while reviewing the dev note - it came up as something that wasn't immediately clear. Personally, I would be happy to add support for those. It's a gap that we still have time to fill if someone has the time to open a PR. @t-hamano WDYT?
We discussed that gap with
fillandstrokeon the root element with @t-hamano while reviewing the dev note - it came up as something that wasn't immediately clear. Personally, I would be happy to add support for those. It's a gap that we still have time to fill if someone has the time to open a PR. @t-hamano WDYT?
All most all sanitization issues should be resolved once #75550 is merged, but it's a very large change and I'm not sure if it will make it into 7.1. For now, I think we can ship https://github.com/WordPress/gutenberg/pull/81011 first.
@t-hamano Quick question as I was asked this by another person: is there going to be an interface for adding a custom SVG icon in the future? Right now the only way to add custom icons is through the Icon API which is not useful for people who do not code and rely on being able to change things in the editor. I did not see any ticket specificially for this so I thought I'd ask the question here.