rmwc
Version:
A thin React wrapper for Material Design (Web) Components
225 lines (184 loc) • 9.51 kB
JavaScript
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.Snackbar = exports.SnackbarActionButton = exports.SnackbarActionWrapper = exports.SnackbarText = exports.SnackbarRoot = undefined;
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
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 _react = require('react');
var React = _interopRequireWildcard(_react);
var _mdc = require('@material/snackbar/dist/mdc.snackbar');
var _mdc2 = require('@material/animation/dist/mdc.animation');
var _Button = require('../Button');
var _Button2 = _interopRequireDefault(_Button);
var _Base = require('../Base');
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)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
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; } /**
* Shows in browser notifications
*/
var SnackbarRoot = exports.SnackbarRoot = (0, _Base.simpleTag)({
displayName: 'SnackbarRoot',
classNames: function classNames(props) {
return ['mdc-snackbar', {
'mdc-snackbar--align-start': props.alignStart
}];
},
defaultProps: {
alignStart: false,
'aria-live': 'assertive',
'aria-atomic': true,
'aria-hidden': true
},
consumeProps: ['alignStart']
});
var SnackbarText = exports.SnackbarText = (0, _Base.simpleTag)({
displayName: 'SnackbarText',
classNames: 'mdc-snackbar__text'
});
var SnackbarActionWrapper = exports.SnackbarActionWrapper = (0, _Base.simpleTag)({
displayName: 'SnackbarActionWrapper',
classNames: 'mdc-snackbar__action-wrapper'
});
var SnackbarActionButton = exports.SnackbarActionButton = (0, _Base.simpleTag)({
displayName: 'SnackbarActionButton',
tag: _Button2.default,
classNames: 'mdc-snackbar__action-button'
});
/**
* A Snackbar component for notifications.
*/
var Snackbar = exports.Snackbar = function (_withFoundation) {
_inherits(Snackbar, _withFoundation);
function Snackbar() {
var _ref;
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, (_ref = Snackbar.__proto__ || Object.getPrototypeOf(Snackbar)).call.apply(_ref, [this].concat(args))), _this), Object.defineProperty(_this, 'isShowing_', {
enumerable: true,
writable: true,
value: false
}), _temp), _possibleConstructorReturn(_this, _ret);
}
_createClass(Snackbar, [{
key: 'syncWithProps',
value: function syncWithProps(nextProps) {
var _this2 = this;
(0, _Base.syncFoundationProp)(nextProps.dismissesOnAction, this.dismissesOnAction, function () {
return _this2.dismissesOnAction = nextProps.dismissesOnAction;
});
(0, _Base.syncFoundationProp)(nextProps.show, this.isShowing, function () {
var message = nextProps.message,
timeout = nextProps.timeout,
actionHandler = nextProps.actionHandler,
actionText = nextProps.actionText,
multiline = nextProps.multiline,
actionOnBottom = nextProps.actionOnBottom;
_this2.show({
message: message,
timeout: timeout,
actionHandler: actionHandler,
actionText: actionText || ' ',
multiline: multiline,
actionOnBottom: actionOnBottom
});
});
}
}, {
key: 'render',
value: function render() {
var _props = this.props,
show = _props.show,
message = _props.message,
timeout = _props.timeout,
actionHandler = _props.actionHandler,
actionText = _props.actionText,
multiline = _props.multiline,
actionOnBottom = _props.actionOnBottom,
dismissesOnAction = _props.dismissesOnAction,
onHide = _props.onHide,
onShow = _props.onShow,
children = _props.children,
rest = _objectWithoutProperties(_props, ['show', 'message', 'timeout', 'actionHandler', 'actionText', 'multiline', 'actionOnBottom', 'dismissesOnAction', 'onHide', 'onShow', 'children']);
var root_ = this.foundationRefs.root_;
var isJSX = (typeof message === 'undefined' ? 'undefined' : _typeof(message)) === 'object';
var snackbarTextStyle = {};
if (isJSX) {
snackbarTextStyle.display = 'none';
}
var snackbarActionWrapperStyle = !actionText ? {
display: 'none'
} : {};
/**
* The double SnackbarText below is a hack to allow for rendering JSX
* The real message gets rendered in the hidden container, and the second one is
* ignored and shows th rendered content :)
*/
return React.createElement(
SnackbarRoot,
Object.assign({}, rest, { elementRef: root_ }),
React.createElement(SnackbarText, { style: snackbarTextStyle }),
isJSX && React.createElement(
SnackbarText,
null,
message
),
React.createElement(
SnackbarActionWrapper,
{ style: snackbarActionWrapperStyle },
React.createElement(SnackbarActionButton, null)
),
children
);
}
}, {
key: 'isShowing',
get: function get() {
return this.isShowing_;
},
set: function set(isShowing) {
this.isShowing_ = isShowing;
}
}]);
return Snackbar;
}((0, _Base.withFoundation)({
constructor: _mdc.MDCSnackbar,
adapter: {
removeClass: function removeClass(className) {
// only remove if we still have a reference to our root.
this.root_ && this.root_.classList.remove(className);
},
registerTransitionEndHandler: function registerTransitionEndHandler(handler) {
// only add if we still have a reference to our root.
this.root_ && this.root_.addEventListener((0, _mdc2.getCorrectEventName)(window, 'transitionend'), handler);
}
}
}));
Object.defineProperty(Snackbar, 'displayName', {
enumerable: true,
writable: true,
value: 'Snackbar'
});
Object.defineProperty(Snackbar, 'defaultProps', {
enumerable: true,
writable: true,
value: {
show: false,
onHide: _Base.noop,
onShow: _Base.noop,
message: undefined,
timeout: undefined,
actionHandler: undefined,
actionText: undefined,
multiline: false,
actionOnBottom: false,
dismissesOnAction: true
}
});
exports.default = Snackbar;
;