Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
BlockLockModal: restore focus to first focusable item when unlocking …
…block from toolbar button
  • Loading branch information
ciampo committed Jun 26, 2023
commit af51379fbed18551d8896b01550048afb59d4708
3 changes: 2 additions & 1 deletion packages/block-editor/src/components/block-lock/modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ function getTemplateLockValue( lock ) {
return false;
}

export default function BlockLockModal( { clientId, onClose } ) {
export default function BlockLockModal( { clientId, onClose, onFocusReturn } ) {
const [ lock, setLock ] = useState( { move: false, remove: false } );
const { canEdit, canMove, canRemove } = useBlockLock( clientId );
const { allowsEditLocking, templateLock, hasTemplateLock } = useSelect(
Expand Down Expand Up @@ -89,6 +89,7 @@ export default function BlockLockModal( { clientId, onClose } ) {
) }
overlayClassName="block-editor-block-lock-modal"
onRequestClose={ onClose }
onFocusReturn={ onFocusReturn }
>
<p>
{ __(
Expand Down
35 changes: 32 additions & 3 deletions packages/block-editor/src/components/block-lock/toolbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
*/
import { __ } from '@wordpress/i18n';
import { ToolbarButton, ToolbarGroup } from '@wordpress/components';
import { useReducer } from '@wordpress/element';
import { focus } from '@wordpress/dom';
import { useReducer, useRef } from '@wordpress/element';
import { lock } from '@wordpress/icons';

/**
Expand All @@ -12,14 +13,16 @@ import { lock } from '@wordpress/icons';
import BlockLockModal from './modal';
import useBlockLock from './use-block-lock';

export default function BlockLockToolbar( { clientId } ) {
export default function BlockLockToolbar( { clientId, wrapperRef } ) {
const { canEdit, canMove, canRemove, canLock } = useBlockLock( clientId );

const [ isModalOpen, toggleModal ] = useReducer(
( isActive ) => ! isActive,
false
);

const lockButtonRef = useRef( null );

if ( ! canLock ) {
return null;
}
Expand All @@ -35,10 +38,36 @@ export default function BlockLockToolbar( { clientId } ) {
icon={ lock }
label={ __( 'Unlock' ) }
onClick={ toggleModal }
ref={ lockButtonRef }
/>
</ToolbarGroup>
{ isModalOpen && (
<BlockLockModal clientId={ clientId } onClose={ toggleModal } />
<BlockLockModal
clientId={ clientId }
onClose={ toggleModal }
onFocusReturn={ ( defaultFocusReturnElement ) => {
if ( defaultFocusReturnElement ) {
defaultFocusReturnElement.focus();
}

if (
defaultFocusReturnElement.ownerDocument
.activeElement !== defaultFocusReturnElement &&
wrapperRef.current
) {
focus.focusable
.find( wrapperRef.current, {
sequential: false,
} )
.find(
( element ) =>
element.tagName === 'BUTTON' &&
element !== lockButtonRef.current
)
?.focus();
}
} }
/>
) }
</>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ const BlockToolbar = ( { hideDragHandle } ) => {
};
}, [] );

const toolbarWrapperRef = useRef( null );

// Handles highlighting the current block outline on hover or focus of the
// block type toolbar area.
const { toggleBlockHighlight } = useDispatch( blockEditorStore );
Expand Down Expand Up @@ -123,7 +125,7 @@ const BlockToolbar = ( { hideDragHandle } ) => {
} );

return (
<div className={ classes }>
<div className={ classes } ref={ toolbarWrapperRef }>
{ ! isMultiToolbar &&
isLargeViewport &&
blockEditingMode === 'default' && <BlockParentSelector /> }
Expand All @@ -135,6 +137,7 @@ const BlockToolbar = ( { hideDragHandle } ) => {
{ ! isMultiToolbar && (
<BlockLockToolbar
clientId={ blockClientIds[ 0 ] }
wrapperRef={ toolbarWrapperRef }
/>
) }
<BlockMover
Expand Down