Skip to content

Commit

Permalink
Focus first section root block if no selected block and tabbing to zo…
Browse files Browse the repository at this point in the history
…om out canvas (#65843)
  • Loading branch information
jeryj authored Oct 3, 2024
1 parent 6e0b843 commit f4402db
Showing 1 changed file with 28 additions and 4 deletions.
32 changes: 28 additions & 4 deletions packages/block-editor/src/components/writing-flow/use-tab-nav.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand All @@ -61,7 +86,6 @@ export default function useTabNav() {
const next = isBefore
? tabbables[ 0 ]
: tabbables[ tabbables.length - 1 ];

next.focus();
}
}
Expand Down

0 comments on commit f4402db

Please sign in to comment.