Skip to content

Commit

Permalink
Convert core toolbar buttons into ToolbarButton (#20008)
Browse files Browse the repository at this point in the history
  • Loading branch information
diegohaz committed May 7, 2020
1 parent 8eed967 commit b87d666
Show file tree
Hide file tree
Showing 39 changed files with 498 additions and 253 deletions.
52 changes: 26 additions & 26 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { find } from 'lodash';
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { Toolbar } from '@wordpress/components';
import { ToolbarGroup } from '@wordpress/components';
import { alignLeft, alignRight, alignCenter } from '@wordpress/icons';

const DEFAULT_ALIGNMENT_CONTROLS = [
Expand Down Expand Up @@ -58,7 +58,7 @@ export function AlignmentToolbar( props ) {
}

return (
<Toolbar
<ToolbarGroup
isCollapsed={ isCollapsed }
icon={ setIcon() }
label={ label }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`AlignmentToolbar should allow custom alignment controls to be specified 1`] = `
<Toolbar
<ToolbarGroup
controls={
Array [
Object {
Expand Down Expand Up @@ -58,7 +58,7 @@ exports[`AlignmentToolbar should allow custom alignment controls to be specified
`;

exports[`AlignmentToolbar should match snapshot 1`] = `
<Toolbar
<ToolbarGroup
controls={
Array [
Object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { Toolbar } from '@wordpress/components';
import { ToolbarGroup } from '@wordpress/components';
import { withSelect } from '@wordpress/data';
import { compose } from '@wordpress/compose';
import {
Expand Down Expand Up @@ -67,7 +67,7 @@ export function BlockAlignmentToolbar( {
BLOCK_ALIGNMENTS_CONTROLS[ DEFAULT_CONTROL ];

return (
<Toolbar
<ToolbarGroup
isCollapsed={ isCollapsed }
icon={
activeAlignmentControl
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`BlockAlignmentToolbar should match snapshot 1`] = `
<Toolbar
<ToolbarGroup
controls={
Array [
Object {
Expand Down
43 changes: 35 additions & 8 deletions packages/block-editor/src/components/block-controls/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
/**
* External dependencies
*/
import { isEmpty } from 'lodash';

/**
* WordPress dependencies
*/
import { createSlotFill, Toolbar } from '@wordpress/components';
import { useContext } from '@wordpress/element';
import {
__experimentalToolbarContext as ToolbarContext,
createSlotFill,
ToolbarGroup,
} from '@wordpress/components';

/**
* Internal dependencies
Expand All @@ -10,15 +20,32 @@ import { ifBlockEditSelected } from '../block-edit/context';

const { Fill, Slot } = createSlotFill( 'BlockControls' );

const BlockControlsFill = ( { controls, children } ) => (
<Fill>
<Toolbar controls={ controls } />
{ children }
</Fill>
);
function BlockControlsSlot( props ) {
const accessibleToolbarState = useContext( ToolbarContext );
return <Slot { ...props } fillProps={ accessibleToolbarState } />;
}

function BlockControlsFill( { controls, children } ) {
return (
<Fill>
{ ( fillProps ) => {
// Children passed to BlockControlsFill will not have access to any
// React Context whose Provider is part of the BlockControlsSlot tree.
// So we re-create the Provider in this subtree.
const value = ! isEmpty( fillProps ) ? fillProps : null;
return (
<ToolbarContext.Provider value={ value }>
<ToolbarGroup controls={ controls } />
{ children }
</ToolbarContext.Provider>
);
} }
</Fill>
);
}

const BlockControls = ifBlockEditSelected( BlockControlsFill );

BlockControls.Slot = Slot;
BlockControls.Slot = BlockControlsSlot;

export default BlockControls;
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
/**
* External dependencies
*/
import { isEmpty } from 'lodash';

/**
* WordPress dependencies
*/
import { createSlotFill } from '@wordpress/components';
import { useContext } from '@wordpress/element';
import {
__experimentalToolbarContext as ToolbarContext,
createSlotFill,
} from '@wordpress/components';

/**
* Internal dependencies
Expand All @@ -10,8 +19,28 @@ import { ifBlockEditSelected } from '../block-edit/context';

const { Fill, Slot } = createSlotFill( 'BlockFormatControls' );

const BlockFormatControls = ifBlockEditSelected( Fill );
function BlockFormatControlsSlot( props ) {
const accessibleToolbarState = useContext( ToolbarContext );
return <Slot { ...props } fillProps={ accessibleToolbarState } />;
}

function BlockFormatControlsFill( props ) {
return (
<Fill>
{ ( fillProps ) => {
const value = ! isEmpty( fillProps ) ? fillProps : null;
return (
<ToolbarContext.Provider value={ value }>
{ props.children }
</ToolbarContext.Provider>
);
} }
</Fill>
);
}

const BlockFormatControls = ifBlockEditSelected( BlockFormatControlsFill );

BlockFormatControls.Slot = Slot;
BlockFormatControls.Slot = BlockFormatControlsSlot;

export default BlockFormatControls;
1 change: 1 addition & 0 deletions packages/block-editor/src/components/block-list/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,7 @@
border-radius: $radius-block-ui;
background-color: $white;

.block-editor-block-toolbar .components-toolbar-group,
.block-editor-block-toolbar .components-toolbar {
border-right-color: $dark-gray-primary;
}
Expand Down
17 changes: 11 additions & 6 deletions packages/block-editor/src/components/block-mover/button.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* External dependencies
*/
import classnames from 'classnames';
import { castArray, first, last, partial } from 'lodash';
import { castArray, first, last } from 'lodash';

/**
* WordPress dependencies
Expand Down Expand Up @@ -121,9 +121,14 @@ const BlockMoverButton = forwardRef(
);
const moverFunction =
direction === 'up' ? moveBlocksUp : moveBlocksDown;
const onClick = isDisabled
? null
: partial( moverFunction, clientIds, rootClientId );

const onClick = ( event ) => {
moverFunction( clientIds, rootClientId );
if ( props.onClick ) {
props.onClick( event );
}
};

const descriptionId = `block-editor-block-mover-button__description-${ instanceId }`;

return (
Expand All @@ -141,9 +146,9 @@ const BlockMoverButton = forwardRef(
isRTL
) }
aria-describedby={ descriptionId }
onClick={ onClick }
aria-disabled={ isDisabled }
{ ...props }
onClick={ isDisabled ? null : onClick }
aria-disabled={ isDisabled }
/>
<span
id={ descriptionId }
Expand Down
29 changes: 22 additions & 7 deletions packages/block-editor/src/components/block-mover/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ import classnames from 'classnames';
/**
* WordPress dependencies
*/
import { ToolbarGroup } from '@wordpress/components';
import {
ToolbarGroup,
__experimentalToolbarItem as ToolbarItem,
} from '@wordpress/components';
import { getBlockType } from '@wordpress/blocks';
import { Component } from '@wordpress/element';
import { withSelect } from '@wordpress/data';
Expand Down Expand Up @@ -73,16 +76,28 @@ export class BlockMover extends Component {
onDragEnd={ onDraggableEnd }
>
<ToolbarGroup>
<BlockMoverUpButton
clientIds={ clientIds }
<ToolbarItem
onFocus={ this.onFocus }
onBlur={ this.onBlur }
/>
<BlockMoverDownButton
clientIds={ clientIds }
>
{ ( itemProps ) => (
<BlockMoverUpButton
clientIds={ clientIds }
{ ...itemProps }
/>
) }
</ToolbarItem>
<ToolbarItem
onFocus={ this.onFocus }
onBlur={ this.onBlur }
/>
>
{ ( itemProps ) => (
<BlockMoverDownButton
clientIds={ clientIds }
{ ...itemProps }
/>
) }
</ToolbarItem>
</ToolbarGroup>
</div>
) }
Expand Down
Loading

0 comments on commit b87d666

Please sign in to comment.