UNPKG

@catho/quantum

Version:
295 lines (294 loc) 16.3 kB
"use strict"; function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); } Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = void 0; var _react = require("react"); var _reactDom = require("react-dom"); var _styledComponents = _interopRequireWildcard(require("styled-components")); var _propTypes = _interopRequireDefault(require("prop-types")); var _shared = require("../Grid/sub-components/shared"); var _shared2 = require("../shared"); var _Button = _interopRequireDefault(require("../Button")); var _Grid = require("../Grid"); var _uniqId = require("../shared/uniqId"); var _theme = require("../shared/theme"); var _Icon = _interopRequireDefault(require("../Icon/Icon")); var _IconTypes = require("./IconTypes"); var _jsxRuntime = require("react/jsx-runtime"); var _excluded = ["text", "onClose", "closeButtonAriaLabel", "actionTrigger", "theme", "skin", "inverted", "hideIcon"]; var _templateObject; function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; } function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); } function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != _typeof(e) && "function" != typeof e) return { "default": e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n["default"] = e, t && t.set(e, n), n; } function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } function _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var n = Object.getOwnPropertySymbols(e); for (r = 0; r < n.length; r++) o = n[r], t.indexOf(o) >= 0 || {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; } function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (e.indexOf(n) >= 0) continue; t[n] = r[n]; } return t; } function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); } function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } } function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; } function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); } function _possibleConstructorReturn(t, e) { if (e && ("object" == _typeof(e) || "function" == typeof e)) return e; if (void 0 !== e) throw new TypeError("Derived constructors may only return object or undefined"); return _assertThisInitialized(t); } function _assertThisInitialized(e) { if (void 0 === e) throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); return e; } function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); } function _getPrototypeOf(t) { return _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function (t) { return t.__proto__ || Object.getPrototypeOf(t); }, _getPrototypeOf(t); } function _inherits(t, e) { if ("function" != typeof e && null !== e) throw new TypeError("Super expression must either be null or a function"); t.prototype = Object.create(e && e.prototype, { constructor: { value: t, writable: !0, configurable: !0 } }), Object.defineProperty(t, "prototype", { writable: !1 }), e && _setPrototypeOf(t, e); } function _setPrototypeOf(t, e) { return _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function (t, e) { return t.__proto__ = e, t; }, _setPrototypeOf(t, e); } function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; } function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } function _taggedTemplateLiteral(e, t) { return t || (t = e.slice(0)), Object.freeze(Object.defineProperties(e, { raw: { value: Object.freeze(t) } })); } var mediaQueries = (0, _shared.query)(_shared2.BREAKPOINTS); var uniqId = (0, _uniqId.createUniqId)('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-myzzmd-0" })(["display:flex;align-items:center;justify-content:flex-start;border-radius:4px;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 (0, _styledComponents.css)(["background-color:", ";color:", ";a{color:inherit;text-decoration:underline;}"], background, text); }, a11yFocusTab); var TextContainer = _styledComponents["default"].span.withConfig({ displayName: "SnackBar__TextContainer", componentId: "sc-myzzmd-1" })(["flex:1;width:100%;& > p{margin:0;}&:focus{", "}"], a11yFocusTab); var SkinIcon = (0, _styledComponents["default"])(_Icon["default"]).withConfig({ displayName: "SnackBar__SkinIcon", componentId: "sc-myzzmd-2" })(["", ""], function (_ref3) { var skin = _ref3.skin, _ref3$theme = _ref3.theme, iconColor = _ref3$theme.components.snackbar.skins[skin].iconColor, small = _ref3$theme.spacing.small; return "\n color: ".concat(iconColor, ";\n margin-right: ").concat(small, "px;\n "); }); var CloseButton = (0, _styledComponents["default"])(_Button["default"].Icon).attrs({ icon: 'close' }).withConfig({ displayName: "SnackBar__CloseButton", componentId: "sc-myzzmd-3" })(["", ""], function (_ref4) { var skin = _ref4.skin, theme = _ref4.theme, inverted = _ref4.inverted; var _getBackgroundAndText3 = getBackgroundAndTextColorBySkin({ skin: skin, theme: theme, inverted: inverted }), text = _getBackgroundAndText3.text; var small = theme.spacing.small; return (0, _styledComponents.css)(["margin-left:", "px;color:", ";&:hover,&:focus{background:transparent;box-shadow:none;color:", ";", ";}"], small, text, text, a11yFocusTab); }); var ActionButton = (0, _styledComponents["default"])(_Button["default"]).withConfig({ displayName: "SnackBar__ActionButton", componentId: "sc-myzzmd-4" })(["", ""], function (_ref5) { var theme = _ref5.theme, skin = _ref5.skin, inverted = _ref5.inverted; var _getBackgroundAndText4 = getBackgroundAndTextColorBySkin({ skin: skin, theme: theme, inverted: inverted }), color = _getBackgroundAndText4.text; var small = theme.spacing.small; return (0, _styledComponents.css)(["background:", ";margin-left:", "px;box-shadow:none;color:", ";border-width:0;border-color:unset;&:hover,&:focus{background:", ";box-shadow:none;color:", ";}", ""], (0, _shared2.hexToRgba)(color, 0.15), small, color, (0, _shared2.hexToRgba)(color, 0.15), color, mediaQueries.medium(_templateObject || (_templateObject = _taggedTemplateLiteral(["\n background-color: transparent;\n "])))); }); var DialogBlock = _styledComponents["default"].section.withConfig({ displayName: "SnackBar__DialogBlock", componentId: "sc-myzzmd-5" })(["bottom:0;left:0;position:fixed;width:100%;z-index:98;"]); var SnackBar = /*#__PURE__*/function (_Component) { function SnackBar(props) { var _this; _classCallCheck(this, SnackBar); _this = _callSuper(this, SnackBar, [props]); _defineProperty(_this, "handleKeyPress", function (event) { var onClose = _this.props.onClose; if (event.key === 'Enter') onClose(); }); _defineProperty(_this, "objectFocus", function (element) { if (element && element !== null) element.focus(); }); _defineProperty(_this, "closeOnTime", function (secondsToClose, callback) { _this.closeEventtimeOut = setTimeout(function () { callback(); }, secondsToClose * 1000); }); _defineProperty(_this, "handleSkinIcon", function (theme, skin) { return skin !== 'neutral' ? /*#__PURE__*/(0, _jsxRuntime.jsx)(SkinIcon, { theme: theme, name: (0, _IconTypes.GetSkinIcon)(skin), skin: skin }) : false; }); var id = props.id; _this.snackBarSection = document.createElement('section'); _this._id = id || uniqId(); return _this; } _inherits(SnackBar, _Component); return _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, hideIcon = _this$props2.hideIcon, rest = _objectWithoutProperties(_this$props2, _excluded); return /*#__PURE__*/(0, _reactDom.createPortal)( /*#__PURE__*/(0, _jsxRuntime.jsx)(DialogBlock, { children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_Grid.Row, { children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_Grid.Col, { "xsmall-offset": 0, xsmall: 4, "small-offset": 1, small: 6, "medium-offset": 1, medium: 10, children: /*#__PURE__*/(0, _jsxRuntime.jsxs)(SnackBarDialog, _objectSpread(_objectSpread({}, rest), {}, { theme: theme, inverted: inverted, skin: skin, role: "alertdialog", "aria-describedby": this._id, tabIndex: "0", children: [!hideIcon && this.handleSkinIcon(theme, skin), /*#__PURE__*/(0, _jsxRuntime.jsx)(TextContainer, { id: this._id, inverted: inverted, theme: theme, skin: skin, children: text }), actionTrigger && /*#__PURE__*/(0, _jsxRuntime.jsx)(ActionButton, { className: "action-button", inverted: inverted, theme: theme, skin: skin, onClick: actionTrigger.callbackFn, children: actionTrigger.title }), onClose && /*#__PURE__*/(0, _jsxRuntime.jsx)(CloseButton, { theme: theme, inverted: inverted, skin: skin, onClick: onClose, "aria-label": closeButtonAriaLabel, onKeyPress: this.handleKeyPress })] })) }) }) }), this.snackBarSection); } }]); }(_react.Component); CloseButton.displayName = 'CloseButton'; SkinIcon.displayName = 'SkinIcon'; ActionButton.displayName = 'ActionButton'; SnackBarDialog.displayName = 'SnackBarDialog'; TextContainer.displayName = 'TextContainer'; SnackBar.propTypes = { actionTrigger: _propTypes["default"].shape({ title: _propTypes["default"].string.isRequired, callbackFn: _propTypes["default"].func.isRequired }), 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, hideIcon: _propTypes["default"].bool, onClose: _propTypes["default"].func, secondsToClose: _propTypes["default"].number, skin: _propTypes["default"].oneOf(['primary', 'success', 'error', 'neutral', 'warning']), text: _propTypes["default"].oneOfType([_propTypes["default"].string, _propTypes["default"].element]), /** 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: undefined, hideIcon: false, 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 = exports["default"] = SnackBar;