-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #131 from medialize/issue/121-element-focus
closes #121
- Loading branch information
Showing
26 changed files
with
225 additions
and
134 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
--- | ||
layout: doc-api.html | ||
tags: argument-list | ||
--- | ||
|
||
# ally.element.focus | ||
|
||
Shifts focus to an element if it does not already have focus. | ||
|
||
|
||
## Description | ||
|
||
The [`focus()`](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/focus) method is available on all [`HTMLElement`](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement). Considering the following HTML, applying `focus()` to the `<span>` element does not do anything. `ally.element.focus()` uses [`ally.get.focusTarget`](../get/focus-target.md) to identify the nearest focusable ancestor, in this case the `<a>`, and shifts focus to that. | ||
|
||
```html | ||
<a href="#victim"> | ||
<span>click me</span> | ||
</a> | ||
``` | ||
|
||
While `focus()` is available on `HTMLElement`, this is not necessarily true for [`SVGElement`](https://developer.mozilla.org/en-US/docs/Web/API/SVGElement). Only Blink and WebKit expose the `focus()` method on SVG elements, Gecko, Trident and Edge do not. This will likely change once [SVG 2](http://www.w3.org/TR/SVG2/interact.html#Focus) is a thing. Until then `ally.element.focus()` tries to apply `HTMLElement.prototoype.focus` to `SVGElement`s. This only works for Internet Explorer 9 - 11, as Gecko and Edge actively prevent this. | ||
|
||
|
||
## Usage | ||
|
||
```js | ||
var element = document.getElementById('victim'); | ||
var result = ally.element.focus(element); | ||
``` | ||
|
||
### Arguments | ||
|
||
| Name | Type | Default | Description | | ||
| ---- | ---- | ------- | ----------- | | ||
| element | [`HTMLElement`](https://developer.mozilla.org/en/docs/Web/API/HTMLElement) | *required* | The Element to focus | | ||
|
||
### Returns | ||
|
||
[`HTMLElement`](https://developer.mozilla.org/en/docs/Web/API/HTMLElement) that received focus or `null` if focus could not be shifted. | ||
|
||
### Throws | ||
|
||
[`TypeError`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypeError) if `element` argument is not of type `HTMLElement`. | ||
|
||
|
||
## Examples | ||
|
||
|
||
## Changes | ||
|
||
* Added in `v#master`. | ||
|
||
|
||
## Notes | ||
|
||
|
||
## Related resources | ||
|
||
|
||
## Contributing | ||
|
||
* [module source](https://github.com/medialize/ally.js/blob/master/src/element/focus.js) | ||
* [document source](https://github.com/medialize/ally.js/blob/master/docs/api/element/focus.md) | ||
* [unit test](https://github.com/medialize/ally.js/blob/master/test/unit/element.focus.test.js) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
|
||
// wrapper for HTMLElement.prototype.focus | ||
|
||
import getFocusTarget from '../get/focus-target'; | ||
import isActiveElement from '../is/active-element'; | ||
import contextToElement from '../util/context-to-element'; | ||
import getWindow from '../util/get-window'; | ||
|
||
export default function(context) { | ||
const element = contextToElement({ | ||
label: 'element/focus', | ||
context, | ||
}); | ||
|
||
const target = getFocusTarget({ | ||
context: element, | ||
except: { | ||
// exclude elements affected by the IE flexbox bug | ||
flexbox: true, | ||
// exclude overflowing elements that are not intentionally | ||
// made focusable by adding a tabindex attribute | ||
scrollable: true, | ||
// include elements that don't have a focus() method | ||
onlyTabbable: true, | ||
}, | ||
}); | ||
|
||
if (!target) { | ||
return null; | ||
} | ||
|
||
if (isActiveElement(target)) { | ||
return target; | ||
} | ||
|
||
if (target.focus) { | ||
target.focus(); | ||
return isActiveElement(target) ? target : null; | ||
} | ||
|
||
const _window = getWindow(target); | ||
|
||
try { | ||
// The element itself does not have a focus method. | ||
// This is true for SVG elements in Firefox and IE, | ||
// as well as MathML elements in every browser. | ||
// IE9 - 11 will let us abuse HTMLElement's focus method, | ||
// Firefox and Edge will throw an error. | ||
_window.HTMLElement.prototype.focus.call(target); | ||
return target; | ||
} catch (e) { | ||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.