Skip to content

Commit

Permalink
Writing flow: fix vertical arrow nav in table (and generally grid) (#…
Browse files Browse the repository at this point in the history
…22105)

* Writing flow: fix arrow nav in table (and generally gird)

* Add e2e test

* fix columns e2e test
  • Loading branch information
ellatrix authored May 5, 2020
1 parent 464026a commit 433236b
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 21 deletions.
30 changes: 28 additions & 2 deletions packages/block-editor/src/components/writing-flow/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,18 @@ export function isNavigationCandidate( element, keyCode, hasModifier ) {
* @param {Element} target Currently focused text field.
* @param {boolean} isReverse True if considering as the first field.
* @param {Element} containerElement Element containing all blocks.
* @param {boolean} onlyVertical Wether to only consider tabbable elements
* that are visually above or under the
* target.
*
* @return {?Element} Optimal tab target, if one exists.
*/
export function getClosestTabbable( target, isReverse, containerElement ) {
export function getClosestTabbable(
target,
isReverse,
containerElement,
onlyVertical
) {
// Since the current focus target is not guaranteed to be a text field,
// find all focusables. Tabbability is considered later.
let focusableNodes = focus.focusable.find( containerElement );
Expand All @@ -112,12 +120,29 @@ export function getClosestTabbable( target, isReverse, containerElement ) {
focusableNodes.indexOf( target ) + 1
);

let targetRect;

if ( onlyVertical ) {
targetRect = target.getBoundingClientRect();
}

function isTabCandidate( node, i, array ) {
// Not a candidate if the node is not tabbable.
if ( ! focus.tabbable.isTabbableIndex( node ) ) {
return false;
}

if ( onlyVertical ) {
const nodeRect = node.getBoundingClientRect();

if (
nodeRect.left >= targetRect.right ||
nodeRect.right <= targetRect.left
) {
return false;
}
}

// Prefer text fields...
if ( isTextField( node ) ) {
return true;
Expand Down Expand Up @@ -517,7 +542,8 @@ export default function WritingFlow( { children } ) {
const closestTabbable = getClosestTabbable(
target,
isReverse,
container.current
container.current,
true
);

if ( closestTabbable ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,9 @@ exports[`Table displays a form for choosing the row and column count of the tabl
<figure class=\\"wp-block-table\\"><table><tbody><tr><td></td><td></td><td></td><td></td><td></td></tr><tr><td></td><td></td><td></td><td></td><td></td></tr><tr><td></td><td></td><td></td><td></td><td></td></tr><tr><td></td><td></td><td></td><td></td><td></td></tr><tr><td></td><td></td><td></td><td></td><td></td></tr><tr><td></td><td></td><td></td><td></td><td></td></tr><tr><td></td><td></td><td></td><td></td><td></td></tr><tr><td></td><td></td><td></td><td></td><td></td></tr><tr><td></td><td></td><td></td><td></td><td></td></tr><tr><td></td><td></td><td></td><td></td><td></td></tr></tbody></table></figure>
<!-- /wp:table -->"
`;
exports[`Table up and down arrow navigation 1`] = `
"<!-- wp:table -->
<figure class=\\"wp-block-table\\"><table><tbody><tr><td>1</td><td>4</td></tr><tr><td>2</td><td>3</td></tr></tbody></table></figure>
<!-- /wp:table -->"
`;
18 changes: 18 additions & 0 deletions packages/e2e-tests/specs/editor/blocks/table.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -267,4 +267,22 @@ describe( 'Table', () => {

expect( await getEditedPostContent() ).toMatchSnapshot();
} );

it( 'up and down arrow navigation', async () => {
await insertBlock( 'Table' );

// Create the table.
await clickButton( createButtonLabel );

await page.keyboard.press( 'Tab' );
await page.keyboard.type( '1' );
await page.keyboard.press( 'ArrowDown' );
await page.keyboard.type( '2' );
await page.keyboard.press( 'ArrowRight' );
await page.keyboard.type( '3' );
await page.keyboard.press( 'ArrowUp' );
await page.keyboard.type( '4' );

expect( await getEditedPostContent() ).toMatchSnapshot();
} );
} );
20 changes: 1 addition & 19 deletions packages/e2e-tests/specs/editor/various/writing-flow.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,25 +80,7 @@ describe( 'Writing Flow', () => {
activeBlockName = await getActiveBlockName();
expect( activeBlockName ).toBe( 'core/column' );
await page.keyboard.press( 'ArrowUp' );
activeBlockName = await getActiveBlockName();
expect( activeBlockName ).toBe( 'core/paragraph' );
activeElementText = await page.evaluate(
() => document.activeElement.textContent
);
expect( activeElementText ).toBe( '1st col' );

// Arrow up from first text field in nested context focuses column and
// columns wrappers before escaping out.
let activeElementBlockType;
await page.keyboard.press( 'ArrowUp' );
activeElementBlockType = await page.evaluate( () =>
document.activeElement.getAttribute( 'data-type' )
);
expect( activeElementBlockType ).toBe( 'core/column' );
activeBlockName = await getActiveBlockName();
expect( activeBlockName ).toBe( 'core/column' );
await page.keyboard.press( 'ArrowUp' );
activeElementBlockType = await page.evaluate( () =>
const activeElementBlockType = await page.evaluate( () =>
document.activeElement.getAttribute( 'data-type' )
);
expect( activeElementBlockType ).toBe( 'core/columns' );
Expand Down

0 comments on commit 433236b

Please sign in to comment.