Icon field for JavaScript blocks
Put the Easy SVG Pro icon picker in the sidebar and the toolbar of a block you write in JavaScript.
A block written in JavaScript can use the same icon picker as the PHP-only blocks: your collections, search and preview. Easy SVG Pro gives you two React components on window.EasySvgPro:
IconControlfor the sidebar (InspectorControls),IconToolbarButtonfor the block toolbar (BlockControls).
Both save the icon name as a string, for example my-set/star. You draw it in PHP with easy_svg_icon(), the free plugin's function, so the page keeps working without Pro.
templates/javascript-block/ in plain JavaScript without a build step, or templates/javascript-block-jsx/ in JSX with block.json and @wordpress/scripts.Before you start
- Easy SVG (free) and Easy SVG Pro are active, and your Pro license is active.
- Under Easy SVG → Settings → Features, in the group For developers, PHP-only blocks is switched on. The same switch covers JavaScript blocks.
- The site has at least one icon, in the free library or in a set.
The recommended way: import, like @wordpress/*
Write the block in JSX with @wordpress/scripts and import the components the way you import WordPress's own:
import { IconControl, IconToolbarButton } from '@unleash-wp/easy-svg-pro';
@unleash-wp/easy-svg-pro is not an npm package, so nothing is downloaded. A webpack.config.js next to your package.json tells the build that the name is Easy SVG Pro's script on the page. The build then writes window.EasySvgPro for it and puts the handle easy-svg-pro-icon-control into build/index.asset.php by itself. This is how WooCommerce offers @woocommerce/*.
const defaultConfig = require( '@wordpress/scripts/config/webpack.config' );
const DependencyExtractionWebpackPlugin = require( '@wordpress/dependency-extraction-webpack-plugin' );
const EASY_SVG_PRO = '@unleash-wp/easy-svg-pro';
module.exports = {
...defaultConfig,
plugins: [
...defaultConfig.plugins.filter( ( plugin ) => ! ( plugin instanceof DependencyExtractionWebpackPlugin ) ),
new DependencyExtractionWebpackPlugin( {
requestToExternal: ( request ) => ( EASY_SVG_PRO === request ? 'EasySvgPro' : undefined ),
requestToHandle: ( request ) => ( EASY_SVG_PRO === request ? 'easy-svg-pro-icon-control' : undefined ),
} ),
],
};
For a dynamic block, src/block.json names "editorScript": "file:./index.js" and "render": "file:./render.php". PHP registers the block with one line, register_block_type( __DIR__ . '/build' ), and render.php prints the icon with easy_svg_icon(). The editor then needs Easy SVG Pro: without it, WordPress leaves the editor script out and says the block is not supported.
The complete block, with easy-svg-pro.d.ts for your editor's autocomplete: templates/javascript-block-jsx/.
Without a build step
The same components are on window.EasySvgPro. This way suits a block that must also load without Pro.
Register the block
Add the script handle easy-svg-pro-icon-control to your editor script's dependencies. Easy SVG Pro registers it on init at priority 20. Check for it at priority 21 or later: on a site without Pro the dependency stays out and the block still loads.
add_action( 'init', function () {
$deps = array( 'wp-blocks', 'wp-element', 'wp-block-editor', 'wp-components' );
if ( wp_script_is( 'easy-svg-pro-icon-control', 'registered' ) ) {
$deps[] = 'easy-svg-pro-icon-control';
}
wp_register_script( 'acme-badge-editor', plugins_url( 'editor.js', __FILE__ ), $deps, '1.0.0', true );
register_block_type( 'acme/badge', array(
'editor_script' => 'acme-badge-editor',
'attributes' => array(
'icon' => array( 'type' => 'string', 'default' => '' ),
),
'render_callback' => function ( $attributes ) {
return function_exists( 'easy_svg_icon' ) ? easy_svg_icon( $attributes['icon'] ) : '';
},
) );
}, 21 );
Use it in the sidebar and the toolbar
( function ( wp ) {
var el = wp.element.createElement;
var be = wp.blockEditor;
var pro = window.EasySvgPro || {};
wp.blocks.registerBlockType( 'acme/badge', {
apiVersion: 3,
title: 'Badge',
attributes: { icon: { type: 'string', default: '' } },
edit: function ( props ) {
var setIcon = function ( icon ) {
props.setAttributes( { icon: icon } );
};
return el(
'div',
be.useBlockProps(),
el(
be.InspectorControls,
null,
pro.IconControl
? el( pro.IconControl, { label: 'Icon', value: props.attributes.icon, collections: [], onChange: setIcon } )
: el( wp.components.TextControl, { label: 'Icon name', value: props.attributes.icon, onChange: setIcon } )
),
pro.IconToolbarButton
? el( be.BlockControls, { group: 'other' }, el( pro.IconToolbarButton, { value: props.attributes.icon, onChange: setIcon } ) )
: null,
props.attributes.icon || 'No icon yet'
);
},
save: function () {
return null;
},
} );
}( window.wp ) );
Put IconToolbarButton straight into BlockControls, not into a Dropdown: it opens the icon library itself, and a dropdown would close as the library opens.
Props
| Prop | IconControl | IconToolbarButton | What it does |
|---|---|---|---|
value | yes | yes | The saved icon name, collection/icon. |
onChange | yes | yes | Gets the new name. IconControl passes an empty string after Remove. |
collections | yes | yes | Collection slugs to offer. Empty offers all. |
label | yes | yes | The field's label, or the button's tooltip. |
help | yes | A line of help under the field. | |
icon | yes | The button's icon, any icon ToolbarButton takes. |
window.EasySvgPro.api is 1. It changes only when a prop or the global changes in a way your block would notice.
Without a license or with the switch off
| Situation | What the editor shows | The page |
|---|---|---|
| License active, switch on | The picker in the sidebar and the toolbar | Draws the icon |
| License ended | The saved name with a note; the toolbar button is greyed out | Draws the icon |
| Switch PHP-only blocks off | IconControl shows the saved name, locked, with a note that names the switch; your fallback is not used | Draws the icon |
| Easy SVG Pro deactivated | Your fallback; with the import way the block is not supported | Draws icons of the free library. Icons of your sets and of WordPress's own collection need Pro active |
The saved value is never changed or emptied in any of these cases.