UNPKG

14.3 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 _overrides = require("../helpers/overrides.js");
11
12var _delete = _interopRequireDefault(require("../icon/delete.js"));
13
14var _styledComponents = require("./styled-components.js");
15
16var _constants = require("./constants.js");
17
18var _index = require("../locale/index.js");
19
20var _focusVisible = require("../utils/focusVisible.js");
21
22function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
23
24function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }
25
26function _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; }
27
28function _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); }
29
30function _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); }
31
32function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest(); }
33
34function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance"); }
35
36function _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; }
37
38function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
39
40function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
41
42function _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); } }
43
44function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
45
46function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
47
48function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
49
50function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
51
52function _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); }
53
54function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
55
56function _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; }
57
58var Toast =
59/*#__PURE__*/
60function (_React$Component) {
61 _inherits(Toast, _React$Component);
62
63 function Toast(props) {
64 var _this;
65
66 _classCallCheck(this, Toast);
67
68 _this = _possibleConstructorReturn(this, _getPrototypeOf(Toast).call(this, props));
69
70 _defineProperty(_assertThisInitialized(_this), "autoHideTimeout", void 0);
71
72 _defineProperty(_assertThisInitialized(_this), "animateInTimer", void 0);
73
74 _defineProperty(_assertThisInitialized(_this), "animateOutCompleteTimer", void 0);
75
76 _defineProperty(_assertThisInitialized(_this), "closeRef", void 0);
77
78 _defineProperty(_assertThisInitialized(_this), "previouslyFocusedElement", void 0);
79
80 _defineProperty(_assertThisInitialized(_this), "state", {
81 isVisible: false,
82 isRendered: true,
83 isFocusVisible: false
84 });
85
86 _defineProperty(_assertThisInitialized(_this), "handleFocus", function (event) {
87 if ((0, _focusVisible.isFocusVisible)(event)) {
88 _this.setState({
89 isFocusVisible: true
90 });
91 }
92 });
93
94 _defineProperty(_assertThisInitialized(_this), "handleBlur", function (event) {
95 if (_this.state.isFocusVisible !== false) {
96 _this.setState({
97 isFocusVisible: false
98 });
99 }
100 });
101
102 _defineProperty(_assertThisInitialized(_this), "animateIn", function () {
103 // Defer to next event loop
104 _this.animateInTimer = setTimeout(function () {
105 _this.setState({
106 isVisible: true
107 });
108 }, 0);
109 });
110
111 _defineProperty(_assertThisInitialized(_this), "animateOut", function () {
112 var callback = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : function () {};
113
114 _this.setState({
115 isVisible: false
116 }); // Remove the toast from the DOM after animation finishes
117
118
119 _this.animateOutCompleteTimer = setTimeout(function () {
120 _this.setState({
121 isRendered: false
122 });
123
124 callback();
125 }, 600);
126 });
127
128 _defineProperty(_assertThisInitialized(_this), "dismiss", function () {
129 _this.animateOut(_this.props.onClose);
130
131 if (_this.props.autoFocus && _this.previouslyFocusedElement) {
132 _this.previouslyFocusedElement.focus();
133 }
134 });
135
136 _defineProperty(_assertThisInitialized(_this), "onFocus", function (e) {
137 if (!_this.state.isVisible) return;
138 clearTimeout(_this.autoHideTimeout);
139 clearTimeout(_this.animateOutCompleteTimer);
140 typeof _this.props.onFocus === 'function' && _this.props.onFocus(e);
141 });
142
143 _defineProperty(_assertThisInitialized(_this), "onMouseEnter", function (e) {
144 if (!_this.state.isVisible) return;
145 clearTimeout(_this.autoHideTimeout);
146 clearTimeout(_this.animateOutCompleteTimer);
147 typeof _this.props.onMouseEnter === 'function' && _this.props.onMouseEnter(e);
148 });
149
150 _defineProperty(_assertThisInitialized(_this), "onBlur", function (e) {
151 _this.startTimeout();
152
153 typeof _this.props.onBlur === 'function' && _this.props.onBlur(e);
154 });
155
156 _defineProperty(_assertThisInitialized(_this), "onMouseLeave", function (e) {
157 _this.startTimeout();
158
159 typeof _this.props.onMouseLeave === 'function' && _this.props.onMouseLeave(e);
160 });
161
162 _this.closeRef = React.createRef();
163 _this.previouslyFocusedElement = null;
164 return _this;
165 }
166
167 _createClass(Toast, [{
168 key: "componentDidMount",
169 value: function componentDidMount() {
170 this.animateIn();
171 this.startTimeout();
172
173 if (typeof document !== 'undefined' && this.props.autoFocus && this.closeRef && this.closeRef.current && this.closeRef.current.focus && typeof this.closeRef.current.focus === 'function') {
174 this.previouslyFocusedElement = document.activeElement; // $FlowFixMe: CloseIcon is `mixed` type so doesn't like `focus` call.
175
176 this.closeRef.current.focus();
177 this.setState({
178 isFocusVisible: true
179 });
180 }
181 }
182 }, {
183 key: "componentDidUpdate",
184 value: function componentDidUpdate(prevProps) {
185 if (this.props.autoHideDuration !== prevProps.autoHideDuration || this.props.__updated !== prevProps.__updated) {
186 this.startTimeout();
187 }
188 }
189 }, {
190 key: "componentWillUnmount",
191 value: function componentWillUnmount() {
192 this.clearTimeout();
193 }
194 }, {
195 key: "startTimeout",
196 value: function startTimeout() {
197 if (this.props.autoHideDuration) {
198 if (this.autoHideTimeout) {
199 clearTimeout(this.autoHideTimeout);
200 }
201
202 this.autoHideTimeout = setTimeout(this.dismiss, this.props.autoHideDuration);
203 }
204 }
205 }, {
206 key: "clearTimeout",
207 value: function (_clearTimeout) {
208 function clearTimeout() {
209 return _clearTimeout.apply(this, arguments);
210 }
211
212 clearTimeout.toString = function () {
213 return _clearTimeout.toString();
214 };
215
216 return clearTimeout;
217 }(function () {
218 [this.autoHideTimeout, this.animateInTimer, this.animateOutCompleteTimer].forEach(function (timerId) {
219 if (timerId) {
220 clearTimeout(timerId);
221 }
222 });
223 })
224 }, {
225 key: "getSharedProps",
226 value: function getSharedProps() {
227 var _this$props = this.props,
228 kind = _this$props.kind,
229 notificationType = _this$props.notificationType,
230 closeable = _this$props.closeable;
231 var _this$state = this.state,
232 isRendered = _this$state.isRendered,
233 isVisible = _this$state.isVisible;
234 return {
235 $kind: kind,
236 $type: notificationType,
237 $closeable: closeable,
238 $isRendered: isRendered,
239 $isVisible: isVisible
240 };
241 }
242 }, {
243 key: "render",
244 value: function render() {
245 var _this2 = this;
246
247 var _this$props2 = this.props,
248 children = _this$props2.children,
249 closeable = _this$props2.closeable;
250 var isRendered = this.state.isRendered;
251 var _this$props$overrides = this.props.overrides,
252 BodyOverride = _this$props$overrides.Body,
253 CloseIconOverride = _this$props$overrides.CloseIcon,
254 InnerContainerOverride = _this$props$overrides.InnerContainer;
255
256 var _getOverrides = (0, _overrides.getOverrides)(BodyOverride, _styledComponents.Body),
257 _getOverrides2 = _slicedToArray(_getOverrides, 2),
258 Body = _getOverrides2[0],
259 bodyProps = _getOverrides2[1];
260
261 var _getOverrides3 = (0, _overrides.getOverrides)(InnerContainerOverride, _styledComponents.InnerContainer),
262 _getOverrides4 = _slicedToArray(_getOverrides3, 2),
263 InnerContainer = _getOverrides4[0],
264 innerContainerProps = _getOverrides4[1];
265
266 var _getOverrides5 = (0, _overrides.getOverrides)(CloseIconOverride, _styledComponents.CloseIconSvg),
267 _getOverrides6 = _slicedToArray(_getOverrides5, 2),
268 CloseIcon = _getOverrides6[0],
269 closeIconProps = _getOverrides6[1];
270
271 var closeIconOverrides = (0, _overrides.mergeOverrides)({
272 Svg: {
273 component: CloseIcon
274 }
275 }, // $FlowFixMe
276 {
277 Svg: CloseIconOverride
278 });
279 var sharedProps = this.getSharedProps();
280
281 if (!isRendered) {
282 return null;
283 }
284
285 return React.createElement(_index.LocaleContext.Consumer, null, function (locale) {
286 return React.createElement(Body, _extends({
287 role: "alert",
288 "data-baseweb": _this2.props['data-baseweb'] || 'toast'
289 }, sharedProps, bodyProps, {
290 // the properties below have to go after overrides
291 onBlur: _this2.onBlur,
292 onFocus: _this2.onFocus,
293 onMouseEnter: _this2.onMouseEnter,
294 onMouseLeave: _this2.onMouseLeave
295 }), React.createElement(InnerContainer, _extends({}, sharedProps, innerContainerProps), typeof children === 'function' ? children({
296 dismiss: _this2.dismiss
297 }) : children), closeable ? React.createElement(_delete.default, _extends({
298 ref: _this2.closeRef,
299 role: "button",
300 tabIndex: 0,
301 $isFocusVisible: _this2.state.isFocusVisible,
302 onClick: _this2.dismiss,
303 onKeyPress: function onKeyPress(event) {
304 if (event.key === 'Enter') {
305 _this2.dismiss();
306 }
307 },
308 title: locale.toast.close
309 }, sharedProps, closeIconProps, {
310 onFocus: (0, _focusVisible.forkFocus)(closeIconProps, _this2.handleFocus),
311 onBlur: (0, _focusVisible.forkBlur)(closeIconProps, _this2.handleBlur),
312 overrides: closeIconOverrides
313 })) : null);
314 });
315 }
316 }]);
317
318 return Toast;
319}(React.Component);
320
321_defineProperty(Toast, "defaultProps", {
322 autoFocus: false,
323 autoHideDuration: 0,
324 closeable: true,
325 kind: _constants.KIND.info,
326 notificationType: _constants.TYPE.toast,
327 // Do we need a separate handler for
328 // when a notification dismisses automatically
329 onClose: function onClose() {},
330 onBlur: function onBlur() {},
331 onFocus: function onFocus() {},
332 onMouseEnter: function onMouseEnter() {},
333 onMouseLeave: function onMouseLeave() {},
334 overrides: {}
335});
336
337var _default = Toast;
338exports.default = _default;
\No newline at end of file