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

Fix modal escape key propagation #17297

Merged
merged 5 commits into from
Sep 4, 2019
Merged

Conversation

talldan
Copy link
Contributor

@talldan talldan commented Sep 2, 2019

Description

Modal components handle an escape key down as a global event. Unfortunately this means that the modal cannot stop propagation of an event to the outer context. It can only cancel bubbling to an inner context.

With our current implementation of modals, this hasn't really been an issue. On #17265 I'm implementing a modal that's launched from a block toolbar, and pressing esc to close the modal is triggering navigation mode.

This change in this PR is to stop using a global event and instead use an event on the IsolatedEventContainer component. This matches how the Popover component is implemented:

<IsolatedEventContainer
className={ classnames( classes, animateClassName ) }
style={ {
top: ! popoverPosition.isMobile && popoverPosition.popoverTop ? popoverPosition.popoverTop + 'px' : undefined,
left: ! popoverPosition.isMobile && popoverPosition.popoverLeft ? popoverPosition.popoverLeft + 'px' : undefined,
visibility: contentSize ? undefined : 'hidden',
} }
{ ...contentProps }
onKeyDown={ maybeClose }
>

// Event handlers
const maybeClose = ( event ) => {
// Close on escape
if ( event.keyCode === ESCAPE && onClose ) {
event.stopPropagation();
onClose();
}
// Preserve original content prop behavior
if ( onKeyDown ) {
onKeyDown( event );
}
};

This PR also moves the IsolatedEventContainer component into ModalFrame. This component is responsible for rendering the overlay behind the modal. Nesting it inside withFocusReturn ensures that when the overlay is clicked, focus is returned to the element that opened the modal.

How has this been tested?

Test that modals:

  • Close when pressing escape. Focus should be returned to the element that opened the modal (or as close to the element as possible).
  • Close clicking the close button. Focus should be returned to the element that opened the modal (or as close to the element as possible).
  • Close when clicking outside. Focus should be returned to the element that opened the modal (or as close to the element as possible).

Screenshots

Types of changes

Bug fix (non-breaking change which fixes an issue)

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.

@talldan talldan self-assigned this Sep 2, 2019
@talldan talldan changed the title Fix modal escape key propagation WIP Fix modal escape key propagation Sep 2, 2019
@talldan talldan added [Package] Components /packages/components [Type] Bug An existing feature does not function as intended [Feature] UI Components Impacts or related to the UI component system labels Sep 2, 2019
}

/**
* Stops proagation of the escape key outside the context of the modal.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fix typo and describe that it triggers the onRequestClose prop.

@gziolo
Copy link
Member

gziolo commented Sep 3, 2019

Noting that I have a similar issue in #16964.

@talldan talldan force-pushed the fix/modal-escape-key-propagation branch from 8853deb to 64ef592 Compare September 3, 2019 08:40
@talldan talldan force-pushed the fix/modal-escape-key-propagation branch from 64ef592 to d10fa38 Compare September 3, 2019 08:42
@talldan talldan marked this pull request as ready for review September 3, 2019 08:59
@talldan talldan changed the title WIP Fix modal escape key propagation Fix modal escape key propagation Sep 3, 2019
@talldan talldan mentioned this pull request Sep 3, 2019
5 tasks
@talldan talldan added the [Focus] Accessibility (a11y) Changes that impact accessibility and need corresponding review (e.g. markup changes). label Sep 3, 2019
Copy link
Member

@gziolo gziolo left a comment

Choose a reason for hiding this comment

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

This worked great with the previous commit when I was testing earlier today. Code looks great with all refactorings applied and it solved the issue I had with the PR I linked in my previous comment.

I'll try to retest this PR later today. I had some very minor question which isn't a blocker.

packages/components/src/modal/index.js Show resolved Hide resolved
@gziolo
Copy link
Member

gziolo commented Sep 3, 2019

Re-tested again. I can confirm it all works as advertised. Excellent job @talldan 🎉

@talldan talldan merged commit 2b7b693 into master Sep 4, 2019
@talldan talldan deleted the fix/modal-escape-key-propagation branch September 4, 2019 04:22
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 [Focus] Accessibility (a11y) Changes that impact accessibility and need corresponding review (e.g. markup changes). [Package] Components /packages/components [Type] Bug An existing feature does not function as intended
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants