UNPKG

@age/quantum

Version:
340 lines (285 loc) 15 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _react = _interopRequireDefault(require("react")); var _reactDom = _interopRequireDefault(require("react-dom")); var _styledComponents = _interopRequireWildcard(require("styled-components")); var _propTypes = _interopRequireDefault(require("prop-types")); var _Button = _interopRequireDefault(require("../Button")); var _Grid = require("../Grid"); var _uniqId = _interopRequireDefault(require("../shared/uniqId")); var _theme = require("../shared/theme"); var _Icon = _interopRequireDefault(require("../Icon/Icon")); var _IconTypes = require("./IconTypes"); function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; } function _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; } function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _typeof(obj) { 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); } 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 _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; } function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a 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); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); } function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); } function _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); } function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); } var ID_GENERATOR = (0, _uniqId.default)('snackbar-dialog-'); var getBackgroundAndTextColorBySkin = function getBackgroundAndTextColorBySkin(_ref) { var skin = _ref.skin, _ref$theme$components = _ref.theme.components.snackbar.skins[skin], text = _ref$theme$components.text, background = _ref$theme$components.background, inverted = _ref.inverted; if (inverted) { var textInverted = background, backgroundInverted = text; return { background: backgroundInverted, text: textInverted }; } return { background: background, text: text }; }; var a11yFocusTab = function a11yFocusTab(props) { var _getBackgroundAndText = getBackgroundAndTextColorBySkin(props), text = _getBackgroundAndText.text; return "\n outline: 1px solid ".concat(text, ";\n "); }; var SnackBarDialog = _styledComponents.default.div.withConfig({ displayName: "SnackBar__SnackBarDialog", componentId: "sc-1sru709-0" })(["align-items:center;border-radius:4px;display:flex;justify-content:space-between;min-height:48px;", " ", ":focus{", "}"], function (_ref2) { var _ref2$theme$spacing = _ref2.theme.spacing, xsmall = _ref2$theme$spacing.xsmall, small = _ref2$theme$spacing.small, medium = _ref2$theme$spacing.medium; return "\n padding: ".concat(small, "px ").concat(medium, "px;\n margin: 0 ").concat(xsmall, "px;\n "); }, function (props) { var _getBackgroundAndText2 = getBackgroundAndTextColorBySkin(props), background = _getBackgroundAndText2.background, text = _getBackgroundAndText2.text; return "\n background-color: ".concat(background, ";\n color: ").concat(text, ";\n "); }, a11yFocusTab); var TextContainer = _styledComponents.default.strong.withConfig({ displayName: "SnackBar__TextContainer", componentId: "sc-1sru709-1" })(["font-weight:normal;", ":focus{", "}"], function (_ref3) { var xsmall = _ref3.theme.spacing.xsmall; return "\n padding-right: ".concat(xsmall, "px;\n "); }, a11yFocusTab); var SkinIcon = (0, _styledComponents.default)(_Icon.default).withConfig({ displayName: "SnackBar__SkinIcon", componentId: "sc-1sru709-2" })(["", ""], function (_ref4) { var skin = _ref4.skin, _ref4$theme = _ref4.theme, iconColor = _ref4$theme.components.snackbar.skins[skin].iconColor, small = _ref4$theme.spacing.small; return "\n color: ".concat(iconColor, ";\n margin-right: ").concat(small, "px;\n "); }); var CloseIconRight = (0, _styledComponents.default)(_Button.default.Icon).attrs({ icon: 'close' }).withConfig({ displayName: "SnackBar__CloseIconRight", componentId: "sc-1sru709-3" })(["", ""], function (props) { var _getBackgroundAndText3 = getBackgroundAndTextColorBySkin(props), text = _getBackgroundAndText3.text; return (0, _styledComponents.css)(["color:", ";:hover,:focus{background-color:transparent;box-shadow:none;color:", ";", "}"], text, text, a11yFocusTab); }); var CloseIconLeft = (0, _styledComponents.default)(CloseIconRight).withConfig({ displayName: "SnackBar__CloseIconLeft", componentId: "sc-1sru709-4" })(["", ""], function (_ref5) { var xsmall = _ref5.theme.spacing.xsmall; return "\n margin-right: ".concat(xsmall, "px;\n "); }); var WrapperLeft = _styledComponents.default.div.withConfig({ displayName: "SnackBar__WrapperLeft", componentId: "sc-1sru709-5" })(["display:inherit;align-items:center;"]); var ActionButton = (0, _styledComponents.default)(_Button.default).withConfig({ displayName: "SnackBar__ActionButton", componentId: "sc-1sru709-6" })([""]); var ActionsSection = _styledComponents.default.div.withConfig({ displayName: "SnackBar__ActionsSection", componentId: "sc-1sru709-7" })(["align-items:center;display:flex;flex:none;"]); var DialogBlock = _styledComponents.default.section.withConfig({ displayName: "SnackBar__DialogBlock", componentId: "sc-1sru709-8" })(["bottom:0;left:0;position:fixed;width:100%;"]); var SnackBar = /*#__PURE__*/ function (_React$Component) { _inherits(SnackBar, _React$Component); function SnackBar(props) { var _this; _classCallCheck(this, SnackBar); _this = _possibleConstructorReturn(this, _getPrototypeOf(SnackBar).call(this, props)); _this.handleKeyPress = function (event) { var onClose = _this.props.onClose; if (event.key === 'Enter') onClose(); }; _this.objectFocus = function (element) { if (element && element !== null) element.focus(); }; _this.closeOnTime = function (secondsToClose, callback) { _this.closeEventtimeOut = setTimeout(function () { callback(); }, secondsToClose * 1000); }; _this.handleSkinIcon = function (theme, skin) { return skin !== 'neutral' ? _react.default.createElement(SkinIcon, { theme: theme, name: (0, _IconTypes.GetSkinIcon)(skin), skin: skin }) : false; }; var id = props.id; _this.snackBarSection = document.createElement('section'); _this._id = id || ID_GENERATOR.next().value; return _this; } _createClass(SnackBar, [{ key: "componentDidMount", value: function componentDidMount() { var _document = document, body = _document.body; body.appendChild(this.snackBarSection); this.objectFocus(document.querySelector('.action-button')); var _this$props = this.props, onClose = _this$props.onClose, secondsToClose = _this$props.secondsToClose; this.closeOnTime(secondsToClose, onClose); } }, { key: "componentWillUnmount", value: function componentWillUnmount() { var _document2 = document, body = _document2.body; body.removeChild(this.snackBarSection); clearTimeout(this.closeEventtimeOut); } }, { key: "render", value: function render() { var _this$props2 = this.props, text = _this$props2.text, onClose = _this$props2.onClose, closeButtonAriaLabel = _this$props2.closeButtonAriaLabel, actionTrigger = _this$props2.actionTrigger, theme = _this$props2.theme, skin = _this$props2.skin, inverted = _this$props2.inverted, rest = _objectWithoutProperties(_this$props2, ["text", "onClose", "closeButtonAriaLabel", "actionTrigger", "theme", "skin", "inverted"]); var isActionTriggerDiffAction = actionTrigger && actionTrigger.title !== 'ACTION' && skin === 'neutral'; return _reactDom.default.createPortal(_react.default.createElement(DialogBlock, null, _react.default.createElement(_Grid.Row, null, _react.default.createElement(_Grid.Col, { "xsmall-offset": 0, xsmall: 4, "small-offset": 1, small: 6, "medium-offset": 1, medium: 10 }, _react.default.createElement(SnackBarDialog, _extends({}, rest, { theme: theme, inverted: inverted, skin: skin, role: "alertdialog", "aria-describedby": this._id, tabIndex: "0" }), _react.default.createElement(WrapperLeft, null, isActionTriggerDiffAction ? _react.default.createElement(CloseIconLeft, { theme: theme, inverted: inverted, skin: skin, onClick: onClose, "aria-label": closeButtonAriaLabel, onKeyPress: this.handleKeyPress }) : this.handleSkinIcon(theme, skin), _react.default.createElement(TextContainer, { id: this._id, inverted: inverted, theme: theme, skin: skin }, text)), _react.default.createElement(ActionsSection, null, isActionTriggerDiffAction ? _react.default.createElement(ActionButton, { className: "action-button", inverted: inverted, theme: theme, skin: "secondary", onClick: actionTrigger.callbackFn }, actionTrigger.title) : onClose && _react.default.createElement(CloseIconRight, { theme: theme, inverted: inverted, skin: skin, onClick: onClose, "aria-label": closeButtonAriaLabel, onKeyPress: this.handleKeyPress })))))), this.snackBarSection); } }]); return SnackBar; }(_react.default.Component); CloseIconLeft.displayName = 'CloseIconLeft'; CloseIconRight.displayName = 'CloseIconRight'; SkinIcon.displayName = 'SkinIcon'; ActionButton.displayName = 'ActionButton'; SnackBarDialog.displayName = 'SnackBarDialog'; TextContainer.displayName = 'TextContainer'; SnackBar.propTypes = { /** This prop works only on neutral skin */ actionTrigger: _propTypes.default.shape({ title: _propTypes.default.string, callback: _propTypes.default.func }), theme: _propTypes.default.shape({ baseFontSize: _propTypes.default.number, colors: _propTypes.default.object, breakpoints: _propTypes.default.object, spacing: _propTypes.default.object, components: _propTypes.default.shape({ snackbar: _propTypes.default.object, button: _propTypes.default.object }) }), closeButtonAriaLabel: _propTypes.default.string, onClose: _propTypes.default.func, secondsToClose: _propTypes.default.number, skin: _propTypes.default.oneOf(['primary', 'success', 'error', 'neutral', 'warning']), text: _propTypes.default.string, /** Swap background and text color */ inverted: _propTypes.default.bool, id: _propTypes.default.string }; SnackBar.defaultProps = { closeButtonAriaLabel: 'close SnackBar', onClose: function onClose() {}, secondsToClose: 6, skin: 'neutral', text: 'Text of SnackBar component', actionTrigger: { title: 'ACTION', callbackFn: function callbackFn() {} }, inverted: false, id: undefined, theme: { colors: _theme.colors, baseFontSize: _theme.baseFontSize, spacing: _theme.spacing, breakpoints: _theme.breakpoints, components: { snackbar: _theme.components.snackbar, button: _theme.components.button } } }; var _default = SnackBar; exports.default = _default;