UNPKG

@10up/react-focus-trap-hoc

Version:

Higher order component used to trap keyboard focus within a wrapped component.

201 lines (175 loc) 7.75 kB
(function webpackUniversalModuleDefinition(root, factory) { if(typeof exports === 'object' && typeof module === 'object') module.exports = factory(require("react")); else if(typeof define === 'function' && define.amd) define(["react"], factory); else if(typeof exports === 'object') exports["ReactFocusTrap"] = factory(require("react")); else root["ReactFocusTrap"] = factory(root["React"]); })(this, function(__WEBPACK_EXTERNAL_MODULE_0__) { return /******/ (function(modules) { // webpackBootstrap /******/ // The module cache /******/ var installedModules = {}; /******/ /******/ // The require function /******/ function __webpack_require__(moduleId) { /******/ /******/ // Check if module is in cache /******/ if(installedModules[moduleId]) { /******/ return installedModules[moduleId].exports; /******/ } /******/ // Create a new module (and put it into the cache) /******/ var module = installedModules[moduleId] = { /******/ i: moduleId, /******/ l: false, /******/ exports: {} /******/ }; /******/ /******/ // Execute the module function /******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__); /******/ /******/ // Flag the module as loaded /******/ module.l = true; /******/ /******/ // Return the exports of the module /******/ return module.exports; /******/ } /******/ /******/ /******/ // expose the modules object (__webpack_modules__) /******/ __webpack_require__.m = modules; /******/ /******/ // expose the module cache /******/ __webpack_require__.c = installedModules; /******/ /******/ // identity function for calling harmony imports with the correct context /******/ __webpack_require__.i = function(value) { return value; }; /******/ /******/ // define getter function for harmony exports /******/ __webpack_require__.d = function(exports, name, getter) { /******/ if(!__webpack_require__.o(exports, name)) { /******/ Object.defineProperty(exports, name, { /******/ configurable: false, /******/ enumerable: true, /******/ get: getter /******/ }); /******/ } /******/ }; /******/ /******/ // getDefaultExport function for compatibility with non-harmony modules /******/ __webpack_require__.n = function(module) { /******/ var getter = module && module.__esModule ? /******/ function getDefault() { return module['default']; } : /******/ function getModuleExports() { return module; }; /******/ __webpack_require__.d(getter, 'a', getter); /******/ return getter; /******/ }; /******/ /******/ // Object.prototype.hasOwnProperty.call /******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); }; /******/ /******/ // __webpack_public_path__ /******/ __webpack_require__.p = ""; /******/ /******/ // Load entry module and return exports /******/ return __webpack_require__(__webpack_require__.s = 1); /******/ }) /************************************************************************/ /******/ ([ /* 0 */ /***/ (function(module, exports) { module.exports = __WEBPACK_EXTERNAL_MODULE_0__; /***/ }), /* 1 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; 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 = __webpack_require__(0); 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; /***/ }) /******/ ]); });