Skip to content

Commit

Permalink
Block: reposition tabbable inserter (#19596)
Browse files Browse the repository at this point in the history
* Block: reposition tabbable inserter

* Fix e2e
  • Loading branch information
ellatrix authored Jan 13, 2020
1 parent e7342de commit 06782e1
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 53 deletions.
29 changes: 29 additions & 0 deletions packages/block-editor/src/components/block-list/block-popover.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* External dependencies
*/
import { findIndex } from 'lodash';
import classnames from 'classnames';

/**
* WordPress dependencies
Expand Down Expand Up @@ -60,6 +61,7 @@ function BlockPopover( {
} = useSelect( selector, [] );
const isLargeViewport = useViewportMatch( 'medium' );
const [ isToolbarForced, setIsToolbarForced ] = useState( false );
const [ isInserterShown, setIsInserterShown ] = useState( false );

const showEmptyBlockSideInserter = ! isNavigationMode && isEmptyDefaultBlock && isValid;
const shouldShowBreadcrumb = isNavigationMode;
Expand Down Expand Up @@ -97,6 +99,14 @@ function BlockPopover( {
return null;
}

function onFocus() {
setIsInserterShown( true );
}

function onBlur() {
setIsInserterShown( false );
}

// Position above the anchor, pop out towards the right, and position in the
// left corner. For the side inserter, pop out towards the left, and
// position in the right corner.
Expand All @@ -119,6 +129,25 @@ function BlockPopover( {
__unstableAllowHorizontalSubpixelPosition={ moverDirection === 'horizontal' && node }
onBlur={ () => setIsToolbarForced( false ) }
>
{ ( shouldShowContextualToolbar || isToolbarForced ) && (
<div
onFocus={ onFocus }
onBlur={ onBlur }
// While ideally it would be enough to capture the
// bubbling focus event from the Inserter, due to the
// characteristics of click focusing of `button`s in
// Firefox and Safari, it is not reliable.
//
// See: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button#Clicking_and_focus
tabIndex={ -1 }
className={ classnames(
'block-editor-block-list__block-popover-inserter',
{ 'is-visible': isInserterShown }
) }
>
<Inserter clientId={ clientId } rootClientId={ rootClientId } />
</div>
) }
{ ( shouldShowContextualToolbar || isToolbarForced ) && (
<BlockContextualToolbar
// If the toolbar is being shown because of being forced
Expand Down
13 changes: 13 additions & 0 deletions packages/block-editor/src/components/block-list/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,20 @@
}

justify-content: center;
}

.block-editor-block-list__block-popover-inserter {
position: absolute;
top: -9999em;
margin-bottom: $block-padding;

&.is-visible {
position: static;
}
}

.block-editor-block-list__insertion-point-inserter,
.block-editor-block-list__block-popover-inserter {
// Show a clickable plus.
.block-editor-inserter__toggle {
border-radius: 50%;
Expand Down
40 changes: 0 additions & 40 deletions packages/block-editor/src/components/block-toolbar/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
/**
* External dependencies
*/
import classnames from 'classnames';

/**
* WordPress dependencies
*/
import { useSelect } from '@wordpress/data';
import { Toolbar } from '@wordpress/components';
import { useState } from '@wordpress/element';

/**
* Internal dependencies
Expand All @@ -20,14 +13,12 @@ import BlockSettingsMenu from '../block-settings-menu';
import BlockSwitcher from '../block-switcher';
import MultiBlocksSwitcher from '../block-switcher/multi-blocks-switcher';
import BlockMover from '../block-mover';
import Inserter from '../inserter';

export default function BlockToolbar( { hasMovers = true } ) {
const {
blockClientIds,
isValid,
mode,
rootClientId,
moverDirection,
} = useSelect( ( select ) => {
const {
Expand Down Expand Up @@ -56,40 +47,11 @@ export default function BlockToolbar( { hasMovers = true } ) {
moverDirection: __experimentalMoverDirection,
};
}, [] );
const [ isInserterShown, setIsInserterShown ] = useState( false );

if ( blockClientIds.length === 0 ) {
return null;
}

function onFocus() {
setIsInserterShown( true );
}

function onBlur() {
setIsInserterShown( false );
}

const inserter = (
<Toolbar
onFocus={ onFocus }
onBlur={ onBlur }
// While ideally it would be enough to capture the
// bubbling focus event from the Inserter, due to the
// characteristics of click focusing of `button`s in
// Firefox and Safari, it is not reliable.
//
// See: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button#Clicking_and_focus
tabIndex={ -1 }
className={ classnames(
'block-editor-block-toolbar__inserter',
{ 'is-visible': isInserterShown }
) }
>
<Inserter clientId={ blockClientIds[ 0 ] } rootClientId={ rootClientId } />
</Toolbar>
);

if ( blockClientIds.length > 1 ) {
return (
<div className="block-editor-block-toolbar">
Expand All @@ -99,7 +61,6 @@ export default function BlockToolbar( { hasMovers = true } ) {
/> ) }
<MultiBlocksSwitcher />
<BlockSettingsMenu clientIds={ blockClientIds } />
{ inserter }
</div>
);
}
Expand All @@ -118,7 +79,6 @@ export default function BlockToolbar( { hasMovers = true } ) {
</>
) }
<BlockSettingsMenu clientIds={ blockClientIds } />
{ inserter }
</div>
);
}
10 changes: 0 additions & 10 deletions packages/block-editor/src/components/block-toolbar/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,6 @@
border-color: $light-gray-500;
}
}

&__inserter {
opacity: 0;
pointer-events: none;

&.is-visible {
opacity: 1;
pointer-events: all;
}
}
}

.block-editor-block-toolbar__slot {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ const navigateToContentEditorTop = async () => {
};

const tabThroughParagraphBlock = async ( paragraphText, blockIndex ) => {
await page.keyboard.press( 'Tab' );
await expect( await getActiveLabel() ).toBe( 'Add block' );

await tabThroughBlockMoverControl();
await tabThroughBlockToolbar();

Expand Down Expand Up @@ -68,9 +71,6 @@ const tabThroughBlockToolbar = async () => {

await page.keyboard.press( 'Tab' );
await expect( await getActiveLabel() ).toBe( 'More options' );

await page.keyboard.press( 'Tab' );
await expect( await getActiveLabel() ).toBe( 'Add block' );
};

describe( 'Order of block keyboard navigation', () => {
Expand Down

0 comments on commit 06782e1

Please sign in to comment.