UNPKG

4.73 kBJavaScriptView Raw
1"use strict";
2
3exports.__esModule = true;
4exports.FocusedContainer = void 0;
5
6var _react = _interopRequireWildcard(require("react"));
7
8var _propTypes = _interopRequireDefault(require("prop-types"));
9
10var _utils = require("../utils");
11
12function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
13
14function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } }
15
16function _extends() { _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; }; return _extends.apply(this, arguments); }
17
18function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
19
20function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; }
21
22function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
23
24function _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; }
25
26var isNotAncestorOf = function isNotAncestorOf(child) {
27 return function (parent) {
28 return !parent.contains(child);
29 };
30};
31
32var FocusedContainer =
33/*#__PURE__*/
34function (_Component) {
35 _inheritsLoose(FocusedContainer, _Component);
36
37 function FocusedContainer() {
38 var _this;
39
40 for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
41 args[_key] = arguments[_key];
42 }
43
44 _this = _Component.call.apply(_Component, [this].concat(args)) || this;
45
46 _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "ref", _react.default.createRef());
47
48 _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "removeTrap", function () {
49 var restrictScroll = _this.props.restrictScroll;
50 var child = _this.ref.current;
51 (0, _utils.getBodyChildElements)().filter(isNotAncestorOf(child)).forEach(_utils.makeNodeFocusable);
52
53 if (restrictScroll) {
54 document.body.style.overflow = _this.bodyOverflowStyle;
55 }
56 });
57
58 _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "trapFocus", function () {
59 var restrictScroll = _this.props.restrictScroll;
60 var child = _this.ref.current;
61 (0, _utils.getBodyChildElements)().filter(isNotAncestorOf(child)).forEach(_utils.makeNodeUnfocusable);
62
63 if (restrictScroll) {
64 _this.bodyOverflowStyle = document.body.style.overflow;
65 document.body.style.overflow = 'hidden';
66 }
67 });
68
69 return _this;
70 }
71
72 var _proto = FocusedContainer.prototype;
73
74 _proto.componentDidMount = function componentDidMount() {
75 var _this2 = this;
76
77 var hidden = this.props.hidden; // making sure trap focus always execute
78 // after removeTrap for the case where two drops
79 // are open at the same time
80
81 setTimeout(function () {
82 if (!hidden) {
83 _this2.trapFocus();
84 }
85 }, 0);
86 };
87
88 _proto.componentWillUnmount = function componentWillUnmount() {
89 this.removeTrap();
90 };
91
92 _proto.render = function render() {
93 var _this$props = this.props,
94 children = _this$props.children,
95 hidden = _this$props.hidden,
96 rest = _objectWithoutPropertiesLoose(_this$props, ["children", "hidden"]);
97
98 delete rest.restrictScroll;
99 return _react.default.createElement("div", _extends({
100 ref: this.ref,
101 "aria-hidden": hidden
102 }, rest), children);
103 };
104
105 return FocusedContainer;
106}(_react.Component);
107
108exports.FocusedContainer = FocusedContainer;
109
110_defineProperty(FocusedContainer, "defaultProps", {
111 hidden: false,
112 restrictScroll: false
113});
114
115_defineProperty(FocusedContainer, "propTypes", {
116 hidden: _propTypes.default.bool,
117 restrictScroll: _propTypes.default.bool
118});
\No newline at end of file