Skip to content

Commit f11a7c0

Browse files
authored
DOM: exclude inert elements from focus.focusable (WordPress#75172)
1 parent 59ee769 commit f11a7c0

File tree

3 files changed

+35
-4
lines changed

3 files changed

+35
-4
lines changed

packages/block-editor/src/components/writing-flow/use-arrow-nav.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,6 @@ export function getClosestTabbable(
114114
}
115115

116116
function isTabCandidate( node ) {
117-
if ( node.closest( '[inert]' ) ) {
118-
return;
119-
}
120-
121117
// Skip if there's only one child that is content editable (and thus a
122118
// better candidate).
123119
if (

packages/dom/src/focusable.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,11 @@ export function find( context, { sequential = false } = {} ) {
107107
return false;
108108
}
109109

110+
// Elements inside an inert subtree are not focusable.
111+
if ( element.closest( '[inert]' ) ) {
112+
return false;
113+
}
114+
110115
const { nodeName } = element;
111116
if ( 'AREA' === nodeName ) {
112117
return isValidFocusableArea(

packages/dom/src/test/focusable.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,5 +157,35 @@ describe( 'focusable', () => {
157157

158158
expect( find( node ) ).toEqual( [] );
159159
} );
160+
161+
it( 'ignores elements inside inert containers', () => {
162+
const node = createElement( 'div' );
163+
const inertDiv = createElement( 'div' );
164+
inertDiv.setAttribute( 'inert', '' );
165+
const input = createElement( 'input' );
166+
inertDiv.appendChild( input );
167+
node.appendChild( inertDiv );
168+
169+
expect( find( node ) ).toEqual( [] );
170+
} );
171+
172+
it( 'returns focusable elements outside inert containers', () => {
173+
const node = createElement( 'div' );
174+
175+
// Inert container with input
176+
const inertDiv = createElement( 'div' );
177+
inertDiv.setAttribute( 'inert', '' );
178+
const inertInput = createElement( 'input' );
179+
inertDiv.appendChild( inertInput );
180+
node.appendChild( inertDiv );
181+
182+
// Non-inert input
183+
const visibleInput = createElement( 'input' );
184+
node.appendChild( visibleInput );
185+
186+
const focusable = find( node );
187+
expect( focusable ).toHaveLength( 1 );
188+
expect( focusable[ 0 ] ).toBe( visibleInput );
189+
} );
160190
} );
161191
} );

0 commit comments

Comments
 (0)