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

Update/block mover accessibility #949

Closed
Show file tree
Hide file tree
Changes from 14 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
21 changes: 21 additions & 0 deletions editor/block-mover/generate-order-title.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
export default function( { typeTitle, position, isFirst, isLast, dir } ) {
const prefix = 'Move "' + typeTitle + '" block from position ' + position;
const errorPrefix = 'Block "' + typeTitle + '"';
let title = '';

if ( isFirst && isLast ) {
title = errorPrefix + ' is the only block, and cannot be moved';
} else if ( dir > 0 ) {
// moving down
title = ( ! isLast ) ?
prefix + ' down to position ' + ( position + 1 ) :
errorPrefix + ' is at the end of the content and can’t be moved down';
} else {
// moving up
title = ( ! isFirst ) ?
prefix + ' up to position ' + ( position - 1 ) :
errorPrefix + ' is at the beginning of the content and can’t be moved up';
}

return title;
Copy link
Member

@dmsnell dmsnell May 30, 2017

Choose a reason for hiding this comment

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

Have you considered using an early-abort style instead of doing more nesting and mutation of title?

if ( isFirst && isLast ) {
	return `${ errorPrefix } is the only block and cannot be moved`;
}

// moving down and last block
if ( dir > 0  && isLast ) {
	return `${ errorPrefix } is at the beginning of the content and can't be moved up`;
}

// moving down and not first block
if ( dir > 0 && ! isLast ) {
	return `${ prefix } down to position ${ position + 1 }`;
}

// moving up and first block
if ( dir < 0 && isFirst ) {
	return `${ errorPrefix } … `
}

// moving up and not first block
if ( dir < 0 && ! isFirst ) {
	return `${ prefix }…`;
}

// no other valid options, return empty
return '';

}
12 changes: 10 additions & 2 deletions editor/block-mover/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,32 @@ import { IconButton } from 'components';
* Internal dependencies
*/
import './style.scss';
import { isFirstBlock, isLastBlock } from '../selectors';
import { isFirstBlock, isLastBlock, getBlock } from '../selectors';
import { getBlockSettings } from '../../blocks/api/registration';
import generateOrderTitle from './generate-order-title';

function BlockMover( { onMoveUp, onMoveDown, isFirst, isLast } ) {
function BlockMover( { onMoveUp, onMoveDown, isFirst, isLast, block, order } ) {
// We emulate a disabled state because forcefully applying the `disabled`
// attribute on the button while it has focus causes the screen to change
// to an unfocused state (body as active element) without firing blur on,
// the rendering parent, leaving it unable to react to focus out.
const type = getBlockSettings( block.blockType ),
typeTitle = type.title,
position = ( order + 1 );

return (
<div className="editor-block-mover">
<IconButton
className="editor-block-mover__control"
onClick={ isFirst ? null : onMoveUp }
label={ generateOrderTitle( { typeTitle, position, isFirst, isLast, dir: -1 } ) }
icon="arrow-up-alt2"
aria-disabled={ isFirst }
/>
<IconButton
className="editor-block-mover__control"
onClick={ isLast ? null : onMoveDown }
label={ generateOrderTitle( { typeTitle, position, isFirst, isLast, dir: 1 } ) }
icon="arrow-down-alt2"
aria-disabled={ isLast }
/>
Expand All @@ -43,6 +50,7 @@ export default connect(
( state, ownProps ) => ( {
isFirst: isFirstBlock( state, first( ownProps.uids ) ),
isLast: isLastBlock( state, last( ownProps.uids ) ),
block: getBlock( state, ownProps.uid ),
} ),
( dispatch, ownProps ) => ( {
onMoveDown() {
Expand Down
4 changes: 2 additions & 2 deletions editor/block-mover/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@
border: none;
outline: none;
background: none;
color: $dark-gray-100;
color: $dark-gray-300;
cursor: pointer;
width: 20px;
height: 20px;

&:hover {
color: $dark-gray-900;
color: $dark-gray-500;
}

&[aria-disabled="true"] {
Expand Down
75 changes: 75 additions & 0 deletions editor/block-mover/test/generate-order-title.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/**
* External dependencies
*/
import { expect } from 'chai';

/**
* Internal dependencies
*/
import generateOrderTitle from '../generate-order-title';

describe( 'block mover', () => {
describe( 'generateOrderTitle', () => {
const typeTitle = 'TestType';

it( 'Should generate a title for the first item moving up', () => {
expect( generateOrderTitle( {
typeTitle,
position: 1,
isFirst: true,
isLast: false,
dir: -1,
} ) ).to.equal(
'Block "' + typeTitle + '" is at the beginning of the content and can’t be moved up'
);
} );

it( 'Should generate a title for the last item moving down', () => {
expect( generateOrderTitle( {
typeTitle,
position: 1,
isFirst: false,
isLast: true,
dir: 1,
} ) ).to.equal(
'Block "' + typeTitle + '" is at the end of the content and can’t be moved down'
);
} );

it( 'Should generate a title for the second item moving up', () => {
expect( generateOrderTitle( {
typeTitle,
position: 2,
isFirst: false,
isLast: false,
dir: -1,
} ) ).to.equal(
'Move "' + typeTitle + '" block from position 2 up to position 1'
);
} );

it( 'Should generate a title for the second item moving down', () => {
expect( generateOrderTitle( {
typeTitle,
position: 2,
isFirst: false,
isLast: false,
dir: 1,
} ) ).to.equal(
'Move "' + typeTitle + '" block from position 2 down to position 3'
);
} );

it( 'Should generate a title for the only item in the list', () => {
expect( generateOrderTitle( {
typeTitle,
position: 1,
isFirst: true,
isLast: true,
dir: 1,
} ) ).to.equal(
'Block "' + typeTitle + '" is the only block, and cannot be moved'
);
} );
} );
} );
2 changes: 1 addition & 1 deletion editor/modes/visual-editor/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ class VisualEditorBlock extends wp.element.Component {
tabIndex="0"
{ ...wrapperProps }
>
{ ( showUI || isHovered ) && <BlockMover uids={ [ block.uid ] } /> }
{ ( showUI || isHovered ) && <BlockMover uid={ block.uid } order={ this.props.order } /> }
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we can't make this change because the block mover is also used with several blocks (multi-selection). See the second usage in this exact same file

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, thank you for spotting and apologies for not seeing it myself! I'll look into amending this tonight and allowing for the case of multiple blocks. Likely the string will just have to read something like "Move n blocks {down|up} by one place".

{ showUI &&
<CSSTransitionGroup
transitionName={ { appear: 'is-appearing', appearActive: 'is-appearing-active' } }
Expand Down