aboutsummaryrefslogtreecommitdiffstats
path: root/src/js/utils/elements.js
diff options
context:
space:
mode:
authorSam Potts <sam@potts.es>2018-07-15 19:23:28 +1000
committerSam Potts <sam@potts.es>2018-07-15 19:23:28 +1000
commite63ad7c74bd763043344fd2bd568a64e7ea18622 (patch)
tree2367ed9ff7d5d14fd6a5d207550a4a3bdc5d7502 /src/js/utils/elements.js
parentead660139489d3b0cf082d97117f735929adaf49 (diff)
downloadplyr-e63ad7c74bd763043344fd2bd568a64e7ea18622.tar.lz
plyr-e63ad7c74bd763043344fd2bd568a64e7ea18622.tar.xz
plyr-e63ad7c74bd763043344fd2bd568a64e7ea18622.zip
Keyboard and focus improvements
Diffstat (limited to 'src/js/utils/elements.js')
-rw-r--r--src/js/utils/elements.js41
1 files changed, 23 insertions, 18 deletions
diff --git a/src/js/utils/elements.js b/src/js/utils/elements.js
index e7e17041..3a3dfcfd 100644
--- a/src/js/utils/elements.js
+++ b/src/js/utils/elements.js
@@ -116,7 +116,11 @@ export function emptyElement(element) {
// Replace element
export function replaceElement(newChild, oldChild) {
- if (!is.element(oldChild) || !is.element(oldChild.parentNode) || !is.element(newChild)) {
+ if (
+ !is.element(oldChild) ||
+ !is.element(oldChild.parentNode) ||
+ !is.element(newChild)
+ ) {
return null;
}
@@ -203,6 +207,10 @@ export function toggleHidden(element, hidden) {
// Mirror Element.classList.toggle, with IE compatibility for "force" argument
export function toggleClass(element, className, force) {
+ if (is.nodeList(element)) {
+ return Array.from(element).map(e => toggleClass(e, className, force));
+ }
+
if (is.element(element)) {
let method = 'toggle';
if (typeof force !== 'undefined') {
@@ -213,7 +221,7 @@ export function toggleClass(element, className, force) {
return element.classList.contains(className);
}
- return null;
+ return false;
}
// Has class name
@@ -249,26 +257,16 @@ export function getElement(selector) {
return this.elements.container.querySelector(selector);
}
-// Get the focused element
-export function getFocusElement() {
- let focused = document.activeElement;
-
- if (!focused || focused === document.body) {
- focused = null;
- } else {
- focused = document.querySelector(':focus');
- }
-
- return focused;
-}
-
// Trap focus inside container
export function trapFocus(element = null, toggle = false) {
if (!is.element(element)) {
return;
}
- const focusable = getElements.call(this, 'button:not(:disabled), input:not(:disabled), [tabindex]');
+ const focusable = getElements.call(
+ this,
+ 'button:not(:disabled), input:not(:disabled), [tabindex]',
+ );
const first = focusable[0];
const last = focusable[focusable.length - 1];
@@ -279,7 +277,7 @@ export function trapFocus(element = null, toggle = false) {
}
// Get the current focused element
- const focused = getFocusElement();
+ const focused = document.activeElement;
if (focused === last && !event.shiftKey) {
// Move focus to first element that can be tabbed if Shift isn't used
@@ -292,7 +290,14 @@ export function trapFocus(element = null, toggle = false) {
}
};
- toggleListener.call(this, this.elements.container, 'keydown', trap, toggle, false);
+ toggleListener.call(
+ this,
+ this.elements.container,
+ 'keydown',
+ trap,
+ toggle,
+ false,
+ );
}
// Set focus and tab focus class