From c8aad4d5952a34874eb3c56fa901cce1b55967f2 Mon Sep 17 00:00:00 2001 From: Jerry Jones Date: Thu, 3 Oct 2024 12:11:53 -0500 Subject: [PATCH] Focus first section root block if no selected block and tabbing to zoom out canvas (#65843) --- .../components/writing-flow/use-tab-nav.js | 32 ++++++++++++++++--- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/packages/block-editor/src/components/writing-flow/use-tab-nav.js b/packages/block-editor/src/components/writing-flow/use-tab-nav.js index 3788c7021fd66..216e7b6e04ad5 100644 --- a/packages/block-editor/src/components/writing-flow/use-tab-nav.js +++ b/packages/block-editor/src/components/writing-flow/use-tab-nav.js @@ -19,10 +19,17 @@ export default function useTabNav() { const focusCaptureBeforeRef = useRef(); const focusCaptureAfterRef = useRef(); - const { hasMultiSelection, getSelectedBlockClientId, getBlockCount } = - useSelect( blockEditorStore ); + const { + hasMultiSelection, + getSelectedBlockClientId, + getBlockCount, + getBlockOrder, + getLastFocus, + getSectionRootClientId, + isZoomOut, + __unstableGetEditorMode, + } = unlock( useSelect( blockEditorStore ) ); const { setLastFocus } = unlock( useDispatch( blockEditorStore ) ); - const { getLastFocus } = unlock( useSelect( blockEditorStore ) ); // Reference that holds the a flag for enabling or disabling // capturing on the focus capture elements. @@ -45,6 +52,24 @@ export default function useTabNav() { ) .focus(); } + } + // In "compose" mode without a selected ID, we want to place focus on the section root when tabbing to the canvas. + else if ( __unstableGetEditorMode() === 'zoom-out' && isZoomOut() ) { + const sectionRootClientId = getSectionRootClientId(); + const sectionBlocks = getBlockOrder( sectionRootClientId ); + + // If we have section within the section root, focus the first one. + if ( sectionBlocks.length ) { + container.current + .querySelector( `[data-block="${ sectionBlocks[ 0 ] }"]` ) + .focus(); + } + // If we don't have any section blocks, focus the section root. + else { + container.current + .querySelector( `[data-block="${ sectionRootClientId }"]` ) + .focus(); + } } else { const canvasElement = container.current.ownerDocument === event.target.ownerDocument @@ -61,7 +86,6 @@ export default function useTabNav() { const next = isBefore ? tabbables[ 0 ] : tabbables[ tabbables.length - 1 ]; - next.focus(); } }