Skip to content

Commit

Permalink
Merge branch 'master' into fix/3839-undelayed-slot-prop-update
Browse files Browse the repository at this point in the history
  • Loading branch information
tiny-james committed Jan 9, 2018
2 parents ff4f76a + b4fc426 commit 4520961
Show file tree
Hide file tree
Showing 17 changed files with 104 additions and 43 deletions.
2 changes: 1 addition & 1 deletion blocks/alignment-toolbar/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export default function AlignmentToolbar( { value, onChange } ) {
return {
...control,
isActive,
onClick: () => onChange( isActive ? null : align ),
onClick: () => onChange( isActive ? undefined : align ),
};
} ) }
/>
Expand Down
4 changes: 2 additions & 2 deletions blocks/alignment-toolbar/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ describe( 'AlignmentToolbar', () => {
expect( wrapper ).toMatchSnapshot();
} );

test( 'should call on change with null when a control is already active', () => {
test( 'should call on change with undefined when a control is already active', () => {
const activeControl = controls.find( ( { isActive } ) => isActive );
activeControl.onClick();

expect( activeControl.align ).toBe( alignment );
expect( onChangeSpy ).toHaveBeenCalledTimes( 1 );
expect( onChangeSpy ).toHaveBeenCalledWith( null );
expect( onChangeSpy ).toHaveBeenCalledWith( undefined );
} );

test( 'should call on change a new value when the control is not active', () => {
Expand Down
11 changes: 5 additions & 6 deletions blocks/library/audio/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { Button, Dashicon, Placeholder, Toolbar } from '@wordpress/components';
import { Button, IconButton, Placeholder, Toolbar } from '@wordpress/components';
import { Component } from '@wordpress/element';

/**
Expand Down Expand Up @@ -99,13 +99,12 @@ registerBlockType( 'core/audio', {
onChange={ updateAlignment }
/>
<Toolbar>
<Button
<IconButton
className="components-icon-button components-toolbar__control"
aria-label={ __( 'Edit audio' ) }
label={ __( 'Edit audio' ) }
onClick={ switchToEditing }
>
<Dashicon icon="edit" />
</Button>
icon="edit"
/>
</Toolbar>
</BlockControls>
);
Expand Down
4 changes: 3 additions & 1 deletion blocks/library/cover-image/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ registerBlockType( 'core/cover-image', {
}
);

const editButtonLabel = __( 'Edit image' );
const controls = focus && [
<BlockControls key="controls">
<BlockAlignmentToolbar
Expand All @@ -114,11 +115,12 @@ registerBlockType( 'core/cover-image', {
<MediaUploadButton
buttonProps={ {
className: 'components-icon-button components-toolbar__control',
'aria-label': __( 'Edit image' ),
'aria-label': editButtonLabel,
} }
onSelect={ onSelectImage }
type="image"
value={ id }
tooltip={ editButtonLabel }
>
<Dashicon icon="edit" />
</MediaUploadButton>
Expand Down
24 changes: 21 additions & 3 deletions blocks/library/freeform/editor.scss
Original file line number Diff line number Diff line change
Expand Up @@ -234,10 +234,28 @@
}
}

.mce-tooltip {
display: none;
.components-toolbar__control .dashicon {
display: block;
}

.components-toolbar__control .dashicon {

.mce-widget.mce-tooltip {
display: block;
opacity: initial;

.mce-tooltip-inner {
padding: 4px 12px;
background: $dark-gray-400;
border-width: 0;
color: white;
white-space: nowrap;
box-shadow: none;
font-size: $default-font-size;
border-radius: 0;
}

.mce-tooltip-arrow {
border-top-color: $dark-gray-400;
border-bottom-color: $dark-gray-400;
}
}
13 changes: 12 additions & 1 deletion blocks/library/gallery/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,15 @@ class GalleryBlock extends Component {
);
}

componentWillReceiveProps( nextProps ) {
// Deselect images when losing focus
if ( ! nextProps.focus && this.props.focus ) {
this.setState( {
selectedImage: null,
} );
}
}

render() {
const { attributes, focus, className } = this.props;
const { images, columns = defaultColumnsNumber( attributes ), align, imageCrop, linkTo } = attributes;
Expand All @@ -137,6 +146,7 @@ class GalleryBlock extends Component {
/>
);

const editButtonLabel = __( 'Edit Gallery' );
const controls = (
focus && (
<BlockControls key="controls">
Expand All @@ -149,13 +159,14 @@ class GalleryBlock extends Component {
<MediaUploadButton
buttonProps={ {
className: 'components-icon-button components-toolbar__control',
'aria-label': __( 'Edit Gallery' ),
'aria-label': editButtonLabel,
} }
onSelect={ this.onSelectImages }
type="image"
multiple
gallery
value={ images.map( ( img ) => img.id ) }
tooltip={ editButtonLabel }
>
<Dashicon icon="edit" />
</MediaUploadButton>
Expand Down
4 changes: 3 additions & 1 deletion blocks/library/image/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ class ImageBlock extends Component {
const uploadFromFiles = ( event ) => mediaUpload( event.target.files, setAttributes );
const dropFiles = ( files ) => mediaUpload( files, setAttributes );

const editButtonLabel = __( 'Edit image' );
const controls = (
focus && (
<BlockControls key="controls">
Expand All @@ -133,11 +134,12 @@ class ImageBlock extends Component {
<MediaUploadButton
buttonProps={ {
className: 'components-icon-button components-toolbar__control',
'aria-label': __( 'Edit image' ),
'aria-label': editButtonLabel,
} }
onSelect={ this.onSelectImage }
type="image"
value={ id }
tooltip={ editButtonLabel }
>
<Dashicon icon="edit" />
</MediaUploadButton>
Expand Down
12 changes: 6 additions & 6 deletions blocks/library/latest-posts/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class LatestPostsBlock extends Component {
value={ columns }
onChange={ ( value ) => setAttributes( { columns: value } ) }
min={ 2 }
max={ Math.min( MAX_POSTS_COLUMNS, latestPosts.length ) }
max={ ! hasPosts ? MAX_POSTS_COLUMNS : Math.min( MAX_POSTS_COLUMNS, latestPosts.length ) }
/>
}
</InspectorControls>
Expand All @@ -93,10 +93,9 @@ class LatestPostsBlock extends Component {
}

// Removing posts from display should be instant.
const postsDifference = latestPosts.length - postsToShow;
if ( postsDifference > 0 ) {
latestPosts.splice( postsToShow, postsDifference );
}
const displayPosts = latestPosts.length > postsToShow ?
latestPosts.slice( 0, postsToShow ) :
latestPosts;

const layoutControls = [
{
Expand Down Expand Up @@ -133,7 +132,7 @@ class LatestPostsBlock extends Component {
} ) }
key="latest-posts"
>
{ latestPosts.map( ( post, i ) =>
{ displayPosts.map( ( post, i ) =>
<li key={ i }>
<a href={ post.link } target="_blank">{ decodeEntities( post.title.rendered.trim() ) || __( '(Untitled)' ) }</a>
{ displayPostDate && post.date_gmt &&
Expand All @@ -155,6 +154,7 @@ export default withAPIData( ( props ) => {
order,
orderBy,
per_page: postsToShow,
_fields: [ 'date_gmt', 'link', 'title' ],
}, value => ! isUndefined( value ) ) );
return {
latestPosts: `/wp/v2/posts?${ queryString }`,
Expand Down
11 changes: 5 additions & 6 deletions blocks/library/video/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { Placeholder, Toolbar, Dashicon, Button } from '@wordpress/components';
import { Placeholder, Toolbar, IconButton, Button } from '@wordpress/components';
import { Component } from '@wordpress/element';

/**
Expand Down Expand Up @@ -100,13 +100,12 @@ registerBlockType( 'core/video', {
onChange={ updateAlignment }
/>
<Toolbar>
<Button
<IconButton
className="components-icon-button components-toolbar__control"
aria-label={ __( 'Edit video' ) }
label={ __( 'Edit video' ) }
onClick={ switchToEditing }
>
<Dashicon icon="edit" />
</Button>
icon="edit"
/>
</Toolbar>
</BlockControls>
);
Expand Down
12 changes: 9 additions & 3 deletions blocks/media-upload-button/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/
import { Component } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import { Button } from '@wordpress/components';
import { Button, Tooltip } from '@wordpress/components';
import { pick } from 'lodash';

// Getter for the sake of unit tests.
Expand Down Expand Up @@ -149,13 +149,19 @@ class MediaUploadButton extends Component {
}

render() {
const { children, buttonProps } = this.props;
const { children, buttonProps, tooltip } = this.props;

return (
let element = (
<Button onClick={ this.openModal } { ...buttonProps }>
{ children }
</Button>
);

if ( tooltip ) {
element = <Tooltip text={ tooltip }>{ element }</Tooltip>;
}

return element;
}
}

Expand Down
1 change: 1 addition & 0 deletions components/dropdown-menu/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ function DropdownMenu( {
aria-haspopup="true"
aria-expanded={ isOpen }
label={ label }
tooltip={ label }
>
<Dashicon icon="arrow-down" />
</IconButton>
Expand Down
16 changes: 13 additions & 3 deletions components/icon-button/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* External dependencies
*/
import classnames from 'classnames';
import { isString } from 'lodash';
import { isString, isArray } from 'lodash';

/**
* WordPress dependencies
Expand All @@ -23,6 +23,16 @@ class IconButton extends Component {
render() {
const { icon, children, label, className, tooltip, focus, ...additionalProps } = this.props;
const classes = classnames( 'components-icon-button', className );
const tooltipText = tooltip || label;

// Should show the tooltip if an explicit tooltip is passed
// or if there's a label and the children are empty and the tooltip is not explicitely disabled
const showTooltip = !! tooltip ||
(
label &&
( ! children || ( isArray( children ) && ! children.length ) ) &&
false !== tooltip
);

let element = (
<Button { ...additionalProps } aria-label={ label } className={ classes } focus={ focus }>
Expand All @@ -31,8 +41,8 @@ class IconButton extends Component {
</Button>
);

if ( label && ! children && false !== tooltip ) {
element = <Tooltip text={ tooltip || label }>{ element }</Tooltip>;
if ( showTooltip ) {
element = <Tooltip text={ tooltipText }>{ element }</Tooltip>;
}

return element;
Expand Down
6 changes: 6 additions & 0 deletions components/icon-button/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,11 @@ describe( 'IconButton', () => {
expect( iconButton.name() ).toBe( 'Button' );
expect( iconButton.prop( 'aria-label' ) ).toBe( 'WordPress' );
} );

it( 'should show the tooltip for empty children', () => {
const iconButton = shallow( <IconButton label="WordPress" children={ [] } /> );
expect( iconButton.name() ).toBe( 'Tooltip' );
expect( iconButton.prop( 'text' ) ).toBe( 'WordPress' );
} );
} );
} );
17 changes: 12 additions & 5 deletions docs/blocks-controls.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ registerBlockType( 'gutenberg-boilerplate-es5/hello-world-step-04', {
type: 'array',
source: 'children',
selector: 'p',
}
},
alignment: {
type: 'string',
},
},

edit: function( props ) {
Expand Down Expand Up @@ -76,9 +79,10 @@ registerBlockType( 'gutenberg-boilerplate-es5/hello-world-step-04', {
},

save: function( props ) {
var content = props.attributes.content;
var content = props.attributes.content,
alignment = props.attributes.alignment;

return el( 'p', { className: props.className }, content );
return el( 'p', { className: props.className, style: { textAlign: alignment } }, content );
},
} );
```
Expand All @@ -105,6 +109,9 @@ registerBlockType( 'gutenberg-boilerplate-esnext/hello-world-step-04', {
source: 'children',
selector: 'p',
},
alignment: {
type: 'string',
},
},

edit( { attributes, className, focus, setAttributes, setFocus } ) {
Expand Down Expand Up @@ -141,9 +148,9 @@ registerBlockType( 'gutenberg-boilerplate-esnext/hello-world-step-04', {
},

save( { attributes, className } ) {
const { content } = attributes;
const { content, alignment } = attributes;

return <p className={ className }>{ content }</p>;
return <p className={ className } style={ textAlign: alignment }>{ content }</p>;
},
} );
```
Expand Down
4 changes: 3 additions & 1 deletion editor/components/block-switcher/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ function BlockSwitcher( { blocks, onTransform, isLocked } ) {
onToggle();
}
};
const label = __( 'Change block type' );

return (
<Toolbar>
Expand All @@ -55,7 +56,8 @@ function BlockSwitcher( { blocks, onTransform, isLocked } ) {
onClick={ onToggle }
aria-haspopup="true"
aria-expanded={ isOpen }
label={ __( 'Change block type' ) }
label={ label }
tooltip={ label }
onKeyDown={ openOnArrowDown }
>
<Dashicon icon="arrow-down" />
Expand Down
4 changes: 2 additions & 2 deletions editor/edit-post/sidebar/header.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ const SidebarHeader = ( { panel, onSetPanel, onToggleSidebar, count } ) => {
<button
onClick={ () => onSetPanel( 'block' ) }
className={ `editor-sidebar__panel-tab ${ panel === 'block' ? 'is-active' : '' }` }
aria-label={ __( 'Block settings' ) }
aria-label={ __( 'Format settings' ) }
>
{ sprintf( _n( 'Block', '%d Blocks', count ), count ) }
{ sprintf( _n( 'Format', 'Format (%d)', count ), count ) }
</button>
<IconButton
onClick={ closeSidebar }
Expand Down
Loading

0 comments on commit 4520961

Please sign in to comment.