Skip to content

Commit

Permalink
Defer focusing on then next frame
Browse files Browse the repository at this point in the history
  • Loading branch information
kevin940726 committed May 19, 2023
1 parent d4cec92 commit e75c4e7
Showing 1 changed file with 11 additions and 15 deletions.
26 changes: 11 additions & 15 deletions packages/block-editor/src/components/list-view/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,21 +192,17 @@ function ListViewBlock( {
return focus.focusable.find( row )[ 0 ];
};

let focusElement = getFocusElement();
if ( focusElement ) {
focusElement.focus();
} else {
// The element hasn't been painted yet. Defer focusing on the next frame.
// This could happen when all blocks have been deleted and the default block
// hasn't been added to the editor yet.
window.requestAnimationFrame( () => {
focusElement = getFocusElement();
// Ignore if the element still doesn't exist.
if ( focusElement ) {
focusElement.focus();
}
} );
}
// Defer focusing on the next frame so that React can update the description
// of the focused element before some Screen Readers read it.
// It also solves problem when all blocks have been deleted and the default
// block hasn't been rendered yet.
window.requestAnimationFrame( () => {
const focusElement = getFocusElement();
// Ignore if the element doesn't exist.
if ( focusElement ) {
focusElement.focus();
}
} );
},
[ selectBlock, treeGridElementRef ]
);
Expand Down

0 comments on commit e75c4e7

Please sign in to comment.