Skip to content
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { useCallback } from '@wordpress/element';
* Internal dependencies
*/
import { store as blockEditorStore } from '../../../store';
import { unlock } from '../../../lock-unlock';

/**
* @typedef WPInserterConfig
Expand Down Expand Up @@ -86,10 +87,23 @@ function useInsertionPoint( {
insertBlocks,
showInsertionPoint,
hideInsertionPoint,
} = useDispatch( blockEditorStore );
setLastFocus,
} = unlock( useDispatch( blockEditorStore ) );

const onInsertBlocks = useCallback(
( blocks, meta, shouldForceFocusBlock = false ) => {
// When we are trying to move focus or select a new block on insert, we also
// need to clear the last focus to avoid the focus being set to the wrong block
// when tabbing back into the canvas if the block was added from outside the
// editor canvas.
if (
shouldForceFocusBlock ||
shouldFocusBlock ||
selectBlockOnInsert
) {
setLastFocus( null );

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I considered doing this within the inserter sidebar menu, but I think it should live here as it will be an issue for anyone trying to insert new blocks from outside the editor canvas. This allows people to choose if they want focus to return to the canvas where it was (preserve last focus) or clear it and focus the new block.

}

const selectedBlock = getSelectedBlock();

if (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,16 @@ export default function useTabNav() {
} else if ( hasMultiSelection() ) {
container.current.focus();
} else if ( getSelectedBlockClientId() ) {
getLastFocus()?.current.focus();
if ( getLastFocus()?.current ) {
getLastFocus().current.focus();
} else {
Comment on lines +48 to +50

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had to preserve this, as focusing the selected block broke focusing inner blocks or things like focusing the table grid cell text field within a table block.

// Handles when the last focus has not been set yet, or has been cleared by new blocks being added via the inserter.
container.current
.querySelector(
`[data-block="${ getSelectedBlockClientId() }"]`
)
.focus();
}
} else {
setNavigationMode( true );

Expand Down