Skip to content

Commit

Permalink
🐛Adds capability for closing story share menu on escape key press. (#…
Browse files Browse the repository at this point in the history
…23744)

* adding files

* added comment in test

* adding files

* added comment in test

* fixes lint errors

* resolves merge

* fixes linter error

* adds proper path for Keys import

* fixes unit test
  • Loading branch information
johannaperez authored and newmuis committed Aug 9, 2019
1 parent 4e6d1a5 commit 5970a1e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
11 changes: 9 additions & 2 deletions extensions/amp-story/1.0/amp-story-share-menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
getStoreService,
} from './amp-story-store-service';
import {CSS} from '../../../build/amp-story-share-menu-1.0.css';
import {Keys} from '../../../src/utils/key-codes';
import {Services} from '../../../src/services';
import {ShareWidget} from './amp-story-share';
import {closest} from '../../../src/dom';
Expand Down Expand Up @@ -190,6 +191,13 @@ export class ShareMenu {
this.element_.addEventListener('click', event =>
this.onShareMenuClick_(event)
);

this.win_.addEventListener('keyup', event => {
if (event.key == Keys.ESCAPE) {
event.preventDefault();
this.close_();
}
});
}
}

Expand Down Expand Up @@ -221,13 +229,12 @@ export class ShareMenu {
/**
* Handles click events and maybe closes the menu for the fallback UI.
* @param {!Event} event
* @return {*} TODO(#23582): Specify return type
*/
onShareMenuClick_(event) {
const el = dev().assertElement(event.target);

if (el.classList.contains('i-amphtml-story-share-menu-close-button')) {
return this.close_();
this.close_();
}

// Closes the menu if click happened outside of the menu main container.
Expand Down
16 changes: 16 additions & 0 deletions extensions/amp-story/1.0/test/test-amp-story-share-menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
AmpStoryStoreService,
StateProperty,
} from '../amp-story-store-service';
import {Keys} from '../../../../src/utils/key-codes';
import {LocalizationService} from '../../../../src/service/localization';
import {Services} from '../../../../src/services';
import {ShareMenu, VISIBLE_CLASS} from '../amp-story-share-menu';
Expand Down Expand Up @@ -130,6 +131,21 @@ describes.realWin('amp-story-share-menu', {amp: true}, env => {
expect(shareMenu.element_).to.have.class(VISIBLE_CLASS);
});

it('should hide the share menu on escape key press', () => {
shareMenu.build();

const clickCallbackSpy = sandbox.spy();
win.addEventListener('keyup', clickCallbackSpy);

storeService.dispatch(Action.TOGGLE_SHARE_MENU, true);
// Create escape keyup event.
const keyupEvent = new Event('keyup');
keyupEvent.keyCode = Keys.ESCAPE;
win.dispatchEvent(keyupEvent);

expect(clickCallbackSpy).to.have.been.calledOnce;
});

it('should render the amp-social-share button if system share', () => {
isSystemShareSupported = true;

Expand Down

0 comments on commit 5970a1e

Please sign in to comment.