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

Close modals when clicking outside of them #3578

Merged
merged 1 commit into from
Oct 22, 2019

Conversation

schlessera
Copy link
Collaborator

Summary

This PR adds a set of AMP actions to the <amp-lightbox> element that is used to replace the TwentyTwenty modals. Basically, the <amp-lightbox> is yet another "toggle" to close the modal.

As there's another .modal-inner element that contains all the actionable elements of the modal including its container, the <amp-lightbox> is effectively the background in the AMP context.

This PR offered a few technical challenges that made the solution non-trivial and required adding two helper methods:

  • AMP_DOM_Utils::copy_attributes() is used to copy a set of attributes from one element to the other. This helper method makes it easier in our case to move all the relevant bits from the original modal element to the <amp-lightbox> replacements. This was previously just a class attribute, but is now the following attributes instead: 'class', 'on' (for moving the actions over), 'data-toggle-target' (which is needed for the generic toggle functionality to know what to target).
  • AMP_DOM_Utils::merge_amp_actions() is added (and used as part of AMP_DOM_Utils::add_amp_action()) to allow for smart deduplication of AMP actions. This is now needed as the <amp-lightbox> is turned into a toggle as well, while already being a modal. As the toggle functionality adds toggleClass() actions for the 'active' class on both toggles and their modals, we ended up with a duplicate toggle on the modal, which cancel each other out. The deduplication in merge_amp_actions() makes sure this doesn't happen.

Both helper methods are generic tools that prove to be useful and come with tests, so they were added to AMP_DOM_Utils, and not as internal helpers for the AMP_Core_Theme_Sanitizer.

Fixes #3570

Checklist

  • My pull request is addressing an open issue (please create one otherwise).
  • My code is tested and passes existing tests.
  • My code follows the Engineering Guidelines (updates are often made to the guidelines, check it out periodically).

@schlessera schlessera added Bug Something isn't working Sanitizers labels Oct 21, 2019
@googlebot googlebot added the cla: yes Signed the Google CLA label Oct 21, 2019
@westonruter westonruter modified the milestones: v.1.4.1, v1.4 Oct 21, 2019
Copy link
Contributor

@kienstra kienstra left a comment

Choose a reason for hiding this comment

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

Looks Good!

Hi @schlessera,
Wow, this has such great and thorough tests.

And closing the search modal in Twenty Twenty worked as expected:

clicking-modal

'on' => 'event:action;other-event:other-action',
'some-attribute' => 'value-a',
],
],
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice, very thorough testing.

Copy link
Contributor

@pierlon pierlon Oct 22, 2019

Choose a reason for hiding this comment

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

Tests cover all the possible scenarios I can think of, and are thorough.

* @param DOMElement $to DOM element to copy the attributes to.
* @param string $default_separator Default separator to use for multiple values if the attribute is not known.
*/
public static function copy_attributes( $attributes, DOMElement $from, DOMElement $to, $default_separator = ',' ) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Good idea to create this function.

Copy link
Contributor

@pierlon pierlon left a comment

Choose a reason for hiding this comment

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

Excellent work overall; it works exactly as described 👌.

'on' => 'event:action;other-event:other-action',
'some-attribute' => 'value-a',
],
],
Copy link
Contributor

@pierlon pierlon Oct 22, 2019

Choose a reason for hiding this comment

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

Tests cover all the possible scenarios I can think of, and are thorough.

@westonruter westonruter merged commit e9aa2f3 into develop Oct 22, 2019
@westonruter westonruter deleted the fix/3570-close-modal-when-clicking-outside branch October 22, 2019 04:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Something isn't working cla: yes Signed the Google CLA Sanitizers
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Clicking outside of a TwentyTwenty modal does not close the modal
5 participants