Skip to content
This repository was archived by the owner on Oct 4, 2022. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/schema-blocks/css/schema-blocks.css
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,7 @@
.yoast-block-date-picker .components-datetime__time > fieldset:last-child {
display: none;
}

.yoast-schema-blocks-icon svg {
fill:none;
}
29 changes: 27 additions & 2 deletions packages/schema-blocks/src/core/blocks/BlockDefinition.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { createElement, Fragment, ReactElement } from "@wordpress/element";
import { registerBlockType, BlockConfiguration, BlockEditProps, BlockSaveProps } from "@wordpress/blocks";
import { InspectorControls } from "@wordpress/block-editor";
import {
registerBlockType,
BlockConfiguration,
BlockEditProps,
BlockSaveProps,
} from "@wordpress/blocks";
import { InspectorControls, BlockIcon } from "@wordpress/block-editor";
import BlockInstruction from "./BlockInstruction";
import Definition from "../Definition";
import BlockRootLeaf from "../../leaves/blocks/BlockRootLeaf";
Expand Down Expand Up @@ -87,6 +92,10 @@ export default class BlockDefinition extends Definition {

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

if ( configuration.icon && typeof configuration.icon === "string" && configuration.icon.startsWith( "<svg" ) ) {
configuration.icon = this.createBlockIcon( configuration );
}

// Register the block to WordPress.
registerBlockType( name, configuration );
// Register the block with our own code.
Expand All @@ -105,4 +114,20 @@ export default class BlockDefinition extends Definition {
.map( ( instruction, index ) => instruction.sidebar( props, index ) )
.filter( e => e !== null );
}

/**
* Creates an block icon.
*
* @param {MutableBlockConfiguration} configuration The block configuration.
*
* @returns {ReactElement[]} The sidebar element to render.
*/
private createBlockIcon( configuration: MutableBlockConfiguration ): ReactElement {
const icon = <span
className="yoast-schema-blocks-icon"
dangerouslySetInnerHTML={ { __html: configuration.icon as string } }
/>;

return <BlockIcon icon={ icon } />;
}
}