Skip to content

Commit

Permalink
Restore focus on focusout
Browse files Browse the repository at this point in the history
  • Loading branch information
tomivirkki committed Nov 6, 2019
1 parent e95d4f0 commit ff3547b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/vaadin-overlay.html
Expand Up @@ -405,6 +405,18 @@
// and <vaadin-context-menu>).
this.addEventListener('click', () => {});
this.$.backdrop.addEventListener('click', () => {});

// Make sure the focus stays within the overlay (even on caret browsing)
this.addEventListener('focusout', e => {
const related = e.relatedTarget;
if (this.focusTrap && (!related || !this._deepContains(related))) {
setTimeout(() => {
this.tabIndex = -1;
this.focus();
this.tabIndex = undefined;
});
}
});
}

_detectIosNavbar() {
Expand Down
19 changes: 19 additions & 0 deletions test/focus-trap.html
Expand Up @@ -167,6 +167,25 @@
overlay.opened = true;
});

it('should restore focus on focusout', done => {
const spy = sinon.spy(overlay, 'focus');
overlay.dispatchEvent(new CustomEvent('focusout'));
setTimeout(() => {
expect(spy.called).to.be.true;
done();
});
});

it('should not restore focus on focusout if not a focustrap', done => {
overlay.focusTrap = false;
const spy = sinon.spy(overlay, 'focus');
overlay.dispatchEvent(new CustomEvent('focusout'));
setTimeout(() => {
expect(spy.called).to.be.false;
done();
});
});

it('should properly detect focusable elements inside the content', () => {
expect(focusableElements.length).to.eql(11);
expect(focusableElements[0]).to.eql(overlay.content.querySelector('textarea'));
Expand Down

0 comments on commit ff3547b

Please sign in to comment.