UNPKG

9.26 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = Layer;
7
8var React = _interopRequireWildcard(require("react"));
9
10var _reactDom = _interopRequireDefault(require("react-dom"));
11
12var _index = require("../styles/index.js");
13
14var _layersManager = require("./layers-manager.js");
15
16function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
17
18function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
19
20function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
21
22function _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); }
23
24function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
25
26function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
27
28function _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); } }
29
30function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
31
32function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
33
34function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
35
36function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
37
38function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
39
40function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
41
42function _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; }
43
44var Container = (0, _index.styled)('div', function (_ref) {
45 var $zIndex = _ref.$zIndex;
46 return {
47 position: 'absolute',
48 top: 0,
49 left: 0,
50 right: 0,
51 zIndex: $zIndex || null
52 };
53});
54Container.displayName = "Container";
55
56var LayerComponent =
57/*#__PURE__*/
58function (_React$Component) {
59 _inherits(LayerComponent, _React$Component);
60
61 function LayerComponent() {
62 var _getPrototypeOf2;
63
64 var _this;
65
66 _classCallCheck(this, LayerComponent);
67
68 for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
69 args[_key] = arguments[_key];
70 }
71
72 _this = _possibleConstructorReturn(this, (_getPrototypeOf2 = _getPrototypeOf(LayerComponent)).call.apply(_getPrototypeOf2, [this].concat(args)));
73
74 _defineProperty(_assertThisInitialized(_this), "state", {
75 container: null
76 });
77
78 _defineProperty(_assertThisInitialized(_this), "onEscape", function () {
79 if (_this.props.onEscape) {
80 _this.props.onEscape();
81 }
82 });
83
84 _defineProperty(_assertThisInitialized(_this), "onDocumentClick", function (event) {
85 if (_this.props.onDocumentClick) {
86 _this.props.onDocumentClick(event);
87 }
88 });
89
90 return _this;
91 }
92
93 _createClass(LayerComponent, [{
94 key: "componentDidMount",
95 value: function componentDidMount() {
96 this.context.addEscapeHandler(this.onEscape);
97 this.context.addDocClickHandler(this.onDocumentClick);
98 var _this$props = this.props,
99 onMount = _this$props.onMount,
100 mountNode = _this$props.mountNode,
101 layersManagerHost = _this$props.host;
102
103 if (mountNode) {
104 onMount && onMount();
105 return;
106 } // There was no LayersManager added if this.props.host === undefined.
107 // Use document.body is the case no LayersManager is used.
108
109
110 var hasLayersManager = layersManagerHost !== undefined;
111
112 if (process.env.NODE_ENV !== "production") {
113 if (!hasLayersManager) {
114 console.warn('`LayersManager` was not found. This occurs if you are attempting to use a component requiring `Layer` without using the `BaseProvider` at the root of your app. Please visit https://baseweb.design/components/base-provider/ for more information');
115 }
116 }
117
118 var host = hasLayersManager ? layersManagerHost : document.body;
119
120 if (host) {
121 this.addContainer(host);
122 }
123 }
124 }, {
125 key: "componentDidUpdate",
126 value: function componentDidUpdate(prevProps) {
127 var _this$props2 = this.props,
128 host = _this$props2.host,
129 mountNode = _this$props2.mountNode;
130
131 if (mountNode) {
132 return;
133 }
134
135 if (host && host !== prevProps.host && prevProps.host === null) {
136 this.addContainer(host);
137 }
138 }
139 }, {
140 key: "componentWillUnmount",
141 value: function componentWillUnmount() {
142 this.context.removeEscapeHandler(this.onEscape);
143 this.context.removeDocClickHandler(this.onDocumentClick);
144
145 if (this.props.onUnmount) {
146 this.props.onUnmount();
147 }
148
149 var host = this.props.host;
150 var container = this.state.container;
151
152 if (host && container) {
153 if (host.contains(container)) {
154 host.removeChild(container);
155 }
156 }
157 }
158 }, {
159 key: "addContainer",
160 value: function addContainer(host) {
161 var _this$props3 = this.props,
162 index = _this$props3.index,
163 mountNode = _this$props3.mountNode,
164 onMount = _this$props3.onMount; // Do nothing if mountNode is provided
165
166 if (mountNode) {
167 return;
168 }
169
170 if (host) {
171 var container = host.ownerDocument.createElement('div'); // `host` is an DOM node, but not a React component
172
173 var sibling = typeof index === 'number' ? host.children[index] : null;
174 sibling ? host.insertBefore(container, sibling) : host.appendChild(container);
175 this.setState({
176 container: container
177 }, function () {
178 onMount && onMount();
179 });
180 }
181 }
182 }, {
183 key: "render",
184 value: function render() {
185 var container = this.state.container;
186 var _this$props4 = this.props,
187 children = _this$props4.children,
188 mountNode = _this$props4.mountNode,
189 zIndex = _this$props4.zIndex; // Only adding an additional wrapper when a layer has z-index to be set
190
191 var childrenToRender = zIndex ? React.createElement(Container, {
192 $zIndex: zIndex
193 }, children) : children;
194
195 if (typeof document !== 'undefined') {
196 if (mountNode || container) {
197 // $FlowFixMe
198 return _reactDom.default.createPortal(childrenToRender, mountNode || container);
199 }
200
201 return null;
202 }
203
204 return null;
205 }
206 }]);
207
208 return LayerComponent;
209}(React.Component);
210
211_defineProperty(LayerComponent, "contextType", _layersManager.LayersContext);
212
213function Layer(props) {
214 return React.createElement(_layersManager.Consumer, null, function (_ref2) {
215 var host = _ref2.host,
216 zIndex = _ref2.zIndex;
217 return React.createElement(LayerComponent, _extends({}, props, {
218 host: host,
219 zIndex: zIndex
220 }));
221 });
222}
\No newline at end of file