Skip to content

Commit

Permalink
Merge pull request #830 from WordPress/try/block-controls-fill
Browse files Browse the repository at this point in the history
Try: Block controls rendering by block's `edit`
  • Loading branch information
aduth committed May 26, 2017
2 parents 11d2651 + 8c42fdd commit 91c208e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 15 deletions.
17 changes: 17 additions & 0 deletions blocks/block-controls/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* External dependencies
*/
import { Fill } from 'react-slot-fill';

/**
* WordPress dependencies
*/
import Toolbar from 'components/toolbar';

export default function BlockControls( { controls } ) {
return (
<Fill name="Formatting.Toolbar">
<Toolbar controls={ controls } />
</Fill>
);
}
34 changes: 19 additions & 15 deletions blocks/library/heading/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { isString } from 'lodash';
import './style.scss';
import { registerBlock, createBlock, query } from '../../api';
import Editable from '../../editable';
import BlockControls from '../../block-controls';

const { children, prop } = query;

Expand All @@ -24,18 +25,6 @@ registerBlock( 'core/heading', {
nodeName: prop( 'h1,h2,h3,h4,h5,h6', 'nodeName' ),
},

controls: [
...'123456'.split( '' ).map( ( level ) => ( {
icon: 'heading',
title: wp.i18n.sprintf( wp.i18n.__( 'Heading %s' ), level ),
isActive: ( { nodeName } ) => 'H' + level === nodeName,
onClick( attributes, setAttributes ) {
setAttributes( { nodeName: 'H' + level } );
},
subscript: level,
} ) ),
],

transforms: {
from: [
{
Expand Down Expand Up @@ -90,17 +79,32 @@ registerBlock( 'core/heading', {
edit( { attributes, setAttributes, focus, setFocus, mergeBlocks } ) {
const { content, nodeName = 'H2' } = attributes;

return (
return [
focus && (
<BlockControls
key="controls"
controls={
'123456'.split( '' ).map( ( level ) => ( {
icon: 'heading',
title: wp.i18n.sprintf( wp.i18n.__( 'Heading %s' ), level ),
isActive: 'H' + level === nodeName,
onClick: () => setAttributes( { nodeName: 'H' + level } ),
subscript: level,
} ) )
}
/>
),
<Editable
key="editable"
tagName={ nodeName.toLowerCase() }
value={ content }
focus={ focus }
onFocus={ setFocus }
onChange={ ( value ) => setAttributes( { content: value } ) }
onMerge={ mergeBlocks }
inline
/>
);
/>,
];
},

save( { attributes } ) {
Expand Down

0 comments on commit 91c208e

Please sign in to comment.