@10up/react-focus-trap-hoc
Version:
Higher order component used to trap keyboard focus within a wrapped component.
108 lines (87 loc) • 4.52 kB
JavaScript
Object.defineProperty(exports, "__esModule", {
value: true
});
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
var _react = require('react');
var _react2 = _interopRequireDefault(_react);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
var trapHOC = function trapHOC() {
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
return function (WrappedComponent) {
var args = Object.assign({ trapIsActive: false }, options);
var FocusTrap = function (_Component) {
_inherits(FocusTrap, _Component);
function FocusTrap() {
_classCallCheck(this, FocusTrap);
var _this = _possibleConstructorReturn(this, (FocusTrap.__proto__ || Object.getPrototypeOf(FocusTrap)).call(this));
_this.state = {
trapIsActive: args.trapIsActive
};
_this.focusLock = _this.focusLock.bind(_this);
_this.activateTrap = _this.activateTrap.bind(_this);
_this.deactivateTrap = _this.deactivateTrap.bind(_this);
return _this;
}
_createClass(FocusTrap, [{
key: 'componentDidMount',
value: function componentDidMount() {
if (typeof document !== 'undefined') {
document.addEventListener('focus', this.focusLock, true);
}
}
}, {
key: 'componentWillUnmount',
value: function componentWillUnmount() {
if (typeof document !== 'undefined') {
document.removeEventListener('focus', this.focusLock, true);
}
}
}, {
key: 'activateTrap',
value: function activateTrap() {
this.setState({ trapIsActive: true });
}
}, {
key: 'deactivateTrap',
value: function deactivateTrap() {
this.setState({ trapIsActive: false });
}
}, {
key: 'focusLock',
value: function focusLock(e) {
if (this.state.trapIsActive === true && this.el && e.target) {
var el = this.el;
var focusable = el.querySelectorAll('[tabindex]:not([tabindex="-1"]), button:not([tabindex="-1"]), [role="button"]:not([tabindex="-1"]), [href]:not([tabindex="-1"]), input:not([tabindex="-1"]), select:not([tabindex="-1"]), textarea:not([tabindex="-1"])');
var firstFocusable = focusable[0];
if (!el.contains(e.target)) {
e.stopPropagation();
if (firstFocusable) {
firstFocusable.focus();
}
}
}
}
}, {
key: 'render',
value: function render() {
var _this2 = this;
return _react2.default.createElement(
'div',
{ className: 'trapper', ref: function ref(el) {
_this2.el = el;
} },
_react2.default.createElement(WrappedComponent, _extends({}, this.props, { activateTrap: this.activateTrap, deactivateTrap: this.deactivateTrap }))
);
}
}]);
return FocusTrap;
}(_react.Component);
return FocusTrap;
};
};
exports.default = trapHOC;
;