focus-trap
Advanced tools
Comparing version 2.1.0 to 2.2.0
# Changelog | ||
## 2.2.0 | ||
- Added/fixed, depending on your perspective: If focus is already inside the focus trap when it is activated, leave focus where it is instead of forcing it to the first tabbable node or `initialFocus`. | ||
## 2.1.0 | ||
@@ -4,0 +8,0 @@ |
19
index.js
@@ -15,6 +15,6 @@ var tabbable = require('tabbable'); | ||
var config = userOptions || {}; | ||
config.returnFocusOnDeactivate = (userOptions && userOptions.returnFocusOnDeactivate != undefined) | ||
config.returnFocusOnDeactivate = (userOptions && userOptions.returnFocusOnDeactivate !== undefined) | ||
? userOptions.returnFocusOnDeactivate | ||
: true; | ||
config.escapeDeactivates = (userOptions && userOptions.escapeDeactivates != undefined) | ||
config.escapeDeactivates = (userOptions && userOptions.escapeDeactivates !== undefined) | ||
? userOptions.escapeDeactivates | ||
@@ -52,3 +52,3 @@ : true; | ||
var defaultedDeactivateOptions = { | ||
returnFocus: (deactivateOptions && deactivateOptions.returnFocus != undefined) | ||
returnFocus: (deactivateOptions && deactivateOptions.returnFocus !== undefined) | ||
? deactivateOptions.returnFocus | ||
@@ -68,3 +68,3 @@ : config.returnFocusOnDeactivate, | ||
if (defaultedDeactivateOptions.returnFocus) { | ||
setTimeout(function() { | ||
setTimeout(function () { | ||
tryFocus(nodeFocusedBeforeActivation); | ||
@@ -120,3 +120,3 @@ }, 0); | ||
if (!node) { | ||
throw new Error('`'+key+'` refers to no known node'); | ||
throw new Error('`' + key + '` refers to no known node'); | ||
} | ||
@@ -128,3 +128,10 @@ } | ||
function firstFocusNode() { | ||
var node = getNodeForOption('initialFocus') || tabbableNodes[0] || getNodeForOption('fallbackFocus'); | ||
var node; | ||
if (getNodeForOption('initialFocus') !== null) { | ||
node = getNodeForOption('initialFocus'); | ||
} else if (container.contains(document.activeElement)) { | ||
node = document.activeElement; | ||
} else { | ||
node = tabbableNodes[0] || getNodeForOption('fallbackFocus'); | ||
} | ||
@@ -131,0 +138,0 @@ if (!node) { |
{ | ||
"name": "focus-trap", | ||
"version": "2.1.0", | ||
"version": "2.2.0", | ||
"description": "Trap focus within a DOM node.", | ||
@@ -9,4 +9,3 @@ "main": "index.js", | ||
"demo-bundle": "browserify demo/js/index.js -o demo/demo-bundle.js", | ||
"demo-watch": "watchify demo/js/index.js -d -v -o demo/demo-bundle.js", | ||
"demo-dev": "npm run demo-watch & http-server demo", | ||
"start": "budo demo/js/index.js:demo-bundle.js --dir demo", | ||
"test": "npm run lint" | ||
@@ -36,6 +35,5 @@ }, | ||
"devDependencies": { | ||
"browserify": "13.0.0", | ||
"eslint": "1.10.3", | ||
"http-server": "0.9.0", | ||
"watchify": "3.7.0" | ||
"browserify": "^13.3.0", | ||
"budo": "^9.4.1", | ||
"eslint": "^3.13.1" | ||
}, | ||
@@ -42,0 +40,0 @@ "files": [ |
@@ -69,2 +69,10 @@ # focus-trap | ||
If focus is already within it the trap, it remains unaffected. Otherwise, focus-trap will try to focus the following nodes, in order: | ||
- `createOptions.initialFocus` | ||
- The first tabbable node in the trap | ||
- `createOptions.fallbackFocus` | ||
If none of the above exist, an error will be thrown. You cannot have a focus trap that lacks focus. | ||
Returns the `focusTrap`. | ||
@@ -101,2 +109,4 @@ | ||
Focus is forced into the trap just as described for `focusTrap.activate()`. | ||
If the focus trap has not been activated or has not been paused, nothing happens. | ||
@@ -117,3 +127,3 @@ | ||
var focusTrapOne = createFocusTrap('#demo-one', { | ||
onDeactivate: function() { | ||
onDeactivate: function () { | ||
containerOne.className = 'trap'; | ||
@@ -123,3 +133,3 @@ }, | ||
document.getElementById('activate-one').addEventListener('click', function() { | ||
document.getElementById('activate-one').addEventListener('click', function () { | ||
focusTrapOne.activate(); | ||
@@ -129,3 +139,3 @@ containerOne.className = 'trap is-active'; | ||
document.getElementById('deactivate-one').addEventListener('click', function() { | ||
document.getElementById('deactivate-one').addEventListener('click', function () { | ||
focusTrapOne.deactivate(); | ||
@@ -132,0 +142,0 @@ }); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
15590
3
169
147