Skip to content

Commit

Permalink
Persistent List View: Fix the list stealing focus from the canvas on …
Browse files Browse the repository at this point in the history
…item mount (#31058)

When a block is selected, opening the persistent list view moves the focus away from the canvas to the corresponding list item.
To prevent the list view from stealing focus every time the selected block changes if the list view is open, the focus change only happened on list item mount.
This approach didn't take into account newly created blocks, which inherently spawn new list items, which of course mount and steal focus from the canvas.

This fix makes sure that the focus change only happens when the entire list view mounts for the first time.
Creating new blocks while the list view is open, will keep the focus in the canvas, as expected.
  • Loading branch information
Copons authored Apr 22, 2021
1 parent 971c0f6 commit 0d1757f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ export default function BlockNavigationBlock( {
const {
__experimentalFeatures: withExperimentalFeatures,
__experimentalPersistentListViewFeatures: withExperimentalPersistentListViewFeatures,
isTreeGridMounted,
} = useBlockNavigationContext();
const blockNavigationBlockSettingsClassName = classnames(
'block-editor-block-navigation-block__menu-cell',
Expand All @@ -88,7 +89,11 @@ export default function BlockNavigationBlock( {
// only focus the selected list item on mount; otherwise the list would always
// try to steal the focus from the editor canvas.
useEffect( () => {
if ( withExperimentalPersistentListViewFeatures && isSelected ) {
if (
withExperimentalPersistentListViewFeatures &&
! isTreeGridMounted &&
isSelected
) {
cellRef.current.focus();
}
}, [] );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/

import { __experimentalTreeGrid as TreeGrid } from '@wordpress/components';
import { useMemo } from '@wordpress/element';
import { useEffect, useMemo, useRef } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
/**
* Internal dependencies
Expand Down Expand Up @@ -31,6 +31,11 @@ export default function BlockNavigationTree( {
target: blockDropTarget,
} = useBlockNavigationDropZone();

const isMounted = useRef( false );
useEffect( () => {
isMounted.current = true;
}, [] );

if ( ! __experimentalFeatures ) {
blockDropTarget = undefined;
}
Expand All @@ -40,11 +45,13 @@ export default function BlockNavigationTree( {
__experimentalFeatures,
__experimentalPersistentListViewFeatures,
blockDropTarget,
isTreeGridMounted: isMounted.current,
} ),
[
__experimentalFeatures,
__experimentalPersistentListViewFeatures,
blockDropTarget,
isMounted.current,
]
);

Expand Down

0 comments on commit 0d1757f

Please sign in to comment.