-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes #3.
- Loading branch information
Showing
5 changed files
with
673 additions
and
775 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
{ | ||
"presets": ["env"] | ||
} | ||
"presets": [["env", { "modules": false }]] | ||
} |
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,45 @@ | ||
'use strict'; | ||
|
||
const babel = require('rollup-plugin-babel'); | ||
const resolve = require('rollup-plugin-node-resolve'); | ||
const commonjs = require('rollup-plugin-commonjs'); | ||
const globals = require('rollup-plugin-node-globals'); | ||
|
||
module.exports = [{ | ||
input: 'src/index.js', | ||
output: [ | ||
{ | ||
file: 'dist/index.js', | ||
format: 'cjs' | ||
}, | ||
{ | ||
file: 'dist/index.esm.js', | ||
format: 'esm' | ||
} | ||
], | ||
plugins: [ | ||
babel({ | ||
exclude: 'node_modules/**' | ||
}) | ||
], | ||
external: [ | ||
'focus-lock' | ||
] | ||
}, { | ||
input: 'src/index.js', | ||
output: [ | ||
{ | ||
file: 'umd/index.js', | ||
format: 'umd', | ||
name: 'focusLock' | ||
} | ||
], | ||
plugins: [ | ||
babel({ | ||
exclude: 'node_modules/**' | ||
}), | ||
resolve(), | ||
commonjs(), | ||
globals() | ||
] | ||
}]; |
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 |
---|---|---|
@@ -1,81 +1,79 @@ | ||
(function () { | ||
const {default: moveFocusInside, focusInside, focusIsHidden} = require('focus-lock'); | ||
import moveFocusInside, {focusInside, focusIsHidden} from 'focus-lock'; | ||
|
||
let lastActiveTrap = 0; | ||
let lastActiveFocus = null; | ||
let lastActiveTrap = 0; | ||
let lastActiveFocus = null; | ||
|
||
|
||
const focusOnBody = () => ( | ||
document && document.activeElement === document.body | ||
); | ||
const focusOnBody = () => ( | ||
document && document.activeElement === document.body | ||
); | ||
|
||
const isFreeFocus = () => focusOnBody() || focusIsHidden(); | ||
const isFreeFocus = () => focusOnBody() || focusIsHidden(); | ||
|
||
|
||
const activateTrap = () => { | ||
let result = false; | ||
if (lastActiveTrap) { | ||
const observed = lastActiveTrap; | ||
if(!isFreeFocus()) { | ||
if (observed && !focusInside(observed)) { | ||
result = moveFocusInside(observed, lastActiveFocus); | ||
} | ||
lastActiveFocus = document.activeElement; | ||
const activateTrap = () => { | ||
let result = false; | ||
if (lastActiveTrap) { | ||
const observed = lastActiveTrap; | ||
if(!isFreeFocus()) { | ||
if (observed && !focusInside(observed)) { | ||
result = moveFocusInside(observed, lastActiveFocus); | ||
} | ||
lastActiveFocus = document.activeElement; | ||
} | ||
return result; | ||
}; | ||
} | ||
return result; | ||
}; | ||
|
||
const reducePropsToState = (propsList) => { | ||
return propsList | ||
.filter(node => node) | ||
.slice(-1)[0]; | ||
}; | ||
const reducePropsToState = (propsList) => { | ||
return propsList | ||
.filter(node => node) | ||
.slice(-1)[0]; | ||
}; | ||
|
||
const handleStateChangeOnClient = (trap) => { | ||
lastActiveTrap = trap; | ||
if (trap) { | ||
activateTrap(); | ||
} | ||
}; | ||
|
||
let instances = []; | ||
const handleStateChangeOnClient = (trap) => { | ||
lastActiveTrap = trap; | ||
if (trap) { | ||
activateTrap(); | ||
} | ||
}; | ||
|
||
const emitChange = (event) => { | ||
if (handleStateChangeOnClient(reducePropsToState(instances))) { | ||
event && event.preventDefault(); | ||
return true; | ||
} | ||
return false; | ||
}; | ||
let instances = []; | ||
|
||
const attachHandler = () => { | ||
document.addEventListener('focusin', emitChange); | ||
}; | ||
const emitChange = (event) => { | ||
if (handleStateChangeOnClient(reducePropsToState(instances))) { | ||
event && event.preventDefault(); | ||
return true; | ||
} | ||
return false; | ||
}; | ||
|
||
const detachHandler = () => { | ||
document.removeEventListener('focusin', emitChange); | ||
}; | ||
const attachHandler = () => { | ||
document.addEventListener('focusin', emitChange); | ||
}; | ||
|
||
const focusLock = { | ||
on(domNode){ | ||
if (instances.length === 0) { | ||
attachHandler(); | ||
} | ||
if (instances.indexOf(domNode) < 0) { | ||
instances.push(domNode); | ||
emitChange(); | ||
} | ||
}, | ||
const detachHandler = () => { | ||
document.removeEventListener('focusin', emitChange); | ||
}; | ||
|
||
off(domNode){ | ||
instances = instances.filter(node => node !== domNode); | ||
const focusLock = { | ||
on(domNode){ | ||
if (instances.length === 0) { | ||
attachHandler(); | ||
} | ||
if (instances.indexOf(domNode) < 0) { | ||
instances.push(domNode); | ||
emitChange(); | ||
if (instances.length === 0) { | ||
detachHandler(); | ||
} | ||
} | ||
}; | ||
}, | ||
|
||
off(domNode){ | ||
instances = instances.filter(node => node !== domNode); | ||
emitChange(); | ||
if (instances.length === 0) { | ||
detachHandler(); | ||
} | ||
} | ||
}; | ||
|
||
module.exports = focusLock; | ||
})(); | ||
export default focusLock; |
Oops, something went wrong.