UNPKG

react-toastify

Version:
331 lines (278 loc) 11.4 kB
"use strict"; exports.__esModule = true; exports.default = void 0; var _react = _interopRequireWildcard(require("react")); var _propTypes = _interopRequireDefault(require("prop-types")); var _classnames = _interopRequireDefault(require("classnames")); var _ProgressBar = _interopRequireDefault(require("./ProgressBar")); var _constant = require("./../utils/constant"); var _propValidator = require("./../utils/propValidator"); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _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; } } function _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); } function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; } function getX(e) { return e.targetTouches && e.targetTouches.length >= 1 ? e.targetTouches[0].clientX : e.clientX; } function getY(e) { return e.targetTouches && e.targetTouches.length >= 1 ? e.targetTouches[0].clientY : e.clientY; } var noop = function noop() {}; var Toast = /*#__PURE__*/ function (_Component) { _inheritsLoose(Toast, _Component); function Toast() { var _this; for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } _this = _Component.call.apply(_Component, [this].concat(args)) || this; _this.state = { isRunning: true, preventExitTransition: false }; _this.flag = { canCloseOnClick: true, canDrag: false }; _this.drag = { start: 0, x: 0, y: 0, deltaX: 0, removalDistance: 0 }; _this.ref = null; _this.pauseToast = function () { if (_this.props.autoClose) { _this.setState({ isRunning: false }); } }; _this.playToast = function () { if (_this.props.autoClose) { _this.setState({ isRunning: true }); } }; _this.onDragStart = function (e) { _this.flag.canCloseOnClick = true; _this.flag.canDrag = true; _this.ref.style.transition = ''; _this.drag.start = _this.drag.x = getX(e.nativeEvent); _this.drag.removalDistance = _this.ref.offsetWidth * (_this.props.draggablePercent / 100); }; _this.onDragMove = function (e) { if (_this.flag.canDrag) { if (_this.state.isRunning) { _this.pauseToast(); } _this.drag.x = getX(e); _this.drag.deltaX = _this.drag.x - _this.drag.start; // prevent false positif during a toast click _this.drag.start !== _this.drag.x && (_this.flag.canCloseOnClick = false); _this.ref.style.transform = "translateX(" + _this.drag.deltaX + "px)"; _this.ref.style.opacity = 1 - Math.abs(_this.drag.deltaX / _this.drag.removalDistance); } }; _this.onDragEnd = function (e) { if (_this.flag.canDrag) { _this.flag.canDrag = false; if (Math.abs(_this.drag.deltaX) > _this.drag.removalDistance) { _this.setState({ preventExitTransition: true }, _this.props.closeToast); return; } _this.drag.y = getY(e); _this.ref.style.transition = 'transform 0.2s, opacity 0.2s'; _this.ref.style.transform = 'translateX(0)'; _this.ref.style.opacity = 1; } }; _this.onDragTransitionEnd = function () { var _this$ref$getBounding = _this.ref.getBoundingClientRect(), top = _this$ref$getBounding.top, bottom = _this$ref$getBounding.bottom, left = _this$ref$getBounding.left, right = _this$ref$getBounding.right; if (_this.props.pauseOnHover && _this.drag.x >= left && _this.drag.x <= right && _this.drag.y >= top && _this.drag.y <= bottom) { _this.pauseToast(); } else { _this.playToast(); } }; return _this; } var _proto = Toast.prototype; _proto.componentDidMount = function componentDidMount() { this.props.onOpen(this.props.children.props); if (this.props.draggable) { this.bindDragEvents(); } // Maybe I could bind the event in the ToastContainer and rely on delegation if (this.props.pauseOnFocusLoss) { this.bindFocusEvents(); } }; _proto.componentDidUpdate = function componentDidUpdate(prevProps) { if (prevProps.draggable !== this.props.draggable) { if (this.props.draggable) { this.bindDragEvents(); } else { this.unbindDragEvents(); } } if (prevProps.pauseOnFocusLoss !== this.props.pauseOnFocusLoss) { if (this.props.pauseOnFocusLoss) { this.bindFocusEvents(); } else { this.unbindFocusEvents(); } } }; _proto.componentWillUnmount = function componentWillUnmount() { this.props.onClose(this.props.children.props); if (this.props.draggable) { this.unbindDragEvents(); } if (this.props.pauseOnFocusLoss) { this.unbindFocusEvents(); } }; _proto.bindFocusEvents = function bindFocusEvents() { window.addEventListener('focus', this.playToast); window.addEventListener('blur', this.pauseToast); }; _proto.unbindFocusEvents = function unbindFocusEvents() { window.removeEventListener('focus', this.playToast); window.removeEventListener('blur', this.pauseToast); }; _proto.bindDragEvents = function bindDragEvents() { document.addEventListener('mousemove', this.onDragMove); document.addEventListener('mouseup', this.onDragEnd); document.addEventListener('touchmove', this.onDragMove); document.addEventListener('touchend', this.onDragEnd); }; _proto.unbindDragEvents = function unbindDragEvents() { document.removeEventListener('mousemove', this.onDragMove); document.removeEventListener('mouseup', this.onDragEnd); document.removeEventListener('touchmove', this.onDragMove); document.removeEventListener('touchend', this.onDragEnd); }; _proto.render = function render() { var _this2 = this; var _this$props = this.props, closeButton = _this$props.closeButton, children = _this$props.children, autoClose = _this$props.autoClose, pauseOnHover = _this$props.pauseOnHover, closeOnClick = _this$props.closeOnClick, type = _this$props.type, hideProgressBar = _this$props.hideProgressBar, closeToast = _this$props.closeToast, Transition = _this$props.transition, position = _this$props.position, onExited = _this$props.onExited, className = _this$props.className, bodyClassName = _this$props.bodyClassName, progressClassName = _this$props.progressClassName, progressStyle = _this$props.progressStyle, updateId = _this$props.updateId, role = _this$props.role, progress = _this$props.progress, isProgressDone = _this$props.isProgressDone, rtl = _this$props.rtl; var toastProps = { className: (0, _classnames.default)('Toastify__toast', "Toastify__toast--" + type, { 'Toastify__toast--rtl': rtl }, className) }; if (autoClose && pauseOnHover) { toastProps.onMouseEnter = this.pauseToast; toastProps.onMouseLeave = this.playToast; } // prevent toast from closing when user drags the toast if (closeOnClick) { toastProps.onClick = function () { return _this2.flag.canCloseOnClick && closeToast(); }; } var controlledProgress = parseFloat(progress) === progress; return _react.default.createElement(Transition, { in: this.props.in, appear: true, unmountOnExit: true, onExited: onExited, position: position, preventExitTransition: this.state.preventExitTransition }, _react.default.createElement("div", _extends({}, toastProps, { ref: function ref(_ref) { return _this2.ref = _ref; }, onMouseDown: this.onDragStart, onTouchStart: this.onDragStart, onTransitionEnd: this.onDragTransitionEnd }), _react.default.createElement("div", _extends({}, this.props.in && { role: role }, { className: (0, _classnames.default)('Toastify__toast-body', bodyClassName) }), children), closeButton && closeButton, (autoClose || controlledProgress) && _react.default.createElement(_ProgressBar.default, _extends({}, updateId && !controlledProgress ? { key: "pb-" + updateId } : {}, { rtl: rtl, delay: autoClose, isRunning: this.state.isRunning, closeToast: closeToast, hide: hideProgressBar, type: type, style: progressStyle, className: progressClassName, controlledProgress: controlledProgress, isProgressDone: isProgressDone, progress: progress })))); }; return Toast; }(_react.Component); Toast.propTypes = { closeButton: _propValidator.falseOrElement.isRequired, autoClose: _propValidator.falseOrDelay.isRequired, children: _propTypes.default.node.isRequired, closeToast: _propTypes.default.func.isRequired, position: _propTypes.default.oneOf((0, _propValidator.objectValues)(_constant.POSITION)).isRequired, pauseOnHover: _propTypes.default.bool.isRequired, pauseOnFocusLoss: _propTypes.default.bool.isRequired, closeOnClick: _propTypes.default.bool.isRequired, transition: _propTypes.default.func.isRequired, rtl: _propTypes.default.bool.isRequired, hideProgressBar: _propTypes.default.bool.isRequired, draggable: _propTypes.default.bool.isRequired, draggablePercent: _propTypes.default.number.isRequired, in: _propTypes.default.bool, onExited: _propTypes.default.func, onOpen: _propTypes.default.func, onClose: _propTypes.default.func, type: _propTypes.default.oneOf((0, _propValidator.objectValues)(_constant.TYPE)), className: _propTypes.default.oneOfType([_propTypes.default.string, _propTypes.default.object]), bodyClassName: _propTypes.default.oneOfType([_propTypes.default.string, _propTypes.default.object]), progressClassName: _propTypes.default.oneOfType([_propTypes.default.string, _propTypes.default.object]), progressStyle: _propTypes.default.object, progress: _propTypes.default.number, isProgressDone: _propTypes.default.bool, updateId: _propTypes.default.oneOfType([_propTypes.default.string, _propTypes.default.number]), ariaLabel: _propTypes.default.string }; Toast.defaultProps = { type: _constant.TYPE.DEFAULT, in: true, onOpen: noop, onClose: noop, className: null, bodyClassName: null, progressClassName: null, updateId: null, role: 'alert' }; var _default = Toast; exports.default = _default;