Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Block warning: add ellipsis menu and convert to classic block #7741

Merged
merged 7 commits into from
Aug 28, 2018
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions packages/editor/src/components/block-list/block-invalid-warning.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ import { withDispatch } from '@wordpress/data';
*/
import Warning from '../warning';

function BlockInvalidWarning( { convertToHTML, convertToBlocks } ) {
function BlockInvalidWarning( { convertToHTML, convertToBlocks, convertToClassic } ) {
const hasHTMLBlock = !! getBlockType( 'core/html' );

return (
<Warning
actions={ [
primaryActions={ [
<Button key="convert" onClick={ convertToBlocks } isLarge isPrimary={ ! hasHTMLBlock }>
{ __( 'Convert to Blocks' ) }
</Button>,
Expand All @@ -30,6 +30,10 @@ function BlockInvalidWarning( { convertToHTML, convertToBlocks } ) {
</Button>
),
] }
hiddenActions={ [
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this be secondaryActions (to match the "primaryActions" prop)?

{ title: __( 'Convert to Blocks' ), onClick: convertToBlocks },
{ title: __( 'Convert to Classic Block' ), onClick: convertToClassic },
] }
>
{ __( 'This block has been modified externally.' ) }
</Warning>
Expand All @@ -39,6 +43,11 @@ function BlockInvalidWarning( { convertToHTML, convertToBlocks } ) {
export default withDispatch( ( dispatch, { block } ) => {
const { replaceBlock } = dispatch( 'core/editor' );
return {
convertToClassic() {
replaceBlock( block.uid, createBlock( 'core/freeform', {
content: block.originalContent,
} ) );
},
convertToHTML() {
replaceBlock( block.clientId, createBlock( 'core/html', {
content: block.originalContent,
Expand Down
36 changes: 33 additions & 3 deletions packages/editor/src/components/warning/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,53 @@ import classnames from 'classnames';
* WordPress dependencies
*/
import { Children } from '@wordpress/element';
import { Dropdown, IconButton, MenuGroup, MenuItem } from '@wordpress/components';
import { __ } from '@wordpress/i18n';

function Warning( { className, actions, children } ) {
function Warning( { className, primaryActions, children, hiddenActions } ) {
return (
<div className={ classnames( className, 'editor-warning' ) }>
<div className="editor-warning__contents">
<p className="editor-warning__message">{ children }</p>

{ Children.count( actions ) > 0 && (
{ Children.count( primaryActions ) > 0 && (
<div className="editor-warning__actions">
{ Children.map( actions, ( action, i ) => (
{ Children.map( primaryActions, ( action, i ) => (
<span key={ i } className="editor-warning__action">
{ action }
</span>
) ) }
</div>
) }
</div>

{ hiddenActions && (
<div className="editor-warning__hidden">
<Dropdown
className="edit-post-more-menu"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The edit-post-* className seems useless right? (they don't follow our guidelines neither)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's the same class used in https://github.com/WordPress/gutenberg/blob/master/edit-post/components/header/more-menu/index.js, making the ellipsis vertical rather than horizontal. I can file a separate issue if that also needs changing to match the guidelines?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we should rely on classnames styled in other components. We should aim to reuse components instead of classnames instead.

So two options:

  • I'm fine duplicating the styles (if not too much) and using the right classnames here editor-warning__*
  • Creating a generic component with these styles but in that case, this component should be meaningful. Maybe it could be a prop of Dropdown instead?

position="bottom left"
renderToggle={ ( { isOpen, onToggle } ) => (
<IconButton
icon="ellipsis"
label={ __( 'More options' ) }
onClick={ onToggle }
aria-expanded={ isOpen }
/>
) }
renderContent={ () => (
<div className="edit-post-more-menu__content">
<MenuGroup label={ __( 'More options' ) }>
{ hiddenActions.map( ( item, pos ) =>
<MenuItem onClick={ item.onClick } key={ pos }>
{ item.title }
</MenuItem>
) }
</MenuGroup>
</div>
) }
/>
</div>
) }
</div>
);
}
Expand Down
4 changes: 4 additions & 0 deletions packages/editor/src/components/warning/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,8 @@
margin: 0 6px 0 0;
margin-left: 0;
}

.editor-warning__hidden {
margin-top: 3px;
}
}
13 changes: 11 additions & 2 deletions packages/editor/src/components/warning/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,16 @@ describe( 'Warning', () => {
expect( wrapper ).toMatchSnapshot();
} );

it( 'should has valid class', () => {
it( 'should have valid class', () => {
const wrapper = shallow( <Warning /> );

expect( wrapper.hasClass( 'editor-warning' ) ).toBe( true );
expect( wrapper.find( '.editor-warning__actions' ) ).toHaveLength( 0 );
expect( wrapper.find( '.editor-warning__hidden' ) ).toHaveLength( 0 );
} );

it( 'should show child error message element', () => {
const wrapper = shallow( <Warning actions={ <button /> }>Message</Warning> );
const wrapper = shallow( <Warning primaryActions={ <button /> }>Message</Warning> );

const actions = wrapper.find( '.editor-warning__actions' );
const action = actions.childAt( 0 );
Expand All @@ -32,4 +33,12 @@ describe( 'Warning', () => {
expect( action.hasClass( 'editor-warning__action' ) ).toBe( true );
expect( action.childAt( 0 ).type() ).toBe( 'button' );
} );

it( 'should show hidden actions', () => {
const wrapper = shallow( <Warning hiddenActions={ [ { title: 'test', onClick: null } ] }>Message</Warning> );

const actions = wrapper.find( '.editor-warning__hidden' );

expect( actions ).toHaveLength( 1 );
} );
} );