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: Fix focus trap on certain input types #41538

Merged
merged 3 commits into from
Jun 8, 2022
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
4 changes: 4 additions & 0 deletions packages/block-editor/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### Bug fix

- Fix focus trap on certain `input` elements when navigating within a block with the left/right arrow keys ([#41538](https://github.com/WordPress/gutenberg/pull/41538)).

## 9.2.0 (2022-06-01)

## 9.1.0 (2022-05-18)
Expand Down
26 changes: 22 additions & 4 deletions packages/block-editor/src/components/writing-flow/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,21 @@ describe( 'isNavigationCandidate', () => {
let elements;
beforeAll( () => {
elements = {};
elements.input = document.createElement( 'input' );

elements.inputText = document.createElement( 'input' );
elements.inputText.setAttribute( 'type', 'text' );

elements.inputCheckbox = document.createElement( 'input' );
elements.inputCheckbox.setAttribute( 'type', 'checkbox' );

elements.contentEditable = document.createElement( 'p' );
elements.contentEditable.contentEditable = true;
} );

it( 'should return true if vertically navigating input without modifier', () => {
[ UP, DOWN ].forEach( ( keyCode ) => {
const result = isNavigationCandidate(
elements.input,
elements.inputText,
keyCode,
false
);
Expand All @@ -32,7 +38,7 @@ describe( 'isNavigationCandidate', () => {
it( 'should return false if vertically navigating input with modifier', () => {
[ UP, DOWN ].forEach( ( keyCode ) => {
const result = isNavigationCandidate(
elements.input,
elements.inputText,
keyCode,
true
);
Expand All @@ -44,7 +50,7 @@ describe( 'isNavigationCandidate', () => {
it( 'should return false if horizontally navigating input', () => {
[ LEFT, RIGHT ].forEach( ( keyCode ) => {
const result = isNavigationCandidate(
elements.input,
elements.inputText,
keyCode,
false
);
Expand All @@ -53,6 +59,18 @@ describe( 'isNavigationCandidate', () => {
} );
} );

it( 'should return true if horizontally navigating simple inputs like checkboxes', () => {
[ LEFT, RIGHT ].forEach( ( keyCode ) => {
const result = isNavigationCandidate(
elements.inputCheckbox,
keyCode,
false
);

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

it( 'should return true if horizontally navigating non-input', () => {
[ LEFT, RIGHT ].forEach( ( keyCode ) => {
const result = isNavigationCandidate(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,25 @@ export function isNavigationCandidate( element, keyCode, hasModifier ) {
return true;
}

// Native inputs should not navigate horizontally.
const { tagName } = element;
return tagName !== 'INPUT' && tagName !== 'TEXTAREA';

// 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',
'color',
'file',
'image',
'radio',
'reset',
'submit',
];
return simpleInputTypes.includes( element.getAttribute( 'type' ) );
}

// Native textareas should not navigate horizontally.
return tagName !== 'TEXTAREA';
}

/**
Expand Down