Skip to content

Commit

Permalink
report: close drop down menu when focus is lost (#10208)
Browse files Browse the repository at this point in the history
  • Loading branch information
johnemau authored and patrickhulce committed Jan 14, 2020
1 parent 56d1840 commit b07dbf6
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
18 changes: 18 additions & 0 deletions lighthouse-core/report/html/renderer/report-ui-features.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}

Expand All @@ -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);
}

Expand Down Expand Up @@ -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<Node>} allNodes
* @param {?Node=} startNode
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down

0 comments on commit b07dbf6

Please sign in to comment.