Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Focus currently selected block when entering canvas #61472

Merged
merged 3 commits into from
May 9, 2024
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
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
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
Loading