UNPKG

d2-ui

Version:
288 lines (242 loc) 10.3 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _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; }; var _createClass = function () { function 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); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); var _simpleAssign = require('simple-assign'); var _simpleAssign2 = _interopRequireDefault(_simpleAssign); var _react = require('react'); var _react2 = _interopRequireDefault(_react); var _transitions = require('../styles/transitions'); var _transitions2 = _interopRequireDefault(_transitions); var _ClickAwayListener = require('../internal/ClickAwayListener'); var _ClickAwayListener2 = _interopRequireDefault(_ClickAwayListener); var _SnackbarBody = require('./SnackbarBody'); var _SnackbarBody2 = _interopRequireDefault(_SnackbarBody); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } function getStyles(props, context, state) { var _context$muiTheme = context.muiTheme; var desktopSubheaderHeight = _context$muiTheme.baseTheme.spacing.desktopSubheaderHeight; var zIndex = _context$muiTheme.zIndex; var open = state.open; var styles = { root: { position: 'fixed', left: 0, display: 'flex', right: 0, bottom: 0, zIndex: zIndex.snackbar, visibility: open ? 'visible' : 'hidden', transform: open ? 'translate3d(0, 0, 0)' : 'translate3d(0, ' + desktopSubheaderHeight + 'px, 0)', transition: _transitions2.default.easeOut('400ms', 'transform') + ', ' + _transitions2.default.easeOut('400ms', 'visibility') } }; return styles; } var Snackbar = function (_Component) { _inherits(Snackbar, _Component); function Snackbar() { var _Object$getPrototypeO; var _temp, _this, _ret; _classCallCheck(this, Snackbar); for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_Object$getPrototypeO = Object.getPrototypeOf(Snackbar)).call.apply(_Object$getPrototypeO, [this].concat(args))), _this), _this.componentClickAway = function () { if (_this.timerTransitionId) { // If transitioning, don't close the snackbar. return; } if (_this.props.open !== null && _this.props.onRequestClose) { _this.props.onRequestClose('clickaway'); } else { _this.setState({ open: false }); } }, _temp), _possibleConstructorReturn(_this, _ret); } _createClass(Snackbar, [{ key: 'componentWillMount', value: function componentWillMount() { this.setState({ open: this.props.open, message: this.props.message, action: this.props.action }); } }, { key: 'componentDidMount', value: function componentDidMount() { if (this.state.open) { this.setAutoHideTimer(); this.setTransitionTimer(); } } }, { key: 'componentWillReceiveProps', value: function componentWillReceiveProps(nextProps) { var _this2 = this; if (this.state.open && nextProps.open === this.props.open && (nextProps.message !== this.props.message || nextProps.action !== this.props.action)) { this.setState({ open: false }); clearTimeout(this.timerOneAtTheTimeId); this.timerOneAtTheTimeId = setTimeout(function () { _this2.setState({ message: nextProps.message, action: nextProps.action, open: true }); }, 400); } else { var open = nextProps.open; this.setState({ open: open !== null ? open : this.state.open, message: nextProps.message, action: nextProps.action }); } } }, { key: 'componentDidUpdate', value: function componentDidUpdate(prevProps, prevState) { if (prevState.open !== this.state.open) { if (this.state.open) { this.setAutoHideTimer(); this.setTransitionTimer(); } else { clearTimeout(this.timerAutoHideId); } } } }, { key: 'componentWillUnmount', value: function componentWillUnmount() { clearTimeout(this.timerAutoHideId); clearTimeout(this.timerTransitionId); clearTimeout(this.timerOneAtTheTimeId); } }, { key: 'setAutoHideTimer', // Timer that controls delay before snackbar auto hides value: function setAutoHideTimer() { var _this3 = this; var autoHideDuration = this.props.autoHideDuration; if (autoHideDuration > 0) { clearTimeout(this.timerAutoHideId); this.timerAutoHideId = setTimeout(function () { if (_this3.props.open !== null && _this3.props.onRequestClose) { _this3.props.onRequestClose('timeout'); } else { _this3.setState({ open: false }); } }, autoHideDuration); } } // Timer that controls delay before click-away events are captured (based on when animation completes) }, { key: 'setTransitionTimer', value: function setTransitionTimer() { var _this4 = this; this.timerTransitionId = setTimeout(function () { _this4.timerTransitionId = undefined; }, 400); } }, { key: 'render', value: function render() { var _props = this.props; var onActionTouchTap = _props.onActionTouchTap; var style = _props.style; var bodyStyle = _props.bodyStyle; var others = _objectWithoutProperties(_props, ['onActionTouchTap', 'style', 'bodyStyle']); var _state = this.state; var action = _state.action; var message = _state.message; var open = _state.open; var prepareStyles = this.context.muiTheme.prepareStyles; var styles = getStyles(this.props, this.context, this.state); return _react2.default.createElement( _ClickAwayListener2.default, { onClickAway: open && this.componentClickAway }, _react2.default.createElement( 'div', _extends({}, others, { style: prepareStyles((0, _simpleAssign2.default)(styles.root, style)) }), _react2.default.createElement(_SnackbarBody2.default, { open: open, message: message, action: action, style: bodyStyle, onActionTouchTap: onActionTouchTap }) ) ); } }]); return Snackbar; }(_react.Component); Snackbar.propTypes = { /** * The label for the action on the snackbar. */ action: _react.PropTypes.string, /** * The number of milliseconds to wait before automatically dismissing. * If no value is specified the snackbar will dismiss normally. * If a value is provided the snackbar can still be dismissed normally. * If a snackbar is dismissed before the timer expires, the timer will be cleared. */ autoHideDuration: _react.PropTypes.number, /** * Override the inline-styles of the body element. */ bodyStyle: _react.PropTypes.object, /** * The css class name of the root element. */ className: _react.PropTypes.string, /** * The message to be displayed. * * (Note: If the message is an element or array, and the `Snackbar` may re-render while it is still open, * ensure that the same object remains as the `message` property if you want to avoid the `Snackbar` hiding and * showing again) */ message: _react.PropTypes.node.isRequired, /** * Fired when the action button is touchtapped. * * @param {object} event Action button event. */ onActionTouchTap: _react.PropTypes.func, /** * Fired when the `Snackbar` is requested to be closed by a click outside the `Snackbar`, or after the * `autoHideDuration` timer expires. * * Typically `onRequestClose` is used to set state in the parent component, which is used to control the `Snackbar` * `open` prop. * * The `reason` parameter can optionally be used to control the response to `onRequestClose`, * for example ignoring `clickaway`. * * @param {string} reason Can be:`"timeout"` (`autoHideDuration` expired) or: `"clickaway"` */ onRequestClose: _react.PropTypes.func, /** * Controls whether the `Snackbar` is opened or not. */ open: _react.PropTypes.bool.isRequired, /** * Override the inline-styles of the root element. */ style: _react.PropTypes.object }; Snackbar.contextTypes = { muiTheme: _react.PropTypes.object.isRequired }; exports.default = Snackbar;