Skip to content

Commit

Permalink
Fix focus loss due to filtering blocks
Browse files Browse the repository at this point in the history
When inserting a block like the list block that filters the available blocks in the inserter, there will be a focus loss. onInsert, check if focus is still within the inserter. If it's not, return focus to the first item.
  • Loading branch information
jeryj committed May 10, 2024
1 parent 90e3d43 commit cbd1b60
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
16 changes: 7 additions & 9 deletions packages/block-editor/src/components/inserter/block-types-tab.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* WordPress dependencies
*/
import { __, _x } from '@wordpress/i18n';
import { useMemo, useEffect } from '@wordpress/element';
import { useMemo, useEffect, forwardRef } from '@wordpress/element';
import { pipe, useAsyncList } from '@wordpress/compose';

/**
Expand All @@ -27,12 +27,10 @@ const MAX_SUGGESTED_ITEMS = 6;
*/
const EMPTY_ARRAY = [];

export function BlockTypesTab( {
rootClientId,
onInsert,
onHover,
showMostUsedBlocks,
} ) {
export function BlockTypesTab(
{ rootClientId, onInsert, onHover, showMostUsedBlocks },
ref
) {
const [ items, categories, collections, onSelectItem ] = useBlockTypesState(
rootClientId,
onInsert
Expand Down Expand Up @@ -109,7 +107,7 @@ export function BlockTypesTab( {

return (
<InserterListbox>
<div>
<div ref={ ref }>
{ showMostUsedBlocks && !! suggestedItems.length && (
<InserterPanel title={ _x( 'Most used', 'blocks' ) }>
<BlockTypesList
Expand Down Expand Up @@ -184,4 +182,4 @@ export function BlockTypesTab( {
);
}

export default BlockTypesTab;
export default forwardRef( BlockTypesTab );
17 changes: 16 additions & 1 deletion packages/block-editor/src/components/inserter/menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,27 @@ function InserterMenu(
insertionIndex: __experimentalInsertionIndex,
shouldFocusBlock,
} );
const blockTypesTabRef = useRef();

const onInsert = useCallback(
( blocks, meta, shouldForceFocusBlock ) => {
onInsertBlocks( blocks, meta, shouldForceFocusBlock );
onSelect();

// Check for focus loss due to filtering blocks by selected block type
window.requestAnimationFrame( () => {
if (
! shouldFocusBlock &&
! blockTypesTabRef?.current.contains(
ref.current.ownerDocument.activeElement
)
) {
// There has been a focus loss, so focus the first button in the block types tab
blockTypesTabRef?.current.querySelector( 'button' ).focus();
}
} );
},
[ onInsertBlocks, onSelect ]
[ onInsertBlocks, onSelect, shouldFocusBlock ]
);

const onInsertPattern = useCallback(
Expand Down Expand Up @@ -188,6 +202,7 @@ function InserterMenu(
<>
<div className="block-editor-inserter__block-list">
<BlockTypesTab
ref={ blockTypesTabRef }
rootClientId={ destinationRootClientId }
onInsert={ onInsert }
onHover={ onHover }
Expand Down

0 comments on commit cbd1b60

Please sign in to comment.