beta-parity-react
Version:
Beta Parity React Components
1 lines • 9.88 kB
JavaScript
;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.ToastContainer=void 0;var _react=_interopRequireWildcard(require("react"));var _reactDom=_interopRequireDefault(require("react-dom"));require("./variables.css");require("./index.css");var _Toast=require("./Toast");var _jsxRuntime=require("react/jsx-runtime");var _excluded=["limit","stacked","sortImportance"];function _interopRequireDefault(obj){return obj&&obj.__esModule?obj:{"default":obj}}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 _defineProperty(obj,key,value){key=_toPropertyKey(key);if(key in obj){Object.defineProperty(obj,key,{value:value,enumerable:true,configurable:true,writable:true})}else{obj[key]=value}return obj}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 _toConsumableArray(arr){return _arrayWithoutHoles(arr)||_iterableToArray(arr)||_unsupportedIterableToArray(arr)||_nonIterableSpread()}function _nonIterableSpread(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function _iterableToArray(iter){if(typeof Symbol!=="undefined"&&iter[Symbol.iterator]!=null||iter["@@iterator"]!=null)return Array.from(iter)}function _arrayWithoutHoles(arr){if(Array.isArray(arr))return _arrayLikeToArray(arr)}function _slicedToArray(arr,i){return _arrayWithHoles(arr)||_iterableToArrayLimit(arr,i)||_unsupportedIterableToArray(arr,i)||_nonIterableRest()}function _nonIterableRest(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function _unsupportedIterableToArray(o,minLen){if(!o)return;if(typeof o==="string")return _arrayLikeToArray(o,minLen);var n=Object.prototype.toString.call(o).slice(8,-1);if(n==="Object"&&o.constructor)n=o.constructor.name;if(n==="Map"||n==="Set")return Array.from(o);if(n==="Arguments"||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n))return _arrayLikeToArray(o,minLen)}function _arrayLikeToArray(arr,len){if(len==null||len>arr.length)len=arr.length;for(var i=0,arr2=new Array(len);i<len;i++)arr2[i]=arr[i];return arr2}function _iterableToArrayLimit(r,l){var t=null==r?null:"undefined"!=typeof Symbol&&r[Symbol.iterator]||r["@@iterator"];if(null!=t){var e,n,i,u,a=[],f=!0,o=!1;try{if(i=(t=t.call(r)).next,0===l){if(Object(t)!==t)return;f=!1}else for(;!(f=(e=i.call(t)).done)&&(a.push(e.value),a.length!==l);f=!0);}catch(r){o=!0,n=r}finally{try{if(!f&&null!=t["return"]&&(u=t["return"](),Object(u)!==u))return}finally{if(o)throw n}}return a}}function _arrayWithHoles(arr){if(Array.isArray(arr))return arr}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={};for(var key in source){if(Object.prototype.hasOwnProperty.call(source,key)){if(excluded.indexOf(key)>=0)continue;target[key]=source[key]}}return target}var ToastContainer=exports.ToastContainer=function ToastContainer(_ref){var _ref$limit=_ref.limit,limit=_ref$limit===void 0?1:_ref$limit,_ref$stacked=_ref.stacked,stacked=_ref$stacked===void 0?true:_ref$stacked,_ref$sortImportance=_ref.sortImportance,sortImportance=_ref$sortImportance===void 0?true:_ref$sortImportance,props=_objectWithoutProperties(_ref,_excluded);var _useState=(0,_react.useState)(false),_useState2=_slicedToArray(_useState,2),isClient=_useState2[0],setIsClient=_useState2[1];var _useState3=(0,_react.useState)([]),_useState4=_slicedToArray(_useState3,2),toasts=_useState4[0],setToasts=_useState4[1];var removeToast=(0,_react.useCallback)(function(id){setToasts(function(currentToasts){return currentToasts.filter(function(toast){return toast.id!==id})})},[]);(0,_react.useEffect)(function(){var handleAddToast=function handleAddToast(event){setToasts(function(currentToasts){return[].concat(_toConsumableArray(currentToasts),[event.detail])})};var handleRemoveToast=function handleRemoveToast(event){setToasts(function(currentToasts){return currentToasts.filter(function(toast){return toast.id!==event.detail.id})})};var handleUpdateToast=function handleUpdateToast(event){setToasts(function(currentToasts){return currentToasts.map(function(toast){return toast.id===event.detail.id?_objectSpread(_objectSpread({},toast),event.detail):toast})})};window.addEventListener("parity-add-toast",handleAddToast);window.addEventListener("parity-remove-toast",handleRemoveToast);window.addEventListener("parity-update-toast",handleUpdateToast);return function(){window.removeEventListener("parity-add-toast",handleAddToast);window.removeEventListener("parity-remove-toast",handleRemoveToast);window.removeEventListener("parity-update-toast",handleUpdateToast)}},[]);var sortedToasts=sortImportance?toasts.sort(function(a,b){var _a$importance,_b$importance;return((_a$importance=a.importance)!==null&&_a$importance!==void 0?_a$importance:0)-((_b$importance=b.importance)!==null&&_b$importance!==void 0?_b$importance:0)}):toasts;var topRightToasts=sortedToasts.filter(function(toast){return toast.position==="top-right"});var topCenterToasts=sortedToasts.filter(function(toast){return toast.position==="top-center"});var bottomRightToasts=sortedToasts.filter(function(toast){return toast.position==="bottom-right"});var bottomCenterToasts=sortedToasts.filter(function(toast){return toast.position==="bottom-center"});(0,_react.useEffect)(function(){setIsClient(true)},[]);if(!isClient)return null;return(0,_jsxRuntime.jsxs)(_jsxRuntime.Fragment,{children:[topRightToasts.length?_reactDom["default"].createPortal((0,_jsxRuntime.jsx)("div",_objectSpread(_objectSpread({className:"toast-container top-right"},props),{},{children:generateToast(topRightToasts,removeToast,limit,stacked)})),document.body):null,topCenterToasts.length?_reactDom["default"].createPortal((0,_jsxRuntime.jsx)("div",_objectSpread(_objectSpread({className:"toast-container top-center"},props),{},{children:generateToast(topCenterToasts,removeToast,limit,stacked)})),document.body):null,bottomRightToasts.length?_reactDom["default"].createPortal((0,_jsxRuntime.jsx)("div",_objectSpread(_objectSpread({className:"toast-container bottom-right"},props),{},{children:generateToast(bottomRightToasts,removeToast,limit,stacked)})),document.body):null,bottomCenterToasts.length?_reactDom["default"].createPortal((0,_jsxRuntime.jsx)("div",_objectSpread(_objectSpread({className:"toast-container bottom-center"},props),{},{children:generateToast(bottomCenterToasts,removeToast,limit,stacked)})),document.body):null]})};var generateToast=function generateToast(toasts,removeToast,limit,stacked){var LAYER=3;var reversedIndex=function reversedIndex(index){return toasts.length-(index+1)};var scaleRatio=function scaleRatio(index){return 1-0.05*reversedIndex(index)};var recededBottom=function recededBottom(index){return-10*reversedIndex(index)};var isShowing=function isShowing(index){return index>=toasts.length-LAYER};if(!stacked)return toasts.length&&toasts.map(function(toast){return(0,_jsxRuntime.jsx)(_Toast.Toast,_objectSpread(_objectSpread({},toast),{},{removeToast:removeToast}),toast.id)}).splice(-limit).reverse();return toasts.length>limit?toasts.map(function(toast,index){return(0,_jsxRuntime.jsx)(_Toast.Toast,_objectSpread(_objectSpread({},toast),{},{removeToast:removeToast,className:index!==toasts.length-1?"collapsed":"",style:{top:isShowing(index)?recededBottom(index):0,scale:isShowing(index)?scaleRatio(index):0}}),toast.id)}):toasts.map(function(toast){return(0,_jsxRuntime.jsx)(_Toast.Toast,_objectSpread(_objectSpread({},toast),{},{removeToast:removeToast}),toast.id)})};