Skip to content

Fix AlignmentMatrixControl focus issue#27945

Merged
diegohaz merged 3 commits intoWordPress:masterfrom
rmorse:update/alignmentMatrix
Jan 5, 2021
Merged

Fix AlignmentMatrixControl focus issue#27945
diegohaz merged 3 commits intoWordPress:masterfrom
rmorse:update/alignmentMatrix

Conversation

@rmorse
Copy link
Copy Markdown
Contributor

@rmorse rmorse commented Dec 31, 2020

Description

This fixes a bug in the AlignmentMatrixControl where simply rendering a control triggers a focus event which in turn triggers a change event / state change.

To replicate

  • add a new cover block, change the alignment (eg, bottom right) then close the control / block
  • re-select the block, and click the button to open the alignment matrix again
  • Notice it immediately resets itself to top left (with a state change)

After digging around I realised this was to do with the fact the onChange event is triggered by onFocus of the cell.

I think due to the way the reakit components work (I'm not too familiar), when rendering, the first element is focussed immediately/automatically - in this case top left.

Looking at their docs, I see examples of a CompositeItem using onClick - so I tried with that and it works.

What this does "break" is:

  • Previously when the control is visible (after pressing the button to show it), any keyboard arrow presses would immediately trigger a state change and update the cover attributes
  • Now, navigating with a keyboard doesn't commit any change to the block until enter has been pressed

Personally, I think this still feels natural, but I think it may need a bit of a rethink in terms of UI - perhaps, we have a "focussed" color state for when the keyboard is in the process of selecting an alignment, and when it is selected, we get the larger, black square we currently have.

How has this been tested?

By opening the control an observing behaviour to mouse + keyboard events

Screenshots

Before:
align-matrix-issue

After:
align-matrix-issue-after

Types of changes

This is a 1 line bug fix that will only have a small negative effect for keyboard users - and fixes a bug that causes a state change whenever the component is rendered.

Checklist:

  • My code is tested.
  • My code follows the WordPress code style.
  • My code follows the accessibility standards.
  • My code has proper inline documentation.
  • I've included developer documentation if appropriate.
  • I've updated all React Native files affected by any refactorings/renamings in this PR.

@youknowriad youknowriad added [Feature] UI Components Impacts or related to the UI component system [Type] Bug An existing feature does not function as intended labels Jan 1, 2021
Copy link
Copy Markdown
Member

@diegohaz diegohaz left a comment

Choose a reason for hiding this comment

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

Thanks, @rmorse!

With #27699, the useFocusOnMount hook — the popover uses that — has been changed so it looks for tabbable elements right away when the popover element is mounted. At this point, in the very first render, all the composite items have tabIndex="0" (in a second render, only the active item will have tabIndex="0"). That's why the first item is receiving focus even when it's not the active item.

To keep the current behavior (reflecting the change on the UI when navigating with the keyboard), I suggest we force the tabIndex value instead of using onClick.

@rmorse
Copy link
Copy Markdown
Contributor Author

rmorse commented Jan 3, 2021

@diegohaz - ah yes that makes better sense :) thanks!

I've tested and works as expected, fixing the intial issue without compromising on the keyboard functionality.

Copy link
Copy Markdown
Member

@diegohaz diegohaz left a comment

Choose a reason for hiding this comment

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

This looks good! Thanks! The failing test is not related to this change and I think was fixed on master already.

@diegohaz
Copy link
Copy Markdown
Member

diegohaz commented Jan 5, 2021

@rmorse Can you please merge master into your branch?

@rmorse
Copy link
Copy Markdown
Contributor Author

rmorse commented Jan 5, 2021

@diegohaz done :)

@diegohaz diegohaz merged commit 470e644 into WordPress:master Jan 5, 2021
@github-actions github-actions bot added this to the Gutenberg 9.8 milestone Jan 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Feature] UI Components Impacts or related to the UI component system [Type] Bug An existing feature does not function as intended

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants