Skip to content
This repository was archived by the owner on Oct 4, 2022. It is now read-only.

Commit a4fc1ce

Browse files
Merge pull request #1135 from Yoast/P2-686-render-svg-icon
P2 686 render svg icon
2 parents 6d46f6d + bf83d34 commit a4fc1ce

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

packages/schema-blocks/css/schema-blocks.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,3 +126,7 @@
126126
.yoast-block-date-picker .components-datetime__time > fieldset:last-child {
127127
display: none;
128128
}
129+
130+
.yoast-schema-blocks-icon svg {
131+
fill:none;
132+
}

packages/schema-blocks/src/core/blocks/BlockDefinition.tsx

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import { createElement, Fragment, ReactElement } from "@wordpress/element";
2-
import { registerBlockType, BlockConfiguration, BlockEditProps, BlockSaveProps } from "@wordpress/blocks";
3-
import { InspectorControls } from "@wordpress/block-editor";
2+
import {
3+
registerBlockType,
4+
BlockConfiguration,
5+
BlockEditProps,
6+
BlockSaveProps,
7+
} from "@wordpress/blocks";
8+
import { InspectorControls, BlockIcon } from "@wordpress/block-editor";
49
import BlockInstruction from "./BlockInstruction";
510
import Definition from "../Definition";
611
import BlockRootLeaf from "../../leaves/blocks/BlockRootLeaf";
@@ -87,6 +92,10 @@ export default class BlockDefinition extends Definition {
8792

8893
logger.debug( "registering block " + name );
8994

95+
if ( configuration.icon && typeof configuration.icon === "string" && configuration.icon.startsWith( "<svg" ) ) {
96+
configuration.icon = this.createBlockIcon( configuration );
97+
}
98+
9099
// Register the block to WordPress.
91100
registerBlockType( name, configuration );
92101
// Register the block with our own code.
@@ -105,4 +114,20 @@ export default class BlockDefinition extends Definition {
105114
.map( ( instruction, index ) => instruction.sidebar( props, index ) )
106115
.filter( e => e !== null );
107116
}
117+
118+
/**
119+
* Creates an block icon.
120+
*
121+
* @param {MutableBlockConfiguration} configuration The block configuration.
122+
*
123+
* @returns {ReactElement[]} The sidebar element to render.
124+
*/
125+
private createBlockIcon( configuration: MutableBlockConfiguration ): ReactElement {
126+
const icon = <span
127+
className="yoast-schema-blocks-icon"
128+
dangerouslySetInnerHTML={ { __html: configuration.icon as string } }
129+
/>;
130+
131+
return <BlockIcon icon={ icon } />;
132+
}
108133
}

0 commit comments

Comments
 (0)