react-focus-lock
Advanced tools
Comparing version 2.12.1 to 2.13.0
@@ -167,3 +167,4 @@ "use strict"; | ||
returnFocus: returnFocus, | ||
focusOptions: focusOptions | ||
focusOptions: focusOptions, | ||
noFocusGuards: noFocusGuards | ||
}), /*#__PURE__*/React.createElement(Container, (0, _extends2["default"])({ | ||
@@ -170,0 +171,0 @@ ref: mergedRef |
@@ -26,4 +26,8 @@ "use strict"; | ||
var lastActiveFocus = null; | ||
var tryRestoreFocus = function tryRestoreFocus() { | ||
return null; | ||
}; | ||
var lastPortaledElement = null; | ||
var focusWasOutsideWindow = false; | ||
var windowFocused = false; | ||
var defaultWhitelist = function defaultWhitelist() { | ||
@@ -80,2 +84,10 @@ return true; | ||
}; | ||
var getNodeFocusables = function getNodeFocusables(nodes) { | ||
return (0, _focusLock.getFocusableNodes)(nodes, new Map()); | ||
}; | ||
var isNotFocusable = function isNotFocusable(node) { | ||
return !getNodeFocusables([node.parentNode]).some(function (el) { | ||
return el.node === node; | ||
}); | ||
}; | ||
var activateTrap = function activateTrap() { | ||
@@ -90,9 +102,30 @@ var result = false; | ||
crossFrame = _lastActiveTrap.crossFrame, | ||
focusOptions = _lastActiveTrap.focusOptions; | ||
focusOptions = _lastActiveTrap.focusOptions, | ||
noFocusGuards = _lastActiveTrap.noFocusGuards; | ||
var workingNode = observed || lastPortaledElement && lastPortaledElement.portaledElement; | ||
if (focusOnBody() && lastActiveFocus) { | ||
if (!document.body.contains(lastActiveFocus) || isNotFocusable(lastActiveFocus)) { | ||
lastActiveFocus = null; | ||
var newTarget = tryRestoreFocus(); | ||
if (newTarget) { | ||
newTarget.focus(); | ||
} | ||
} | ||
} | ||
var activeElement = document && document.activeElement; | ||
if (workingNode) { | ||
var workingArea = [workingNode].concat((0, _toConsumableArray2["default"])(shards.map(_util.extractRef).filter(Boolean))); | ||
var shouldForceRestoreFocus = function shouldForceRestoreFocus() { | ||
if (!focusWasOutside(crossFrame) || !noFocusGuards || !lastActiveFocus || windowFocused) { | ||
return false; | ||
} | ||
var nodes = getNodeFocusables(workingArea); | ||
var lastIndex = nodes.findIndex(function (_ref) { | ||
var node = _ref.node; | ||
return node === lastActiveFocus; | ||
}); | ||
return lastIndex === 0 || lastIndex === nodes.length - 1; | ||
}; | ||
if (!activeElement || focusWhitelisted(activeElement)) { | ||
if (persistentFocus || focusWasOutside(crossFrame) || !isFreeFocus() || !lastActiveFocus && autoFocus) { | ||
if (persistentFocus || shouldForceRestoreFocus() || !isFreeFocus() || !lastActiveFocus && autoFocus) { | ||
if (workingNode && !((0, _focusLock.focusInside)(workingArea) || activeElement && withinHost(activeElement, workingArea) || focusIsPortaledPair(activeElement, workingNode))) { | ||
@@ -113,2 +146,3 @@ if (document && !lastActiveFocus && activeElement && !autoFocus) { | ||
lastActiveFocus = document && document.activeElement; | ||
tryRestoreFocus = (0, _focusLock.captureFocusRestore)(lastActiveFocus); | ||
} | ||
@@ -119,13 +153,13 @@ } | ||
var allNodes = (0, _focusLock.expandFocusableNodes)(workingArea); | ||
var focusedIndex = allNodes.map(function (_ref) { | ||
var node = _ref.node; | ||
var focusedIndex = allNodes.map(function (_ref2) { | ||
var node = _ref2.node; | ||
return node; | ||
}).indexOf(newActiveElement); | ||
if (focusedIndex > -1) { | ||
allNodes.filter(function (_ref2) { | ||
var guard = _ref2.guard, | ||
node = _ref2.node; | ||
allNodes.filter(function (_ref3) { | ||
var guard = _ref3.guard, | ||
node = _ref3.node; | ||
return guard && node.dataset.focusAutoGuard; | ||
}).forEach(function (_ref3) { | ||
var node = _ref3.node; | ||
}).forEach(function (_ref4) { | ||
var node = _ref4.node; | ||
return node.removeAttribute('tabIndex'); | ||
@@ -160,4 +194,4 @@ }); | ||
}; | ||
var FocusTrap = function FocusTrap(_ref4) { | ||
var children = _ref4.children; | ||
var FocusTrap = function FocusTrap(_ref5) { | ||
var children = _ref5.children; | ||
return /*#__PURE__*/React.createElement("div", { | ||
@@ -171,3 +205,7 @@ onBlur: onBlur, | ||
} : {}; | ||
var onWindowFocus = function onWindowFocus() { | ||
windowFocused = true; | ||
}; | ||
var onWindowBlur = function onWindowBlur() { | ||
windowFocused = false; | ||
focusWasOutsideWindow = 'just'; | ||
@@ -181,2 +219,3 @@ (0, _util.deferAction)(function () { | ||
document.addEventListener('focusout', onBlur); | ||
window.addEventListener('focus', onWindowFocus); | ||
window.addEventListener('blur', onWindowBlur); | ||
@@ -187,7 +226,8 @@ }; | ||
document.removeEventListener('focusout', onBlur); | ||
window.removeEventListener('focus', onWindowFocus); | ||
window.removeEventListener('blur', onWindowBlur); | ||
}; | ||
function reducePropsToState(propsList) { | ||
return propsList.filter(function (_ref5) { | ||
var disabled = _ref5.disabled; | ||
return propsList.filter(function (_ref6) { | ||
var disabled = _ref6.disabled; | ||
return !disabled; | ||
@@ -215,4 +255,4 @@ }); | ||
lastTrap.onDeactivation(); | ||
if (!traps.filter(function (_ref6) { | ||
var id = _ref6.id; | ||
if (!traps.filter(function (_ref7) { | ||
var id = _ref7.id; | ||
return id === lastTrap.id; | ||
@@ -219,0 +259,0 @@ }).length) { |
@@ -150,3 +150,4 @@ import _extends from "@babel/runtime/helpers/esm/extends"; | ||
returnFocus: returnFocus, | ||
focusOptions: focusOptions | ||
focusOptions: focusOptions, | ||
noFocusGuards: noFocusGuards | ||
}), /*#__PURE__*/React.createElement(Container, _extends({ | ||
@@ -153,0 +154,0 @@ ref: mergedRef |
import * as React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import withSideEffect from 'react-clientside-effect'; | ||
import { moveFocusInside, focusInside, focusIsHidden, expandFocusableNodes, focusNextElement, focusPrevElement, focusFirstElement, focusLastElement, captureFocusRestore } from 'focus-lock'; | ||
import { moveFocusInside, focusInside, focusIsHidden, expandFocusableNodes, getFocusableNodes, focusNextElement, focusPrevElement, focusFirstElement, focusLastElement, captureFocusRestore } from 'focus-lock'; | ||
import { deferAction, extractRef } from './util'; | ||
@@ -15,4 +15,8 @@ import { mediumFocus, mediumBlur, mediumEffect } from './medium'; | ||
var lastActiveFocus = null; | ||
var tryRestoreFocus = function tryRestoreFocus() { | ||
return null; | ||
}; | ||
var lastPortaledElement = null; | ||
var focusWasOutsideWindow = false; | ||
var windowFocused = false; | ||
var defaultWhitelist = function defaultWhitelist() { | ||
@@ -69,2 +73,10 @@ return true; | ||
}; | ||
var getNodeFocusables = function getNodeFocusables(nodes) { | ||
return getFocusableNodes(nodes, new Map()); | ||
}; | ||
var isNotFocusable = function isNotFocusable(node) { | ||
return !getNodeFocusables([node.parentNode]).some(function (el) { | ||
return el.node === node; | ||
}); | ||
}; | ||
var activateTrap = function activateTrap() { | ||
@@ -79,9 +91,30 @@ var result = false; | ||
crossFrame = _lastActiveTrap.crossFrame, | ||
focusOptions = _lastActiveTrap.focusOptions; | ||
focusOptions = _lastActiveTrap.focusOptions, | ||
noFocusGuards = _lastActiveTrap.noFocusGuards; | ||
var workingNode = observed || lastPortaledElement && lastPortaledElement.portaledElement; | ||
if (focusOnBody() && lastActiveFocus) { | ||
if (!document.body.contains(lastActiveFocus) || isNotFocusable(lastActiveFocus)) { | ||
lastActiveFocus = null; | ||
var newTarget = tryRestoreFocus(); | ||
if (newTarget) { | ||
newTarget.focus(); | ||
} | ||
} | ||
} | ||
var activeElement = document && document.activeElement; | ||
if (workingNode) { | ||
var workingArea = [workingNode].concat(shards.map(extractRef).filter(Boolean)); | ||
var shouldForceRestoreFocus = function shouldForceRestoreFocus() { | ||
if (!focusWasOutside(crossFrame) || !noFocusGuards || !lastActiveFocus || windowFocused) { | ||
return false; | ||
} | ||
var nodes = getNodeFocusables(workingArea); | ||
var lastIndex = nodes.findIndex(function (_ref) { | ||
var node = _ref.node; | ||
return node === lastActiveFocus; | ||
}); | ||
return lastIndex === 0 || lastIndex === nodes.length - 1; | ||
}; | ||
if (!activeElement || focusWhitelisted(activeElement)) { | ||
if (persistentFocus || focusWasOutside(crossFrame) || !isFreeFocus() || !lastActiveFocus && autoFocus) { | ||
if (persistentFocus || shouldForceRestoreFocus() || !isFreeFocus() || !lastActiveFocus && autoFocus) { | ||
if (workingNode && !(focusInside(workingArea) || activeElement && withinHost(activeElement, workingArea) || focusIsPortaledPair(activeElement, workingNode))) { | ||
@@ -102,2 +135,3 @@ if (document && !lastActiveFocus && activeElement && !autoFocus) { | ||
lastActiveFocus = document && document.activeElement; | ||
tryRestoreFocus = captureFocusRestore(lastActiveFocus); | ||
} | ||
@@ -108,13 +142,13 @@ } | ||
var allNodes = expandFocusableNodes(workingArea); | ||
var focusedIndex = allNodes.map(function (_ref) { | ||
var node = _ref.node; | ||
var focusedIndex = allNodes.map(function (_ref2) { | ||
var node = _ref2.node; | ||
return node; | ||
}).indexOf(newActiveElement); | ||
if (focusedIndex > -1) { | ||
allNodes.filter(function (_ref2) { | ||
var guard = _ref2.guard, | ||
node = _ref2.node; | ||
allNodes.filter(function (_ref3) { | ||
var guard = _ref3.guard, | ||
node = _ref3.node; | ||
return guard && node.dataset.focusAutoGuard; | ||
}).forEach(function (_ref3) { | ||
var node = _ref3.node; | ||
}).forEach(function (_ref4) { | ||
var node = _ref4.node; | ||
return node.removeAttribute('tabIndex'); | ||
@@ -149,4 +183,4 @@ }); | ||
}; | ||
var FocusTrap = function FocusTrap(_ref4) { | ||
var children = _ref4.children; | ||
var FocusTrap = function FocusTrap(_ref5) { | ||
var children = _ref5.children; | ||
return /*#__PURE__*/React.createElement("div", { | ||
@@ -160,3 +194,7 @@ onBlur: onBlur, | ||
} : {}; | ||
var onWindowFocus = function onWindowFocus() { | ||
windowFocused = true; | ||
}; | ||
var onWindowBlur = function onWindowBlur() { | ||
windowFocused = false; | ||
focusWasOutsideWindow = 'just'; | ||
@@ -170,2 +208,3 @@ deferAction(function () { | ||
document.addEventListener('focusout', onBlur); | ||
window.addEventListener('focus', onWindowFocus); | ||
window.addEventListener('blur', onWindowBlur); | ||
@@ -176,7 +215,8 @@ }; | ||
document.removeEventListener('focusout', onBlur); | ||
window.removeEventListener('focus', onWindowFocus); | ||
window.removeEventListener('blur', onWindowBlur); | ||
}; | ||
function reducePropsToState(propsList) { | ||
return propsList.filter(function (_ref5) { | ||
var disabled = _ref5.disabled; | ||
return propsList.filter(function (_ref6) { | ||
var disabled = _ref6.disabled; | ||
return !disabled; | ||
@@ -204,4 +244,4 @@ }); | ||
lastTrap.onDeactivation(); | ||
if (!traps.filter(function (_ref6) { | ||
var id = _ref6.id; | ||
if (!traps.filter(function (_ref7) { | ||
var id = _ref7.id; | ||
return id === lastTrap.id; | ||
@@ -208,0 +248,0 @@ }).length) { |
{ | ||
"name": "react-focus-lock", | ||
"version": "2.12.1", | ||
"version": "2.13.0", | ||
"description": "It is a trap! (for a focus)", | ||
@@ -5,0 +5,0 @@ "main": "dist/cjs/index.js", |
{ | ||
"private": true, | ||
"type": "module", | ||
"main": "../dist/cjs/sidecar.js", | ||
"module": "../dist/es2015/sidecar.js", | ||
"jsnext:main": "../dist/es2015/sidecar.js", | ||
"module": "../dist/es2015/sidecar.js", | ||
"types": "sidecar.d.ts" | ||
"types": "./sidecar.d.ts" | ||
} |
{ | ||
"private": true, | ||
"type": "module", | ||
"main": "../dist/cjs/UI.js", | ||
"module": "../dist/es2015/UI.js", | ||
"jsnext:main": "../dist/es2015/UI.js", | ||
"module": "../dist/es2015/UI.js", | ||
"types": "UI.d.ts", | ||
"sideEffects": false | ||
"types": "./UI.d.ts" | ||
} |
import * as React from 'react'; | ||
import {ReactFocusLockProps, AutoFocusProps, FreeFocusProps, InFocusGuardProps} from "../interfaces"; | ||
import {ReactFocusLockProps, AutoFocusProps, FreeFocusProps, InFocusGuardProps} from "../dist/cjs/interfaces.js"; | ||
@@ -4,0 +4,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
Deprecated
MaintenanceThe maintainer of the package marked it as deprecated. This could indicate that a single version should not be used, or that the package is no longer maintained and any new vulnerabilities will not be fixed.
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
102942
41
1978
1