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

Writing Flow: Fix focus/keyboard issues with multi-select #3297

Merged
merged 5 commits into from
Nov 2, 2017
Merged
Show file tree
Hide file tree
Changes from all 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
77 changes: 49 additions & 28 deletions components/button/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,40 +6,61 @@ import classnames from 'classnames';
/**
* WordPress dependencies
*/
import { createElement } from '@wordpress/element';
import { Component, createElement } from '@wordpress/element';

/**
* Internal dependencies
*/
import './style.scss';

function Button( {
href,
target,
isPrimary,
isLarge,
isSmall,
isToggled,
className,
disabled,
...additionalProps
} ) {
const classes = classnames( 'components-button', className, {
button: ( isPrimary || isLarge ),
'button-primary': isPrimary,
'button-large': isLarge,
'button-small': isSmall,
'is-toggled': isToggled,
} );

const tag = href !== undefined && ! disabled ? 'a' : 'button';
const tagProps = tag === 'a' ? { href, target } : { type: 'button', disabled };

return createElement( tag, {
...tagProps,
...additionalProps,
className: classes,
} );
class Button extends Component {
constructor( props ) {
super( props );
this.setRef = this.setRef.bind( this );
}

componentDidMount() {
if ( this.props.focus ) {
this.ref.focus();
}
}

setRef( ref ) {
this.ref = ref;
}

render() {
const {
href,
target,
isPrimary,
isLarge,
isSmall,
isToggled,
className,
disabled,
...additionalProps
} = this.props;
const classes = classnames( 'components-button', className, {
button: ( isPrimary || isLarge ),
'button-primary': isPrimary,
'button-large': isLarge,
'button-small': isSmall,
'is-toggled': isToggled,
} );

const tag = href !== undefined && ! disabled ? 'a' : 'button';
const tagProps = tag === 'a' ? { href, target } : { type: 'button', disabled };

delete additionalProps.focus;

return createElement( tag, {
...tagProps,
...additionalProps,
className: classes,
ref: this.setRef,
} );
}
}

export default Button;
4 changes: 2 additions & 2 deletions components/icon-button/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ import Dashicon from '../dashicon';
// is common to apply a ref to the button element (only supported in class)
class IconButton extends Component {
render() {
const { icon, children, label, className, tooltip, ...additionalProps } = this.props;
const { icon, children, label, className, tooltip, focus, ...additionalProps } = this.props;
const classes = classnames( 'components-icon-button', className );

let element = (
<Button { ...additionalProps } aria-label={ label } className={ classes }>
<Button { ...additionalProps } aria-label={ label } className={ classes } focus={ focus }>
{ isString( icon ) ? <Dashicon icon={ icon } /> : icon }
{ children }
</Button>
Expand Down
4 changes: 3 additions & 1 deletion editor/block-settings-menu/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import BlockDeleteButton from './block-delete-button';
import { selectBlock } from '../actions';
import UnknownConverter from './unknown-converter';

function BlockSettingsMenu( { uids, onSelect } ) {
function BlockSettingsMenu( { uids, onSelect, focus } ) {
const count = uids.length;

return (
Expand All @@ -32,6 +32,7 @@ function BlockSettingsMenu( { uids, onSelect } ) {
const toggleClassname = classnames( 'editor-block-settings-menu__toggle', {
'is-opened': isOpen,
} );

return (
<IconButton
className={ toggleClassname }
Expand All @@ -44,6 +45,7 @@ function BlockSettingsMenu( { uids, onSelect } ) {
icon="ellipsis"
label={ isOpen ? __( 'Close Settings Menu' ) : __( 'Open Settings Menu' ) }
aria-expanded={ isOpen }
focus={ focus }
/>
);
} }
Expand Down
7 changes: 5 additions & 2 deletions editor/modes/visual-editor/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ class VisualEditorBlock extends Component {

// Generate the wrapper class names handling the different states of the block.
const { isHovered, isSelected, isMultiSelected, isFirstMultiSelected, focus } = this.props;
const showUI = isSelected && ( ! this.props.isTyping || focus.collapsed === false );
const showUI = isSelected && ( ! this.props.isTyping || ( focus && focus.collapsed === false ) );
const isProperlyHovered = isHovered && ! this.props.isSelecting;
const { error } = this.state;
const wrapperClassName = classnames( 'editor-visual-editor__block', {
Expand Down Expand Up @@ -362,7 +362,10 @@ class VisualEditorBlock extends Component {
<BlockMover uids={ multiSelectedBlockUids } />
}
{ isFirstMultiSelected && ! this.props.isSelecting &&
<BlockSettingsMenu uids={ multiSelectedBlockUids } />
<BlockSettingsMenu
uids={ multiSelectedBlockUids }
focus={ true }
/>
}
<div
ref={ this.bindBlockNode }
Expand Down
20 changes: 9 additions & 11 deletions editor/writing-flow/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,6 @@ class WritingFlow extends Component {
this.verticalRect = null;
}

componentDidMount() {
document.addEventListener( 'keydown', this.onKeyDown );
document.addEventListener( 'mousedown', this.clearVerticalRect );
}

componentWillUnmount() {
document.removeEventListener( 'keydown', this.onKeyDown );
document.addEventListener( 'mousedown', this.clearVerticalRect );
}

bindContainer( ref ) {
this.container = ref;
}
Expand Down Expand Up @@ -166,11 +156,19 @@ class WritingFlow extends Component {
render() {
const { children } = this.props;

// Disable reason: Wrapper itself is non-interactive, but must capture
// bubbling events from children to determine focus transition intents.
/* eslint-disable jsx-a11y/no-static-element-interactions */
return (
<div ref={ this.bindContainer }>
<div
ref={ this.bindContainer }
onKeyDown={ this.onKeyDown }
onMouseDown={ this.clearVerticalRect }
>
{ children }
</div>
);
/* eslint-disable jsx-a11y/no-static-element-interactions */
}
}

Expand Down