I am trying to use UnitControl in my custom Gutenberg block to define the line-height of text. I receive the "This block has encountered an error and cannot be previewed" when I try to access the block in the WordPress editor. I am unsure what is causing this error. I've even tried removing references to the UnitControl in the return and save such as lineHeight:headlineSpacing
to see if it would stop flagging the error. It still gives me the error so I am unsure if I am using UnitControl correctly at all or if perhaps I've made a mistake in the <UnitControl>
element.
CODE:
const { registerBlockType } = wp.blocks;
const {
RichText,
InspectorControls,
ColorPalette,
MediaUpload,
MediaUploadCheck,
Button,
RawHTML,
FontSizePicker,
__experimentalUnitControl,
InnerBlocks
} = wp.blockEditor;
const { PanelBody, IconButton } = wp.components;
registerBlockType('mycustomsite/small-jumbo', {
//Built-in Attributes
title: 'Small Jumbo',
description: 'Block to generate a smaller jumbotron',
icon: 'align-full-width',
category: 'design',
//Custom Attributes
attributes: {
title: {
type: 'string',
source: 'html',
selector: 'h1'
}
},
//Built-in Functions
edit({attributes, setAttributes}) {
const{
title,
headlineSize,
headlineSpacing,
} = attributes;
const headlineSizes = [
{
name: '14px',
slug: 'fourteen',
size: 14
},
{
name: '16px',
slug: 'sixteen',
size: 16
},
{
name: '18px',
slug: 'eighteen',
size: 18
},
{
name: '24px',
slug: 'twentyfour',
size: 24
}
];
//Custom Functions
function onChangeTitle(newTitle) {
setAttributes( { title: newTitle } );
}
function onChangeHeadlineSize( newHeadlineSize ) {
setAttributes( { headlineSize: newHeadlineSize } );
}
function onChangeHeadlineSpacing( newHeadlineSpacing) {
setAttributes( { headlineSize: newHeadlineSpacing } );
}
return ([
<InspectorControls style={ { marginBottom: '40px' } }>
<p><strong>Customize Top Text</strong></p>
<PanelBody title={ 'Headline Size' }>
<FontSizePicker
fontSizes={ headlineSizes }
value={ headlineSize }
onChange={ onChangeHeadlineSize }
/>
</PanelBody>
<PanelBody title={ 'Line Height' }>
<UnitControl
label="Line Height"
isUnitSelectTabbable
value={ headlineSpacing }
onChange={ onChangeHeadlineSpacing }
/>
</PanelBody>
</InspectorControls>,
<div>
<RichText
key="editable"
tagName="h1"
placeholder="H1 Title"
value= { title }
onChange= { onChangeTitle }
style= { { fontSize:headlineSize, lineHeight:headlineSpacing} }
/>
</div>
]);
},
save({ attributes }) {
const {
title,
headlineSize,
headlineSpacing,
} = attributes;
return (
<div>
<RichText.Content style={ {fontSize:headlineSize, lineHeight:headlineSpacing } } tagName="h1" value={title} />
</div>
);
}
});