Content-Length: 322683 | pFad | http://github.com/jsor/domestique/commit/577f469d926de56047f64553420b9a4576a7e7c0

79 Add activeElement() helper · jsor/domestique@577f469 · GitHub
Skip to content

Commit

Permalink
Add activeElement() helper
Browse files Browse the repository at this point in the history
  • Loading branch information
jsor committed Aug 13, 2018
1 parent 0f7991d commit 577f469
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 0 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import {
viewportWidth,

// Element
activeElement,
create,
addClass,
removeClass,
Expand Down Expand Up @@ -74,6 +75,7 @@ API
* [viewportHeight()](#viewportheight)
* [viewportWidth()](#viewportwidth)
* [Element](#element)
* [activeElement()](#activeelement)
* [create()](#create)
* [addClass()](#addclass)
* [removeClass()](#removeclass)
Expand Down Expand Up @@ -158,6 +160,20 @@ const vpWidth = viewportWidth();

### Element

#### activeElement()

```
activeElement(): Element
```

Returns the element that currently has focus.

##### Example

```javascript
const element = activeElement();
```

#### create()

```
Expand Down
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import scrollbarSize from './src/dimension/scrollbar-size';
import viewportHeight from './src/dimension/viewport-height';
import viewportWidth from './src/dimension/viewport-width';

import activeElement from './src/element/active-element';
import create from './src/element/create';
import {addClass, hasClass, removeClass} from './src/element/class';
import data from './src/element/data';
Expand All @@ -25,6 +26,7 @@ export {
viewportWidth,

// Element
activeElement,
create,
addClass,
removeClass,
Expand Down
14 changes: 14 additions & 0 deletions src/element/active-element.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
* Ported from jQuery UI's safeActiveElement()
* https://github.com/jquery/jquery-ui/blob/74f8a0ac952f6f45f773312292baef1c26d81300/ui/safe-active-element.js
*/

export default function activeElement() {
try {
const {activeElement} = document;

return activeElement && activeElement.nodeName ? activeElement : document.body;
} catch (e) {
return document.body;
}
}
21 changes: 21 additions & 0 deletions test/element/active-element.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { activeElement } from '../../index';

describe('activeElement()', () => {
it('returns the active element', () => {
const element = document.createElement('button');

document.body.appendChild(element);

element.focus();

assert.equal(activeElement(), element);

document.body.removeChild(element);
});

it('always returns active element', () => {
document.activeElement && document.activeElement.blur();

assert.equal(activeElement(), document.body);
});
});

0 comments on commit 577f469

Please sign in to comment.








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/jsor/domestique/commit/577f469d926de56047f64553420b9a4576a7e7c0

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy