add toggle control for inline

This commit is contained in:
Ray Elliott 2020-08-14 11:43:04 +01:00
parent ffc0aa7291
commit 8645088119
1 changed files with 59 additions and 17 deletions

View File

@ -3,9 +3,15 @@ import './editor.scss';
import { registerBlockType } from '@wordpress/blocks'; import { registerBlockType } from '@wordpress/blocks';
import { Fragment } from '@wordpress/element'; import { RawHTML, Fragment } from '@wordpress/element';
import { InspectorControls } from '@wordpress/editor'; import { InspectorControls } from '@wordpress/editor';
import { TextControl, Panel, PanelRow } from '@wordpress/components'; import {
Button,
TextControl,
ToggleControl,
Panel,
PanelRow,
} from '@wordpress/components';
import { __ } from '@wordpress/i18n'; import { __ } from '@wordpress/i18n';
registerBlockType('room-vw/iconify', { registerBlockType('room-vw/iconify', {
@ -21,15 +27,25 @@ registerBlockType('room-vw/iconify', {
attributes: { attributes: {
prefix: { prefix: {
type: 'string', type: 'string',
source: 'attribute',
selector: 'data-prefix',
default: 'noto-v1', default: 'noto-v1',
}, },
name: { name: {
type: 'string', type: 'string',
source: 'attribute',
selector: 'data-name',
default: 'unicorn', default: 'unicorn',
}, },
inline: { inline: {
type: 'boolean', type: 'boolean',
default: true, default: false,
},
svg: {
type: 'string',
source: 'html',
selector: '.icon',
default: '<span>Select an icon</span>',
}, },
}, },
@ -54,27 +70,53 @@ registerBlockType('room-vw/iconify', {
}} }}
/> />
</PanelRow> </PanelRow>
<PanelRow>
<ToggleControl
label="Is inline"
checked={attributes.inline}
onChange={(state) => {
setAttributes({ inline: state });
}}
/>
<Button
isBusy={false}
disabled={false}
isSecondary
onClick={() => {
setAttributes({
svg: Iconify.getSVG(
`${attributes.prefix}:${attributes.name}`
),
});
}}
>
Get Icon
</Button>
</PanelRow>
</Panel> </Panel>
</InspectorControls> </InspectorControls>
<div> <RawHTML
<span className={
className={className + ' iconify icon'} className +
data-icon={attributes.prefix + ':' + attributes.name} (attributes.inline ? ' is-inline icon' : ' icon')
data-inline={attributes.inline} }
></span> >
<span>{attributes.prefix + ':' + attributes.name}</span> {attributes.svg}
</div> </RawHTML>
</Fragment> </Fragment>
); );
}, },
save({ className, attributes }) { save({ attributes }) {
return ( return (
<span <RawHTML
className={className + ' iconify icon'} className={attributes.inline ? ' is-inline icon' : ' icon'}
data-icon={attributes.prefix + ':' + attributes.name} dataPrefix={attributes.prefix}
data-inline={attributes.inline} dataName={attributes.name}
></span> dataInline={attributes.inline}
>
{attributes.svg}
</RawHTML>
); );
}, },
}); });