Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
39 changes: 22 additions & 17 deletions packages/block-editor/src/components/writing-flow/use-tab-nav.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { isInSameBlock, isInsideRootBlock } from '../../utils/dom';
import { unlock } from '../../lock-unlock';

export default function useTabNav() {
const container = useRef();
const containerRef = /** @type {typeof useRef<HTMLElement>} */ ( useRef )();
const focusCaptureBeforeRef = useRef();
const focusCaptureAfterRef = useRef();

Expand All @@ -36,21 +36,21 @@ export default function useTabNav() {

function onFocusCapture( event ) {
const canvasElement =
container.current.ownerDocument === event.target.ownerDocument
? container.current
: container.current.ownerDocument.defaultView.frameElement;
containerRef.current.ownerDocument === event.target.ownerDocument
? containerRef.current
: containerRef.current.ownerDocument.defaultView.frameElement;

// Do not capture incoming focus if set by us in WritingFlow.
if ( noCaptureRef.current ) {
noCaptureRef.current = null;
} else if ( hasMultiSelection() ) {
container.current.focus();
containerRef.current.focus();
} else if ( getSelectedBlockClientId() ) {
if ( getLastFocus()?.current ) {
getLastFocus().current.focus();
} else {
// Handles when the last focus has not been set yet, or has been cleared by new blocks being added via the inserter.
container.current
containerRef.current
.querySelector(
`[data-block="${ getSelectedBlockClientId() }"]`
)
Expand All @@ -64,13 +64,13 @@ export default function useTabNav() {

// If we have section within the section root, focus the first one.
if ( sectionBlocks.length ) {
container.current
containerRef.current
.querySelector( `[data-block="${ sectionBlocks[ 0 ] }"]` )
.focus();
}
// If we don't have any section blocks, focus the section root.
else if ( sectionRootClientId ) {
container.current
containerRef.current
.querySelector( `[data-block="${ sectionRootClientId }"]` )
.focus();
} else {
Expand All @@ -82,8 +82,7 @@ export default function useTabNav() {
// eslint-disable-next-line no-bitwise
event.target.compareDocumentPosition( canvasElement ) &
event.target.DOCUMENT_POSITION_FOLLOWING;
const tabbables = focus.tabbable.find( container.current );

const tabbables = focus.tabbable.find( containerRef.current );
if ( tabbables.length ) {
const next = isBefore
? tabbables[ 0 ]
Expand Down Expand Up @@ -125,20 +124,26 @@ export default function useTabNav() {
return;
}

if ( ! hasMultiSelection() && ! getSelectedBlockClientId() ) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@stokesman, could you add a comment here about why we can remove this condition now? This is primarily for the future.

@stokesman stokesman Mar 21, 2025 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This condition and early return when met lets focus to move, as it does innately, to the closest adjacent tab stop. Since the post title is not a block, when shift+tabbing from there it was trapping focus because:

  1. shift+tab moves focus to the writing flow wrapper (closest adjacent tab stop)
  2. a subsequent shift+tab moves focus to the focus capture element which moves it back to the post title

By removing this conditional early return, tabbing from the post title moves focus to the adjacent tab stop outside of the wrapper. Thus it avoids the sibling focus capture elements and the focus trap.

For further reference, this early return was removed in #65204 previous to this PR but that surfaced another bug #69037. It wasn’t clear that the removing the early return had been intentional so we restored it in #69079. That’s how we got here. Though I’m still not sure its prior removal was intentional.

It’s also worth nothing that prior to #65204 when the same conditional early return was there shift+tabbing from the post title worked differently. It took two steps to get outside the canvas, the first step focusing the canvas/writing flow wrapper. I didn’t figure out why/how it worked like that before and why now this same condition/return caused trapping of focus.

if (
// Bails in case the focus capture elements aren’t present. They
// may be omitted to avoid silent tab stops in preview mode.
// See: https://github.com/WordPress/gutenberg/pull/59317
! focusCaptureAfterRef.current ||
! focusCaptureBeforeRef.current
) {
return;
}

const isShift = event.shiftKey;
const { target, shiftKey: isShift } = event;
const direction = isShift ? 'findPrevious' : 'findNext';
const nextTabbable = focus.tabbable[ direction ]( event.target );
const nextTabbable = focus.tabbable[ direction ]( target );

// We want to constrain the tabbing to the block and its child blocks.
// If the preceding form element is within a different block,
// such as two sibling image blocks in the placeholder state,
// we want shift + tab from the first form element to move to the image
// block toolbar and not the previous image block's form element.
const currentBlock = event.target.closest( '[data-block]' );
const currentBlock = target.closest( '[data-block]' );
const isElementPartOfSelectedBlock =
currentBlock &&
nextTabbable &&
Expand All @@ -157,7 +162,6 @@ export default function useTabNav() {
) {
return;
}

const next = isShift ? focusCaptureBeforeRef : focusCaptureAfterRef;

// Disable focus capturing on the focus capture element, so it
Expand All @@ -180,6 +184,7 @@ export default function useTabNav() {
// the writing flow wrapper.
if (
! event.relatedTarget &&
event.target.hasAttribute( 'data-block' ) &&
ownerDocument.activeElement === ownerDocument.body &&
getBlockCount() === 0
) {
Expand All @@ -204,7 +209,7 @@ export default function useTabNav() {
return;
}

if ( container.current === event.target ) {
if ( containerRef.current === event.target ) {
return;
}

Expand Down Expand Up @@ -233,7 +238,7 @@ export default function useTabNav() {
};
}, [] );

const mergedRefs = useMergeRefs( [ container, ref ] );
const mergedRefs = useMergeRefs( [ containerRef, ref ] );

return [ before, mergedRefs, after ];
}
18 changes: 18 additions & 0 deletions test/e2e/specs/editor/various/writing-flow.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -890,6 +890,24 @@ test.describe( 'Writing Flow (@firefox, @webkit)', () => {
).toHaveClass( /is-selected/ );
} );

test( 'should focus preceding tabbable using shift+tab from post title and writing flow container', async ( {
editor,
page,
} ) => {
const optionsButton = page
.getByRole( 'region', { name: 'Editor top bar' } )
.getByRole( 'button', { name: 'Options' } );
await page.keyboard.press( 'Shift+Tab' );
await expect( optionsButton ).toBeFocused();

const editorCanvasBody = editor.canvas.locator( 'body' );
// Focuses the editor canvas body. In the editor the click doesn’t have
// to be on the element itself – just somewhere that won’t focus a block.
await editorCanvasBody.click();
await page.keyboard.press( 'Shift+Tab' );
await expect( optionsButton ).toBeFocused();
} );

test( 'should only consider the content as one tab stop', async ( {
editor,
page,
Expand Down