focus-trap-react
Advanced tools
Comparing version 8.11.2 to 8.11.3
# Changelog | ||
## 8.11.3 | ||
### Patch Changes | ||
- 9947461: Bump focus-trap dependency to v6.9.4 to get typings fix. | ||
- 519e5a5: Fix setReturnFocus option as function not being passed node focused prior to activation. | ||
## 8.11.2 | ||
@@ -4,0 +11,0 @@ |
@@ -25,2 +25,4 @@ "use strict"; | ||
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } | ||
var React = require('react'); | ||
@@ -54,2 +56,45 @@ | ||
_this = _super.call(this, props); | ||
_defineProperty(_assertThisInitialized(_this), "getNodeForOption", function (optionName) { | ||
var _this$internalOptions; | ||
// use internal options first, falling back to original options | ||
var optionValue = (_this$internalOptions = this.internalOptions[optionName]) !== null && _this$internalOptions !== void 0 ? _this$internalOptions : this.originalOptions[optionName]; | ||
if (typeof optionValue === 'function') { | ||
for (var _len = arguments.length, params = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { | ||
params[_key - 1] = arguments[_key]; | ||
} | ||
optionValue = optionValue.apply(void 0, params); | ||
} | ||
if (optionValue === true) { | ||
optionValue = undefined; // use default value | ||
} | ||
if (!optionValue) { | ||
if (optionValue === undefined || optionValue === false) { | ||
return optionValue; | ||
} // else, empty string (invalid), null (invalid), 0 (invalid) | ||
throw new Error("`".concat(optionName, "` was specified but was not a node, or did not return a node")); | ||
} | ||
var node = optionValue; // could be HTMLElement, SVGElement, or non-empty string at this point | ||
if (typeof optionValue === 'string') { | ||
var _this$getDocument; | ||
node = (_this$getDocument = this.getDocument()) === null || _this$getDocument === void 0 ? void 0 : _this$getDocument.querySelector(optionValue); // resolve to node, or null if fails | ||
if (!node) { | ||
throw new Error("`".concat(optionName, "` as selector refers to no known node")); | ||
} | ||
} | ||
return node; | ||
}); | ||
_this.handleDeactivate = _this.handleDeactivate.bind(_assertThisInitialized(_this)); | ||
@@ -79,6 +124,6 @@ _this.handlePostDeactivate = _this.handlePostDeactivate.bind(_assertThisInitialized(_this)); | ||
_this.originalOptions = { | ||
// because of the above `tailoredFocusTrapOptions`, we maintain our own flag for | ||
// because of the above `internalOptions`, we maintain our own flag for | ||
// this option, and default it to `true` because that's focus-trap's default | ||
returnFocusOnDeactivate: true, | ||
// because of the above `tailoredFocusTrapOptions`, we keep these separate since | ||
// because of the above `internalOptions`, we keep these separate since | ||
// they're part of the deactivation process which we configure (internally) to | ||
@@ -101,3 +146,3 @@ // be shared between focus-trap and focus-trap-react | ||
_this.originalOptions[optionName] = focusTrapOptions[optionName]; | ||
continue; // exclude from tailoredFocusTrapOptions | ||
continue; // exclude from internalOptions | ||
} | ||
@@ -135,40 +180,22 @@ | ||
return this.props.focusTrapOptions.document || (typeof document !== 'undefined' ? document : undefined); | ||
} // TODO: Need more test coverage for this function | ||
} | ||
/** | ||
* Gets the node for the given option, which is expected to be an option that | ||
* can be either a DOM node, a string that is a selector to get a node, `false` | ||
* (if a node is explicitly NOT given), or a function that returns any of these | ||
* values. | ||
* @param {string} optionName | ||
* @returns {undefined | false | HTMLElement | SVGElement} Returns | ||
* `undefined` if the option is not specified; `false` if the option | ||
* resolved to `false` (node explicitly not given); otherwise, the resolved | ||
* DOM node. | ||
* @throws {Error} If the option is set, not `false`, and is not, or does not | ||
* resolve to a node. | ||
*/ | ||
}, { | ||
key: "getNodeForOption", | ||
value: function getNodeForOption(optionName) { | ||
var optionValue = this.internalOptions[optionName]; | ||
if (!optionValue) { | ||
return null; | ||
} | ||
var node = optionValue; | ||
if (typeof optionValue === 'string') { | ||
var _this$getDocument; | ||
node = (_this$getDocument = this.getDocument()) === null || _this$getDocument === void 0 ? void 0 : _this$getDocument.querySelector(optionValue); | ||
if (!node) { | ||
throw new Error("`".concat(optionName, "` refers to no known node")); | ||
} | ||
} | ||
if (typeof optionValue === 'function') { | ||
node = optionValue(); | ||
if (!node) { | ||
throw new Error("`".concat(optionName, "` did not return a node")); | ||
} | ||
} | ||
return node; | ||
} | ||
}, { | ||
key: "getReturnFocusNode", | ||
value: function getReturnFocusNode() { | ||
var node = this.getNodeForOption('setReturnFocus'); | ||
return node ? node : this.previouslyFocusedElement; | ||
var node = this.getNodeForOption('setReturnFocus', this.previouslyFocusedElement); | ||
return node ? node : node === false ? false : this.previouslyFocusedElement; | ||
} | ||
@@ -425,8 +452,9 @@ /** Update the previously focused element with the currently focused element. */ | ||
checkCanReturnFocus: PropTypes.func, | ||
initialFocus: PropTypes.oneOfType([PropTypes.instanceOf(ElementType), PropTypes.string, PropTypes.func, PropTypes.bool]), | ||
fallbackFocus: PropTypes.oneOfType([PropTypes.instanceOf(ElementType), PropTypes.string, PropTypes.func]), | ||
initialFocus: PropTypes.oneOfType([PropTypes.instanceOf(ElementType), PropTypes.string, PropTypes.bool, PropTypes.func]), | ||
fallbackFocus: PropTypes.oneOfType([PropTypes.instanceOf(ElementType), PropTypes.string, // NOTE: does not support `false` as value (or return value from function) | ||
PropTypes.func]), | ||
escapeDeactivates: PropTypes.oneOfType([PropTypes.bool, PropTypes.func]), | ||
clickOutsideDeactivates: PropTypes.oneOfType([PropTypes.bool, PropTypes.func]), | ||
returnFocusOnDeactivate: PropTypes.bool, | ||
setReturnFocus: PropTypes.oneOfType([PropTypes.instanceOf(ElementType), PropTypes.string, PropTypes.func]), | ||
setReturnFocus: PropTypes.oneOfType([PropTypes.instanceOf(ElementType), PropTypes.string, PropTypes.bool, PropTypes.func]), | ||
allowOutsideClick: PropTypes.oneOfType([PropTypes.bool, PropTypes.func]), | ||
@@ -433,0 +461,0 @@ preventScroll: PropTypes.bool, |
{ | ||
"name": "focus-trap-react", | ||
"version": "8.11.2", | ||
"version": "8.11.3", | ||
"description": "A React component that traps focus.", | ||
@@ -62,15 +62,15 @@ "main": "dist/focus-trap-react.js", | ||
"@babel/core": "^7.18.2", | ||
"@babel/eslint-parser": "^7.17.0", | ||
"@babel/eslint-parser": "^7.18.2", | ||
"@babel/plugin-proposal-class-properties": "^7.17.12", | ||
"@babel/preset-env": "^7.18.0", | ||
"@babel/preset-env": "^7.18.2", | ||
"@babel/preset-react": "^7.17.12", | ||
"@changesets/cli": "^2.22.0", | ||
"@testing-library/cypress": "^8.0.2", | ||
"@testing-library/cypress": "^8.0.3", | ||
"@testing-library/dom": "^8.13.0", | ||
"@testing-library/jest-dom": "^5.16.4", | ||
"@testing-library/react": "^13.2.0", | ||
"@testing-library/react": "^13.3.0", | ||
"@testing-library/user-event": "^14.2.0", | ||
"@types/jquery": "^3.5.14", | ||
"all-contributors-cli": "^6.20.0", | ||
"babel-jest": "^28.1.0", | ||
"babel-jest": "^28.1.1", | ||
"babelify": "^10.0.0", | ||
@@ -81,9 +81,9 @@ "browserify": "^17.0.0", | ||
"cypress-plugin-tab": "^1.0.5", | ||
"eslint": "^8.16.0", | ||
"eslint": "^8.17.0", | ||
"eslint-config-prettier": "^8.5.0", | ||
"eslint-plugin-cypress": "^2.12.1", | ||
"eslint-plugin-jest": "^26.2.2", | ||
"eslint-plugin-jest": "^26.5.3", | ||
"eslint-plugin-react": "^7.30.0", | ||
"jest": "^28.1.0", | ||
"jest-environment-jsdom": "^28.1.0", | ||
"jest": "^28.1.1", | ||
"jest-environment-jsdom": "^28.1.1", | ||
"jest-watch-typeahead": "^1.1.0", | ||
@@ -97,6 +97,6 @@ "onchange": "^7.1.0", | ||
"start-server-and-test": "^1.14.0", | ||
"typescript": "^4.7.2" | ||
"typescript": "^4.7.3" | ||
}, | ||
"dependencies": { | ||
"focus-trap": "^6.9.3" | ||
"focus-trap": "^6.9.4" | ||
}, | ||
@@ -103,0 +103,0 @@ "peerDependencies": { |
@@ -45,7 +45,7 @@ const React = require('react'); | ||
this.originalOptions = { | ||
// because of the above `tailoredFocusTrapOptions`, we maintain our own flag for | ||
// because of the above `internalOptions`, we maintain our own flag for | ||
// this option, and default it to `true` because that's focus-trap's default | ||
returnFocusOnDeactivate: true, | ||
// because of the above `tailoredFocusTrapOptions`, we keep these separate since | ||
// because of the above `internalOptions`, we keep these separate since | ||
// they're part of the deactivation process which we configure (internally) to | ||
@@ -75,3 +75,3 @@ // be shared between focus-trap and focus-trap-react | ||
this.originalOptions[optionName] = focusTrapOptions[optionName]; | ||
continue; // exclude from tailoredFocusTrapOptions | ||
continue; // exclude from internalOptions | ||
} | ||
@@ -111,22 +111,47 @@ | ||
// TODO: Need more test coverage for this function | ||
getNodeForOption(optionName) { | ||
const optionValue = this.internalOptions[optionName]; | ||
if (!optionValue) { | ||
return null; | ||
/** | ||
* Gets the node for the given option, which is expected to be an option that | ||
* can be either a DOM node, a string that is a selector to get a node, `false` | ||
* (if a node is explicitly NOT given), or a function that returns any of these | ||
* values. | ||
* @param {string} optionName | ||
* @returns {undefined | false | HTMLElement | SVGElement} Returns | ||
* `undefined` if the option is not specified; `false` if the option | ||
* resolved to `false` (node explicitly not given); otherwise, the resolved | ||
* DOM node. | ||
* @throws {Error} If the option is set, not `false`, and is not, or does not | ||
* resolve to a node. | ||
*/ | ||
getNodeForOption = function (optionName, ...params) { | ||
// use internal options first, falling back to original options | ||
let optionValue = | ||
this.internalOptions[optionName] ?? this.originalOptions[optionName]; | ||
if (typeof optionValue === 'function') { | ||
optionValue = optionValue(...params); | ||
} | ||
let node = optionValue; | ||
if (optionValue === true) { | ||
optionValue = undefined; // use default value | ||
} | ||
if (typeof optionValue === 'string') { | ||
node = this.getDocument()?.querySelector(optionValue); | ||
if (!node) { | ||
throw new Error(`\`${optionName}\` refers to no known node`); | ||
if (!optionValue) { | ||
if (optionValue === undefined || optionValue === false) { | ||
return optionValue; | ||
} | ||
// else, empty string (invalid), null (invalid), 0 (invalid) | ||
throw new Error( | ||
`\`${optionName}\` was specified but was not a node, or did not return a node` | ||
); | ||
} | ||
if (typeof optionValue === 'function') { | ||
node = optionValue(); | ||
let node = optionValue; // could be HTMLElement, SVGElement, or non-empty string at this point | ||
if (typeof optionValue === 'string') { | ||
node = this.getDocument()?.querySelector(optionValue); // resolve to node, or null if fails | ||
if (!node) { | ||
throw new Error(`\`${optionName}\` did not return a node`); | ||
throw new Error( | ||
`\`${optionName}\` as selector refers to no known node` | ||
); | ||
} | ||
@@ -136,8 +161,10 @@ } | ||
return node; | ||
} | ||
}; | ||
getReturnFocusNode() { | ||
const node = this.getNodeForOption('setReturnFocus'); | ||
return node ? node : this.previouslyFocusedElement; | ||
const node = this.getNodeForOption( | ||
'setReturnFocus', | ||
this.previouslyFocusedElement | ||
); | ||
return node ? node : node === false ? false : this.previouslyFocusedElement; | ||
} | ||
@@ -402,4 +429,4 @@ | ||
PropTypes.string, | ||
PropTypes.bool, | ||
PropTypes.func, | ||
PropTypes.bool, | ||
]), | ||
@@ -409,2 +436,3 @@ fallbackFocus: PropTypes.oneOfType([ | ||
PropTypes.string, | ||
// NOTE: does not support `false` as value (or return value from function) | ||
PropTypes.func, | ||
@@ -421,2 +449,3 @@ ]), | ||
PropTypes.string, | ||
PropTypes.bool, | ||
PropTypes.func, | ||
@@ -423,0 +452,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
76032
813
Updatedfocus-trap@^6.9.4