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 fccc9132bf98e..bb1d22b2865de 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 @@ -43,13 +43,24 @@ export default function useTabNav() { } else { setNavigationMode( true ); + const canvasElement = + container.current.ownerDocument === event.target.ownerDocument + ? container.current + : container.current.ownerDocument.defaultView.frameElement; + const isBefore = // eslint-disable-next-line no-bitwise - event.target.compareDocumentPosition( container.current ) & + event.target.compareDocumentPosition( canvasElement ) & event.target.DOCUMENT_POSITION_FOLLOWING; - const action = isBefore ? 'findNext' : 'findPrevious'; + const tabbables = focus.tabbable.find( container.current ); + + if ( tabbables.length ) { + const next = isBefore + ? tabbables[ 0 ] + : tabbables[ tabbables.length - 1 ]; - focus.tabbable[ action ]( event.target ).focus(); + next.focus(); + } } }