UNPKG

15.9 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = void 0;
7
8var React = _interopRequireWildcard(require("react"));
9
10var _reactFocusLock = _interopRequireDefault(require("react-focus-lock"));
11
12var _index = require("../locale/index.js");
13
14var _overrides = require("../helpers/overrides.js");
15
16var _index2 = require("../layer/index.js");
17
18var _constants = require("./constants.js");
19
20var _styledComponents = require("./styled-components.js");
21
22var _closeIcon = require("./close-icon.js");
23
24var _focusVisible = require("../utils/focusVisible.js");
25
26function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
27
28function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }
29
30function _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; }
31
32function _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); }
33
34function _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); }
35
36function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest(); }
37
38function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance"); }
39
40function _iterableToArrayLimit(arr, i) { if (!(Symbol.iterator in Object(arr) || Object.prototype.toString.call(arr) === "[object Arguments]")) { return; } var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
41
42function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
43
44function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
45
46function _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); } }
47
48function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
49
50function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
51
52function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
53
54function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
55
56function _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); }
57
58function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
59
60function _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; }
61
62var Drawer =
63/*#__PURE__*/
64function (_React$Component) {
65 _inherits(Drawer, _React$Component);
66
67 function Drawer() {
68 var _getPrototypeOf2;
69
70 var _this;
71
72 _classCallCheck(this, Drawer);
73
74 for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
75 args[_key] = arguments[_key];
76 }
77
78 _this = _possibleConstructorReturn(this, (_getPrototypeOf2 = _getPrototypeOf(Drawer)).call.apply(_getPrototypeOf2, [this].concat(args)));
79
80 _defineProperty(_assertThisInitialized(_this), "animateOutTimer", void 0);
81
82 _defineProperty(_assertThisInitialized(_this), "animateStartTimer", void 0);
83
84 _defineProperty(_assertThisInitialized(_this), "lastFocus", null);
85
86 _defineProperty(_assertThisInitialized(_this), "lastMountNodeOverflowStyle", null);
87
88 _defineProperty(_assertThisInitialized(_this), "_refs", {});
89
90 _defineProperty(_assertThisInitialized(_this), "state", {
91 isVisible: false,
92 mounted: false,
93 isFocusVisible: false
94 });
95
96 _defineProperty(_assertThisInitialized(_this), "handleFocus", function (event) {
97 if ((0, _focusVisible.isFocusVisible)(event)) {
98 _this.setState({
99 isFocusVisible: true
100 });
101 }
102 });
103
104 _defineProperty(_assertThisInitialized(_this), "handleBlur", function (event) {
105 if (_this.state.isFocusVisible !== false) {
106 _this.setState({
107 isFocusVisible: false
108 });
109 }
110 });
111
112 _defineProperty(_assertThisInitialized(_this), "onEscape", function () {
113 if (!_this.props.closeable) {
114 return;
115 }
116
117 _this.triggerClose(_constants.CLOSE_SOURCE.escape);
118 });
119
120 _defineProperty(_assertThisInitialized(_this), "onBackdropClick", function (event) {
121 if (_this.props.onBackdropClick) {
122 _this.props.onBackdropClick(event);
123 }
124
125 if (!_this.props.closeable) {
126 return;
127 }
128
129 _this.triggerClose(_constants.CLOSE_SOURCE.backdrop);
130 });
131
132 _defineProperty(_assertThisInitialized(_this), "onCloseClick", function () {
133 _this.triggerClose(_constants.CLOSE_SOURCE.closeButton);
134 });
135
136 _defineProperty(_assertThisInitialized(_this), "animateOutComplete", function () {
137 _this.setState({
138 isVisible: false
139 });
140 });
141
142 return _this;
143 }
144
145 _createClass(Drawer, [{
146 key: "componentDidMount",
147 value: function componentDidMount() {
148 this.setState({
149 mounted: true
150 });
151 }
152 }, {
153 key: "componentWillUnmount",
154 value: function componentWillUnmount() {
155 this.resetMountNodeScroll();
156 this.clearTimers();
157 }
158 }, {
159 key: "componentDidUpdate",
160 value: function componentDidUpdate(prevProps, prevState) {
161 var isOpen = this.props.isOpen;
162
163 if ( // If isOpen is changing *or* we just mounted and drawer should be open
164 isOpen !== prevProps.isOpen || isOpen && this.state.mounted && !prevState.mounted) {
165 if (isOpen) {
166 this.didOpen();
167 } else {
168 this.didClose();
169 }
170 }
171 }
172 }, {
173 key: "disableMountNodeScroll",
174 value: function disableMountNodeScroll() {
175 if (this.props.showBackdrop) {
176 var mountNode = this.getMountNode();
177 this.lastMountNodeOverflowStyle = mountNode.style.overflow || '';
178 mountNode.style.overflow = 'hidden';
179 }
180 }
181 }, {
182 key: "resetMountNodeScroll",
183 value: function resetMountNodeScroll() {
184 if (this.props.showBackdrop) {
185 var mountNode = this.getMountNode();
186 var lastStyle = this.lastMountNodeOverflowStyle;
187
188 if (mountNode && lastStyle !== null) {
189 mountNode.style.overflow = lastStyle || '';
190 this.lastMountNodeOverflowStyle = null;
191 }
192 }
193 }
194 }, {
195 key: "getMountNode",
196 value: function getMountNode() {
197 var mountNode = this.props.mountNode;
198
199 if (mountNode) {
200 return mountNode;
201 } // Flow thinks body could be null (cast through any)
202 // eslint-disable-next-line flowtype/no-weak-types
203
204
205 return document.body;
206 }
207 }, {
208 key: "clearTimers",
209 value: function clearTimers() {
210 if (this.animateOutTimer) {
211 clearTimeout(this.animateOutTimer);
212 }
213
214 if (this.animateStartTimer) {
215 // eslint-disable-next-line cup/no-undef
216 cancelAnimationFrame(this.animateStartTimer);
217 }
218 }
219 }, {
220 key: "didOpen",
221 value: function didOpen() {
222 var _this2 = this;
223
224 // Sometimes scroll starts past zero, possibly due to animation
225 // Reset scroll to 0 (other libraries do this as well)
226 var rootRef = this.getRef('Root').current;
227
228 if (rootRef) {
229 rootRef.scrollTop = 0;
230 } // Clear any existing timers (like previous animateOutTimer)
231
232
233 this.clearTimers();
234 this.disableMountNodeScroll(); // eslint-disable-next-line cup/no-undef
235
236 this.animateStartTimer = requestAnimationFrame(function () {
237 _this2.setState({
238 isVisible: true
239 });
240 });
241 }
242 }, {
243 key: "didClose",
244 value: function didClose() {
245 this.resetMountNodeScroll();
246 this.animateOutTimer = setTimeout(this.animateOutComplete, 500);
247 }
248 }, {
249 key: "triggerClose",
250 value: function triggerClose(source) {
251 // If there's no source, it just means the isOpen prop changed. No need to call onClose.
252 if (this.props.onClose && source) {
253 this.props.onClose({
254 closeSource: source
255 });
256 }
257 }
258 }, {
259 key: "getSharedProps",
260 value: function getSharedProps() {
261 var _this$props = this.props,
262 animate = _this$props.animate,
263 isOpen = _this$props.isOpen,
264 size = _this$props.size,
265 closeable = _this$props.closeable,
266 anchor = _this$props.anchor;
267 return {
268 $animating: animate,
269 $isVisible: this.state.isVisible,
270 $isOpen: !!isOpen,
271 $size: size,
272 $closeable: !!closeable,
273 $anchor: anchor,
274 $isFocusVisible: this.state.isFocusVisible
275 };
276 }
277 }, {
278 key: "getChildren",
279 value: function getChildren() {
280 var children = this.props.children;
281 return typeof children === 'function' ? children() : children;
282 }
283 }, {
284 key: "getRef",
285 value: function getRef(component) {
286 if (!this._refs[component]) {
287 this._refs[component] = React.createRef();
288 }
289
290 return this._refs[component];
291 }
292 }, {
293 key: "renderDrawer",
294 value: function renderDrawer(renderedContent) {
295 var _this3 = this;
296
297 var _this$props2 = this.props,
298 _this$props2$override = _this$props2.overrides,
299 overrides = _this$props2$override === void 0 ? {} : _this$props2$override,
300 closeable = _this$props2.closeable,
301 showBackdrop = _this$props2.showBackdrop,
302 autoFocus = _this$props2.autoFocus;
303 var RootOverride = overrides.Root,
304 DrawerContainerOverride = overrides.DrawerContainer,
305 DrawerBodyOverride = overrides.DrawerBody,
306 BackdropOverride = overrides.Backdrop,
307 CloseOverride = overrides.Close;
308
309 var _getOverrides = (0, _overrides.getOverrides)(RootOverride, _styledComponents.StyledRoot),
310 _getOverrides2 = _slicedToArray(_getOverrides, 2),
311 Root = _getOverrides2[0],
312 rootProps = _getOverrides2[1];
313
314 var _getOverrides3 = (0, _overrides.getOverrides)(BackdropOverride, _styledComponents.StyledBackdrop),
315 _getOverrides4 = _slicedToArray(_getOverrides3, 2),
316 Backdrop = _getOverrides4[0],
317 backdropProps = _getOverrides4[1];
318
319 var _getOverrides5 = (0, _overrides.getOverrides)(DrawerContainerOverride, _styledComponents.StyledDrawerContainer),
320 _getOverrides6 = _slicedToArray(_getOverrides5, 2),
321 DrawerContainer = _getOverrides6[0],
322 drawerContainerProps = _getOverrides6[1];
323
324 var _getOverrides7 = (0, _overrides.getOverrides)(DrawerBodyOverride, _styledComponents.StyledDrawerBody),
325 _getOverrides8 = _slicedToArray(_getOverrides7, 2),
326 DrawerBody = _getOverrides8[0],
327 drawerBodyProps = _getOverrides8[1];
328
329 var _getOverrides9 = (0, _overrides.getOverrides)(CloseOverride, _styledComponents.StyledClose),
330 _getOverrides10 = _slicedToArray(_getOverrides9, 2),
331 Close = _getOverrides10[0],
332 closeProps = _getOverrides10[1];
333
334 var sharedProps = this.getSharedProps();
335 return React.createElement(_index.LocaleContext.Consumer, null, function (locale) {
336 return (// eslint-disable-next-line jsx-a11y/no-autofocus
337 React.createElement(_reactFocusLock.default, {
338 returnFocus: true,
339 autoFocus: autoFocus
340 }, React.createElement(Root, _extends({
341 "data-baseweb": "drawer",
342 ref: _this3.getRef('Root')
343 }, sharedProps, rootProps), showBackdrop && React.createElement(Backdrop, _extends({
344 onClick: _this3.onBackdropClick
345 }, sharedProps, backdropProps)), React.createElement(DrawerContainer, _extends({
346 "aria-label": "drawer"
347 }, sharedProps, drawerContainerProps), React.createElement(DrawerBody, _extends({}, sharedProps, drawerBodyProps), renderedContent), closeable ? React.createElement(Close, _extends({
348 "aria-label": locale.drawer.close,
349 onClick: _this3.onCloseClick
350 }, sharedProps, closeProps, {
351 onFocus: (0, _focusVisible.forkFocus)(closeProps, _this3.handleFocus),
352 onBlur: (0, _focusVisible.forkBlur)(closeProps, _this3.handleBlur)
353 }), React.createElement(_closeIcon.CloseIcon, null)) : null)))
354 );
355 });
356 }
357 }, {
358 key: "render",
359 value: function render() {
360 var mountedAndOpen = this.state.mounted && (this.props.isOpen || this.state.isVisible);
361 var renderedContent = mountedAndOpen || this.props.renderAll ? this.getChildren() : null;
362
363 if (renderedContent) {
364 if (mountedAndOpen) {
365 return React.createElement(_index2.Layer, {
366 onEscape: this.onEscape,
367 mountNode: this.props.mountNode
368 }, this.renderDrawer(renderedContent));
369 } else {
370 return React.createElement(_styledComponents.Hidden, null, renderedContent);
371 }
372 }
373
374 return null;
375 }
376 }]);
377
378 return Drawer;
379}(React.Component);
380
381_defineProperty(Drawer, "defaultProps", {
382 animate: true,
383 closeable: true,
384 isOpen: false,
385 overrides: {},
386 size: _constants.SIZE.default,
387 anchor: _constants.ANCHOR.right,
388 showBackdrop: true,
389 autoFocus: true,
390 renderAll: false
391});
392
393var _default = Drawer;
394exports.default = _default;
\No newline at end of file