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

Restore Inserter menu focus by tracking opening node #786

Merged
merged 3 commits into from
May 15, 2017

Conversation

aduth
Copy link
Member

@aduth aduth commented May 12, 2017

Related: #578

This pull request seeks to revise the Inserter Menu close-on-escape behavior to focus the original opening element by tracking the node when the opening toggle event occurs. A goal with this change is to eliminate ancestor access to a button's rendered node through its buttonRef prop (removed by the changes here).

Implementation notes:

event.currentTarget could be interchangeable with document.activeElement, but I chose the former since it feels more reliable to the knowledge that the current target is the rendered button we intend to refocus. The downside is that toggle must be used as an event callback (the only current usage).

Testing instructions:

Repeat testing instructions from #578.

@aduth aduth added [Focus] Accessibility (a11y) Changes that impact accessibility and need corresponding review (e.g. markup changes). [Feature] Inserter The main way to insert blocks using the + button in the editing interface labels May 12, 2017
@@ -19,7 +19,6 @@ function Button( { href, isPrimary, isLarge, isToggled, className, buttonRef, ..
...tagProps,
...additionalProps,
className: classes,
ref: buttonRef,
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

Copy link
Contributor

@youknowriad youknowriad left a comment

Choose a reason for hiding this comment

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

I like this change. The buttonRef prop was a bit weird. :shipit:

@@ -56,7 +56,7 @@ class InserterMenu extends wp.element.Component {
selectBlock( slug ) {
return () => {
this.props.onInsertBlock( slug );
this.props.onSelect();
this.props.closeMenu();
Copy link
Contributor

Choose a reason for hiding this comment

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

I think the cloneMenu prop should receive a nullable selected block (or slug) as a parameter (even if it's not used right now).

Copy link
Member Author

Choose a reason for hiding this comment

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

I think the cloneMenu prop should receive a nullable selected block (or slug) as a parameter (even if it's not used right now).

So consolidating to onSelect( slug: ?string ) in place of the onInsertBlock and closeMenu combo?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, or onClose( slug: ?string )

Copy link
Contributor

@BE-Webdesign BE-Webdesign left a comment

Choose a reason for hiding this comment

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

👍 This is cleaner for now. If we ever want to open the inserter via keyboard shortcut, we will need to revisit this, but for our current needs, this is a better solution.

@aduth
Copy link
Member Author

aduth commented May 15, 2017

If we ever want to open the inserter via keyboard shortcut, we will need to revisit this

Sure. The main objective with these changes it to push for components to be self-sufficient and not depend on particular DOM structures of other components. To this point, with keyboard shortcuts, it seems sensible that we'd expose this in a way that the component can define its intent to respond to global keyboard events (perhaps through a higher-order component), rather than have the keyboard event handler call on the inserter(s) directly.

@aduth aduth force-pushed the update/inserter-focus-on-close branch from 7f92bbb to 55829f1 Compare May 15, 2017 14:29
@aduth
Copy link
Member Author

aduth commented May 15, 2017

In the rebased c98a7bb, I consolidated further to a single onSelect prop which accepts a (nullable) slug of the block to insert.

I also found that shifting focus has a tricky gotcha which is to be exempt from click outside behavior; clicking a blank spot on the page shouldn't be forcing the button to be focused. This is accounted for in 55829f1 to only react to the escape key press (or, more accurately, the menu closing without a selected slug).

@aduth aduth merged commit d240601 into master May 15, 2017
@aduth aduth deleted the update/inserter-focus-on-close branch May 15, 2017 17:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Inserter The main way to insert blocks using the + button in the editing interface [Focus] Accessibility (a11y) Changes that impact accessibility and need corresponding review (e.g. markup changes).
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants