Skip to content

Commit fdc494a

Browse files
authored
Writing flow: Improve keyboard navigation on certain input types (#43667)
* Writing flow: Fix vertial focus trap on certain input types Add test Fix e2e test enable left/right native event on input number field Remove changes test test * Fix typo
1 parent 62b3b0e commit fdc494a

File tree

3 files changed

+33
-4
lines changed

3 files changed

+33
-4
lines changed

packages/block-editor/src/components/writing-flow/test/index.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ describe( 'isNavigationCandidate', () => {
1919
elements.inputCheckbox = document.createElement( 'input' );
2020
elements.inputCheckbox.setAttribute( 'type', 'checkbox' );
2121

22+
elements.inputNumber = document.createElement( 'input' );
23+
elements.inputNumber.setAttribute( 'type', 'number' );
24+
2225
elements.contentEditable = document.createElement( 'p' );
2326
elements.contentEditable.contentEditable = true;
2427
} );
@@ -47,6 +50,18 @@ describe( 'isNavigationCandidate', () => {
4750
} );
4851
} );
4952

53+
it( 'should return false if vertically navigating inputs with vertial support like number', () => {
54+
[ UP, DOWN ].forEach( ( keyCode ) => {
55+
const result = isNavigationCandidate(
56+
elements.inputNumber,
57+
keyCode,
58+
false
59+
);
60+
61+
expect( result ).toBe( false );
62+
} );
63+
} );
64+
5065
it( 'should return false if horizontally navigating input', () => {
5166
[ LEFT, RIGHT ].forEach( ( keyCode ) => {
5267
const result = isNavigationCandidate(

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

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,27 +32,40 @@ import { store as blockEditorStore } from '../../store';
3232
*/
3333
export function isNavigationCandidate( element, keyCode, hasModifier ) {
3434
const isVertical = keyCode === UP || keyCode === DOWN;
35+
const { tagName } = element;
36+
const elementType = element.getAttribute( 'type' );
3537

36-
// Currently, all elements support unmodified vertical navigation.
38+
// Native inputs should not navigate vertically, unless they are simple types that don't need up/down arrow keys.
3739
if ( isVertical && ! hasModifier ) {
40+
if ( tagName === 'INPUT' ) {
41+
const verticalInputTypes = [
42+
'date',
43+
'datetime-local',
44+
'month',
45+
'number',
46+
'range',
47+
'time',
48+
'week',
49+
];
50+
return ! verticalInputTypes.includes( elementType );
51+
}
3852
return true;
3953
}
4054

41-
const { tagName } = element;
42-
4355
// Native inputs should not navigate horizontally, unless they are simple types that don't need left/right arrow keys.
4456
if ( tagName === 'INPUT' ) {
4557
const simpleInputTypes = [
4658
'button',
4759
'checkbox',
60+
'number',
4861
'color',
4962
'file',
5063
'image',
5164
'radio',
5265
'reset',
5366
'submit',
5467
];
55-
return simpleInputTypes.includes( element.getAttribute( 'type' ) );
68+
return simpleInputTypes.includes( elementType );
5669
}
5770

5871
// Native textareas should not navigate horizontally.

test/e2e/specs/editor/various/toolbar-roving-tabindex.spec.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ test.describe( 'Toolbar roving tabindex', () => {
9696
pageUtils,
9797
} ) => {
9898
await editor.insertBlock( { name: 'core/table' } );
99+
await page.keyboard.press( 'ArrowLeft' );
99100
await ToolbarRovingTabindexUtils.testBlockToolbarKeyboardNavigation(
100101
'Block: Table',
101102
'Table'

0 commit comments

Comments
 (0)