Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Writing flow: Improve keyboard navigation on certain input types #43667

Merged
merged 2 commits into from
May 23, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
15 changes: 15 additions & 0 deletions packages/block-editor/src/components/writing-flow/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ describe( 'isNavigationCandidate', () => {
elements.inputCheckbox = document.createElement( 'input' );
elements.inputCheckbox.setAttribute( 'type', 'checkbox' );

elements.inputNumber = document.createElement( 'input' );
elements.inputNumber.setAttribute( 'type', 'number' );

elements.contentEditable = document.createElement( 'p' );
elements.contentEditable.contentEditable = true;
} );
Expand Down Expand Up @@ -47,6 +50,18 @@ describe( 'isNavigationCandidate', () => {
} );
} );

it( 'should return false if vertically navigating inputs with vertial support like number', () => {
[ UP, DOWN ].forEach( ( keyCode ) => {
const result = isNavigationCandidate(
elements.inputNumber,
keyCode,
false
);

expect( result ).toBe( false );
} );
} );

it( 'should return false if horizontally navigating input', () => {
[ LEFT, RIGHT ].forEach( ( keyCode ) => {
const result = isNavigationCandidate(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,27 +32,40 @@ import { store as blockEditorStore } from '../../store';
*/
export function isNavigationCandidate( element, keyCode, hasModifier ) {
const isVertical = keyCode === UP || keyCode === DOWN;
const { tagName } = element;
const elementType = element.getAttribute( 'type' );

// Currently, all elements support unmodified vertical navigation.
// Native inputs should not navigate vertically, unless they are simple types that don't need up/down arrow keys.
if ( isVertical && ! hasModifier ) {
if ( tagName === 'INPUT' ) {
const vertiaclInputTypes = [
Copy link
Contributor

Choose a reason for hiding this comment

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

Variable typo here.

'date',
'datetime-local',
'month',
'number',
'range',
'time',
'week',
];
return ! vertiaclInputTypes.includes( elementType );
}
return true;
}

const { tagName } = element;

// Native inputs should not navigate horizontally, unless they are simple types that don't need left/right arrow keys.
if ( tagName === 'INPUT' ) {
const simpleInputTypes = [
'button',
'checkbox',
'number',
'color',
'file',
'image',
'radio',
'reset',
'submit',
];
return simpleInputTypes.includes( element.getAttribute( 'type' ) );
return simpleInputTypes.includes( elementType );
}

// Native textareas should not navigate horizontally.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ test.describe( 'Toolbar roving tabindex', () => {
pageUtils,
} ) => {
await editor.insertBlock( { name: 'core/table' } );
await page.keyboard.press( 'ArrowLeft' );
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Immediately after the table is inserted, the focus is on the input number field (Column Count).
Pressing the up or down key in this state will respect the upper limit of the number (which is a native event), so press the left key in advance to give focus to the block wrapper.

await ToolbarRovingTabindexUtils.testBlockToolbarKeyboardNavigation(
'Block: Table',
'Table'
Expand Down