Skip to content

Commit

Permalink
Fix mobile inserter focus behavior (#29342)
Browse files Browse the repository at this point in the history
On mobile, the focus actually never moves back to the newly inserter block in the inserter. So this means the change in behavior in the block selection behavior (selected used to mean focus), cause the focus to just stay where it was in mobile when inserting blocks.

Co-authored-by: Bernie Reiter <[email protected]>
  • Loading branch information
youknowriad and ockham authored Feb 25, 2021
1 parent fbf65da commit 99aa61e
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 32 deletions.
3 changes: 2 additions & 1 deletion packages/block-editor/src/components/inserter/library.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ function InserterLibrary( {
showMostUsedBlocks = false,
__experimentalInsertionIndex,
onSelect = noop,
shouldFocusBlock = false,
} ) {
const destinationRootClientId = useSelect(
( select ) => {
Expand All @@ -43,7 +44,7 @@ function InserterLibrary( {
showInserterHelpPanel={ showInserterHelpPanel }
showMostUsedBlocks={ showMostUsedBlocks }
__experimentalInsertionIndex={ __experimentalInsertionIndex }
shouldFocusBlock={ false }
shouldFocusBlock={ shouldFocusBlock }
/>
);
}
Expand Down
30 changes: 18 additions & 12 deletions packages/e2e-tests/specs/editor/various/inserter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
createNewPost,
openGlobalBlockInserter,
insertBlock,
setBrowserViewport,
} from '@wordpress/e2e-test-utils';

describe( 'Inserter', () => {
Expand All @@ -25,20 +26,25 @@ describe( 'Inserter', () => {
expect( isPreviewVisible ).toBe( true );
} );

it( 'last-inserted block should be given and keep the focus', async () => {
await page.type(
'.block-editor-default-block-appender__content',
'Testing inserted block focus'
);
it.each( [ 'large', 'small' ] )(
'last-inserted block should be given and keep the focus (%s viewport)',
async ( viewport ) => {
await setBrowserViewport( viewport );

await insertBlock( 'Image' );
await page.type(
'.block-editor-default-block-appender__content',
'Testing inserted block focus'
);

await page.waitForSelector( 'figure[data-type="core/image"]' );
await insertBlock( 'Image' );

const selectedBlock = await page.evaluate( () => {
return wp.data.select( 'core/block-editor' ).getSelectedBlock();
} );
await page.waitForSelector( 'figure[data-type="core/image"]' );

expect( selectedBlock.name ).toBe( 'core/image' );
} );
const selectedBlock = await page.evaluate( () => {
return wp.data.select( 'core/block-editor' ).getSelectedBlock();
} );

expect( selectedBlock.name ).toBe( 'core/image' );
}
);
} );
6 changes: 1 addition & 5 deletions packages/edit-post/src/components/layout/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,11 +220,7 @@ function Layout( { styles } ) {
<Library
showMostUsedBlocks={ showMostUsedBlocks }
showInserterHelpPanel
onSelect={ () => {
if ( isMobileViewport ) {
setIsInserterOpened( false );
}
} }
shouldFocusBlock={ isMobileViewport }
/>
</div>
</div>
Expand Down
12 changes: 3 additions & 9 deletions packages/edit-site/src/components/editor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,15 +219,9 @@ function Editor( { initialSettings } ) {
<div className="edit-site-editor__inserter-panel-content">
<Library
showInserterHelpPanel
onSelect={ () => {
if (
isMobile
) {
setIsInserterOpened(
false
);
}
} }
shouldFocusBlock={
isMobile
}
/>
</div>
</div>
Expand Down
6 changes: 1 addition & 5 deletions packages/edit-widgets/src/components/layout/interface.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,7 @@ function Interface( { blockEditorSettings } ) {
<div className="edit-widgets-layout__inserter-panel-content">
<Library
showInserterHelpPanel
onSelect={ () => {
if ( isMobileViewport ) {
setIsInserterOpened( false );
}
} }
shouldFocusBlock={ isMobileViewport }
rootClientId={ rootClientId }
__experimentalInsertionIndex={ insertionIndex }
/>
Expand Down

0 comments on commit 99aa61e

Please sign in to comment.