Skip to content

Commit

Permalink
Merge pull request #16 from Brightspace/dbatiste/get-composed-active-…
Browse files Browse the repository at this point in the history
…element

Adding method to get the composed activeElement.
  • Loading branch information
dbatiste authored Jan 18, 2018
2 parents e1ec6ae + 86d2a01 commit ca03a7c
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ D2L.Dom.isComposedAncestor(ancestorNode, node);
**D2L.Dom.Focus**

```javascript
// get the composed active element (i.e. the actual element that has focus)
D2L.Dom.Focus.getComposedActiveElement();

// get first focusable child or descendant
D2L.Dom.Focus.getFirstFocusableDescendant(element);

Expand Down
12 changes: 12 additions & 0 deletions d2l-dom-focus.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,18 @@
textarea: true
},

getComposedActiveElement: function() {
var node = document.activeElement;
while (node.shadowRoot) {
if (node.shadowRoot.activeElement) {
node = node.shadowRoot.activeElement;
} else {
break;
}
}
return node;
},

getFirstFocusableDescendant: function(node, includeHidden) {

var composedChildren = D2L.Dom.getComposedChildren(node);
Expand Down
18 changes: 18 additions & 0 deletions test/dom-focus.html
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,24 @@
mixedFixture = fixture('mixedFixture');
});

describe('getComposedActiveElement', function() {

it('returns composed light-dom element', function() {
var expected = wcFixture.querySelector('#light1');
expected.focus();
expect(D2L.Dom.Focus.getComposedActiveElement())
.to.equal(expected);
});

it('returns composed shadow-dom element', function() {
var expected = wcFixture.getShadow1();
expected.focus();
expect(D2L.Dom.Focus.getComposedActiveElement())
.to.equal(expected);
});

});

describe('getFirstFocusableDescendant', function() {

it('returns focusable child', function() {
Expand Down

0 comments on commit ca03a7c

Please sign in to comment.