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

Refactor BlockMover to use React hooks #24774

Merged
merged 1 commit into from
Aug 28, 2020
Merged
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
118 changes: 46 additions & 72 deletions packages/block-editor/src/components/block-mover/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,89 +9,63 @@ import classnames from 'classnames';
*/
import { ToolbarGroup, ToolbarItem } from '@wordpress/components';
import { getBlockType } from '@wordpress/blocks';
import { Component } from '@wordpress/element';
import { useState } from '@wordpress/element';
import { withSelect } from '@wordpress/data';

/**
* Internal dependencies
*/
import { BlockMoverUpButton, BlockMoverDownButton } from './button';

export class BlockMover extends Component {
constructor() {
super( ...arguments );
this.state = {
isFocused: false,
};
this.onFocus = this.onFocus.bind( this );
this.onBlur = this.onBlur.bind( this );
}
function BlockMover( {
isFirst,
isLast,
clientIds,
isLocked,
isHidden,
rootClientId,
orientation,
} ) {
const [ isFocused, setIsFocused ] = useState( false );

onFocus() {
this.setState( {
isFocused: true,
} );
}
const onFocus = () => setIsFocused( true );
const onBlur = () => setIsFocused( false );

onBlur() {
this.setState( {
isFocused: false,
} );
if ( isLocked || ( isFirst && isLast && ! rootClientId ) ) {
return null;
}

render() {
const {
isFirst,
isLast,
clientIds,
isLocked,
isHidden,
rootClientId,
orientation,
} = this.props;
const { isFocused } = this.state;
if ( isLocked || ( isFirst && isLast && ! rootClientId ) ) {
return null;
}

// We emulate a disabled state because forcefully applying the `disabled`
// attribute on the buttons 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.
return (
<div
className={ classnames( 'block-editor-block-mover', {
'is-visible': isFocused || ! isHidden,
'is-horizontal': orientation === 'horizontal',
} ) }
>
<ToolbarGroup>
<ToolbarItem
onFocus={ this.onFocus }
onBlur={ this.onBlur }
>
{ ( itemProps ) => (
<BlockMoverUpButton
clientIds={ clientIds }
{ ...itemProps }
/>
) }
</ToolbarItem>
<ToolbarItem
onFocus={ this.onFocus }
onBlur={ this.onBlur }
>
{ ( itemProps ) => (
<BlockMoverDownButton
clientIds={ clientIds }
{ ...itemProps }
/>
) }
</ToolbarItem>
</ToolbarGroup>
</div>
);
}
// We emulate a disabled state because forcefully applying the `disabled`
// attribute on the buttons 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.
return (
<div
className={ classnames( 'block-editor-block-mover', {
'is-visible': isFocused || ! isHidden,
'is-horizontal': orientation === 'horizontal',
} ) }
>
<ToolbarGroup>
<ToolbarItem onFocus={ onFocus } onBlur={ onBlur }>
{ ( itemProps ) => (
<BlockMoverUpButton
clientIds={ clientIds }
{ ...itemProps }
/>
) }
</ToolbarItem>
<ToolbarItem onFocus={ onFocus } onBlur={ onBlur }>
{ ( itemProps ) => (
<BlockMoverDownButton
clientIds={ clientIds }
{ ...itemProps }
/>
) }
</ToolbarItem>
</ToolbarGroup>
</div>
);
}

export default withSelect( ( select, { clientIds } ) => {
Expand Down