From b07dbf61e0cbfca5af5dde5e6f7638a02b0867a8 Mon Sep 17 00:00:00 2001 From: John <309310+johnemau@users.noreply.github.com> Date: Tue, 14 Jan 2020 15:02:24 -0800 Subject: [PATCH] report: close drop down menu when focus is lost (#10208) --- .../html/renderer/report-ui-features.js | 18 +++++++++ .../html/renderer/report-ui-features-test.js | 38 +++++++++++++++++++ 2 files changed, 56 insertions(+) diff --git a/lighthouse-core/report/html/renderer/report-ui-features.js b/lighthouse-core/report/html/renderer/report-ui-features.js index aecd1be83d0a..50e543f0cd03 100644 --- a/lighthouse-core/report/html/renderer/report-ui-features.js +++ b/lighthouse-core/report/html/renderer/report-ui-features.js @@ -628,6 +628,7 @@ class DropDown { this.onDocumentKeyDown = this.onDocumentKeyDown.bind(this); this.onToggleClick = this.onToggleClick.bind(this); this.onToggleKeydown = this.onToggleKeydown.bind(this); + this.onMenuFocusOut = this.onMenuFocusOut.bind(this); this.onMenuKeydown = this.onMenuKeydown.bind(this); this._getNextMenuItem = this._getNextMenuItem.bind(this); @@ -655,6 +656,7 @@ class DropDown { // Refocus on the tools button if the drop down last had focus this._toggleEl.focus(); } + this._menuEl.removeEventListener('focusout', this.onMenuFocusOut); this._dom.document().removeEventListener('keydown', this.onDocumentKeyDown); } @@ -674,6 +676,7 @@ class DropDown { this._toggleEl.classList.add('active'); this._toggleEl.setAttribute('aria-expanded', 'true'); + this._menuEl.addEventListener('focusout', this.onMenuFocusOut); this._dom.document().addEventListener('keydown', this.onDocumentKeyDown); } @@ -752,6 +755,21 @@ class DropDown { } } + /** + * Focus out handler for the drop down menu. + * @param {Event} e + */ + onMenuFocusOut(e) { + // TODO: The focusout event is not supported in our current version of typescript (3.5.3) + // https://github.com/microsoft/TypeScript/issues/30716 + const focusEvent = /** @type {FocusEvent} */ (e); + const focusedEl = /** @type {?HTMLElement} */ (focusEvent.relatedTarget); + + if (!this._menuEl.contains(focusedEl)) { + this.close(); + } + } + /** * @param {Array} allNodes * @param {?Node=} startNode diff --git a/lighthouse-core/test/report/html/renderer/report-ui-features-test.js b/lighthouse-core/test/report/html/renderer/report-ui-features-test.js index 3c1057cc99c2..c29c2a6e6d4d 100644 --- a/lighthouse-core/test/report/html/renderer/report-ui-features-test.js +++ b/lighthouse-core/test/report/html/renderer/report-ui-features-test.js @@ -412,6 +412,44 @@ describe('ReportUIFeatures', () => { assert.strictEqual(nextNode, nodes[2]); }); }); + + describe('onMenuFocusOut', () => { + beforeEach(() => { + dropDown._toggleEl.click(); + assert.ok(dropDown._toggleEl.classList.contains('active')); + }); + + it('should toggle active class when focus relatedTarget is null', () => { + const event = new window.FocusEvent('focusout', {relatedTarget: null}); + dropDown.onMenuFocusOut(event); + + assert.ok(!dropDown._toggleEl.classList.contains('active')); + }); + + it('should toggle active class when focus relatedTarget is document.body', () => { + const relatedTarget = dom.document().body; + const event = new window.FocusEvent('focusout', {relatedTarget}); + dropDown.onMenuFocusOut(event); + + assert.ok(!dropDown._toggleEl.classList.contains('active')); + }); + + it('should toggle active class when focus relatedTarget is _toggleEl', () => { + const relatedTarget = dropDown._toggleEl; + const event = new window.FocusEvent('focusout', {relatedTarget}); + dropDown.onMenuFocusOut(event); + + assert.ok(!dropDown._toggleEl.classList.contains('active')); + }); + + it('should not toggle active class when focus relatedTarget is a menu item', () => { + const relatedTarget = dropDown._getNextMenuItem(); + const event = new window.FocusEvent('focusout', {relatedTarget}); + dropDown.onMenuFocusOut(event); + + assert.ok(dropDown._toggleEl.classList.contains('active')); + }); + }); }); describe('data-i18n', () => {