Skip to content

Commit

Permalink
Fix block lock toolbar item stealing focus when mounted with StrictMo…
Browse files Browse the repository at this point in the history
…de (#57185)
  • Loading branch information
jeryj authored Dec 19, 2023
1 parent b13ae58 commit c868408
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion packages/block-editor/src/components/block-lock/toolbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export default function BlockLockToolbar( { clientId, wrapperRef } ) {

const lockButtonRef = useRef( null );
const isFirstRender = useRef( true );
const hasModalOpened = useRef( false );

const shouldHideBlockLockUI =
! canLock || ( canEdit && canMove && canRemove );
Expand All @@ -36,7 +37,19 @@ export default function BlockLockToolbar( { clientId, wrapperRef } ) {
return;
}

if ( ! isModalOpen && shouldHideBlockLockUI ) {
if ( isModalOpen && ! hasModalOpened.current ) {
hasModalOpened.current = true;
}

// We only want to allow this effect to happen if the modal has been opened.
// The issue is when we're returning focus from the block lock modal to a toolbar,
// so it can only happen after a modal has been opened. Without this, the toolbar
// will steal focus on rerenders.
if (
hasModalOpened.current &&
! isModalOpen &&
shouldHideBlockLockUI
) {
focus.focusable
.find( wrapperRef.current, {
sequential: false,
Expand Down

0 comments on commit c868408

Please sign in to comment.