UNPKG

rinshad-react-guard

Version:

A robust React error boundary solution developed by [rinshad.com](https://rinshad.com) to elegantly handle runtime errors in React applications. Instead of showing the dreaded white screen of death, this library captures errors and displays a user-friendl

2 lines 10.8 kB
/*! For license information please see index.js.LICENSE.txt */ !function(e,r){"object"==typeof exports&&"object"==typeof module?module.exports=r(require("react")):"function"==typeof define&&define.amd?define("rinshadReactGuard",["react"],r):"object"==typeof exports?exports.rinshadReactGuard=r(require("react")):e.rinshadReactGuard=r(e.React)}(this,(e=>(()=>{"use strict";var r={233:(e,r,n)=>{n.d(r,{A:()=>s});var t=n(601),o=n.n(t),i=n(314),a=n.n(i)()(o());a.push([e.id,"\n\n.error-container {\n display: flex;\n justify-content: center;\n align-items: center;\n height: 100vh;\n background-color: #f8f9fa; /* Light background for contrast */\n}\n\n.error-content {\n text-align: center;\n}\n\n.error-500 {\n position: relative;\n}\n\n.error-500 .title {\n font-size: 250px;\n color: #6c757d; /* Muted text color */\n margin: 0;\n}\n\n.error-500-img {\n position: absolute;\n top: 20px;\n left: 50%;\n transform: translateX(-50%);\n width: 100%; /* Full width for responsiveness */\n max-width: 450px; /* Maximum width to maintain aspect ratio */\n}\n\n.error-message {\n margin-top: 40px;\n}\n\n.error-message h4 {\n font-size: 24px;\n color: #343a40; /* Dark text color */\n}\n\n.error-message p {\n color: #6c757d; /* Muted text color */\n}\n\n.back-home {\n display: inline-block;\n padding: 10px 20px;\n margin-top: 15px;\n background-color: #28a745; /* Success color */\n color: white;\n text-decoration: none;\n border-radius: 5px;\n transition: background-color 0.3s;\n}\n\n.back-home:hover {\n background-color: #218838; \n color: white;\n}\n\n/* Responsive styles */\n@media (max-width: 991.98px) {\n .error-500 .title {\n font-size: 150px; /* Reduce size for tablets */\n }\n\n .error-500-img {\n top: 15px; /* Adjust position */\n }\n}\n\n@media (max-width: 767.98px) {\n .error-500 .title {\n font-size: 68px; /* Reduce size for mobile */\n margin-top: 20px; /* Adjust margin */\n }\n\n .error-500-img {\n position: relative;\n top: 0; /* Adjust position */\n }\n\n .error-message h4 {\n font-size: 20px; /* Adjust heading size */\n }\n}\n",""]);const s=a},314:e=>{e.exports=function(e){var r=[];return r.toString=function(){return this.map((function(r){var n="",t=void 0!==r[5];return r[4]&&(n+="@supports (".concat(r[4],") {")),r[2]&&(n+="@media ".concat(r[2]," {")),t&&(n+="@layer".concat(r[5].length>0?" ".concat(r[5]):""," {")),n+=e(r),t&&(n+="}"),r[2]&&(n+="}"),r[4]&&(n+="}"),n})).join("")},r.i=function(e,n,t,o,i){"string"==typeof e&&(e=[[null,e,void 0]]);var a={};if(t)for(var s=0;s<this.length;s++){var c=this[s][0];null!=c&&(a[c]=!0)}for(var u=0;u<e.length;u++){var l=[].concat(e[u]);t&&a[l[0]]||(void 0!==i&&(void 0===l[5]||(l[1]="@layer".concat(l[5].length>0?" ".concat(l[5]):""," {").concat(l[1],"}")),l[5]=i),n&&(l[2]?(l[1]="@media ".concat(l[2]," {").concat(l[1],"}"),l[2]=n):l[2]=n),o&&(l[4]?(l[1]="@supports (".concat(l[4],") {").concat(l[1],"}"),l[4]=o):l[4]="".concat(o)),r.push(l))}},r}},601:e=>{e.exports=function(e){return e[1]}},20:(e,r,n)=>{var t=n(442),o=Symbol.for("react.element"),i=Symbol.for("react.fragment"),a=Object.prototype.hasOwnProperty,s=t.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.ReactCurrentOwner,c={key:!0,ref:!0,__self:!0,__source:!0};function u(e,r,n){var t,i={},u=null,l=null;for(t in void 0!==n&&(u=""+n),void 0!==r.key&&(u=""+r.key),void 0!==r.ref&&(l=r.ref),r)a.call(r,t)&&!c.hasOwnProperty(t)&&(i[t]=r[t]);if(e&&e.defaultProps)for(t in r=e.defaultProps)void 0===i[t]&&(i[t]=r[t]);return{$$typeof:o,type:e,key:u,ref:l,props:i,_owner:s.current}}r.Fragment=i,r.jsx=u,r.jsxs=u},848:(e,r,n)=>{e.exports=n(20)},72:e=>{var r=[];function n(e){for(var n=-1,t=0;t<r.length;t++)if(r[t].identifier===e){n=t;break}return n}function t(e,t){for(var i={},a=[],s=0;s<e.length;s++){var c=e[s],u=t.base?c[0]+t.base:c[0],l=i[u]||0,d="".concat(u," ").concat(l);i[u]=l+1;var f=n(d),p={css:c[1],media:c[2],sourceMap:c[3],supports:c[4],layer:c[5]};if(-1!==f)r[f].references++,r[f].updater(p);else{var m=o(p,t);t.byIndex=s,r.splice(s,0,{identifier:d,updater:m,references:1})}a.push(d)}return a}function o(e,r){var n=r.domAPI(r);return n.update(e),function(r){if(r){if(r.css===e.css&&r.media===e.media&&r.sourceMap===e.sourceMap&&r.supports===e.supports&&r.layer===e.layer)return;n.update(e=r)}else n.remove()}}e.exports=function(e,o){var i=t(e=e||[],o=o||{});return function(e){e=e||[];for(var a=0;a<i.length;a++){var s=n(i[a]);r[s].references--}for(var c=t(e,o),u=0;u<i.length;u++){var l=n(i[u]);0===r[l].references&&(r[l].updater(),r.splice(l,1))}i=c}}},659:e=>{var r={};e.exports=function(e,n){var t=function(e){if(void 0===r[e]){var n=document.querySelector(e);if(window.HTMLIFrameElement&&n instanceof window.HTMLIFrameElement)try{n=n.contentDocument.head}catch(e){n=null}r[e]=n}return r[e]}(e);if(!t)throw new Error("Couldn't find a style target. This probably means that the value for the 'insert' parameter is invalid.");t.appendChild(n)}},540:e=>{e.exports=function(e){var r=document.createElement("style");return e.setAttributes(r,e.attributes),e.insert(r,e.options),r}},56:(e,r,n)=>{e.exports=function(e){var r=n.nc;r&&e.setAttribute("nonce",r)}},825:e=>{e.exports=function(e){if("undefined"==typeof document)return{update:function(){},remove:function(){}};var r=e.insertStyleElement(e);return{update:function(n){!function(e,r,n){var t="";n.supports&&(t+="@supports (".concat(n.supports,") {")),n.media&&(t+="@media ".concat(n.media," {"));var o=void 0!==n.layer;o&&(t+="@layer".concat(n.layer.length>0?" ".concat(n.layer):""," {")),t+=n.css,o&&(t+="}"),n.media&&(t+="}"),n.supports&&(t+="}");var i=n.sourceMap;i&&"undefined"!=typeof btoa&&(t+="\n/*# sourceMappingURL=data:application/json;base64,".concat(btoa(unescape(encodeURIComponent(JSON.stringify(i))))," */")),r.styleTagTransform(t,e,r.options)}(r,e,n)},remove:function(){!function(e){if(null===e.parentNode)return!1;e.parentNode.removeChild(e)}(r)}}}},113:e=>{e.exports=function(e,r){if(r.styleSheet)r.styleSheet.cssText=e;else{for(;r.firstChild;)r.removeChild(r.firstChild);r.appendChild(document.createTextNode(e))}}},442:r=>{r.exports=e}},n={};function t(e){var o=n[e];if(void 0!==o)return o.exports;var i=n[e]={id:e,exports:{}};return r[e](i,i.exports,t),i.exports}t.n=e=>{var r=e&&e.__esModule?()=>e.default:()=>e;return t.d(r,{a:r}),r},t.d=(e,r)=>{for(var n in r)t.o(r,n)&&!t.o(e,n)&&Object.defineProperty(e,n,{enumerable:!0,get:r[n]})},t.g=function(){if("object"==typeof globalThis)return globalThis;try{return this||new Function("return this")()}catch(e){if("object"==typeof window)return window}}(),t.o=(e,r)=>Object.prototype.hasOwnProperty.call(e,r),t.r=e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},(()=>{var e;t.g.importScripts&&(e=t.g.location+"");var r=t.g.document;if(!e&&r&&(r.currentScript&&"SCRIPT"===r.currentScript.tagName.toUpperCase()&&(e=r.currentScript.src),!e)){var n=r.getElementsByTagName("script");if(n.length)for(var o=n.length-1;o>-1&&(!e||!/^http(s?):/.test(e));)e=n[o--].src}if(!e)throw new Error("Automatic publicPath is not supported in this browser");e=e.replace(/#.*$/,"").replace(/\?.*$/,"").replace(/\/[^\/]+$/,"/"),t.p=e})(),t.nc=void 0;var o={};function i(e,r){(null==r||r>e.length)&&(r=e.length);for(var n=0,t=Array(r);n<r;n++)t[n]=e[n];return t}t.r(o),t.d(o,{Error500:()=>S,ErrorBoundary:()=>A,default:()=>T});var a=t(442),s=t(72),c=t.n(s),u=t(825),l=t.n(u),d=t(659),f=t.n(d),p=t(56),m=t.n(p),h=t(540),v=t.n(h),y=t(113),g=t.n(y),x=t(233),b={};b.styleTagTransform=g(),b.setAttributes=m(),b.insert=f().bind(null,"head"),b.domAPI=l(),b.insertStyleElement=v(),c()(x.A,b),x.A&&x.A.locals&&x.A.locals;const w=t.p+"e1ea7728f2bdad3966f8.png";var j=t(848);const S=function(e){var r,n,t,o,i=e.onBackToHome;(0,a.useEffect)((function(){window.history.pushState(null,"",window.location.href),window.onpopstate=function(){window.history.go(1)}}),[]),sessionStorage.getItem("rinshadReactGuardAlert");var s={width:"undefined"!=typeof window&&(window.innerWidth||(null===(r=document.documentElement)||void 0===r?void 0:r.clientWidth)||(null===(n=document.body)||void 0===n?void 0:n.clientWidth))||"100%",height:"undefined"!=typeof window&&(window.innerHeight||(null===(t=document.documentElement)||void 0===t?void 0:t.clientHeight)||(null===(o=document.body)||void 0===o?void 0:o.clientHeight))||"100%"};return(0,j.jsx)("div",{className:"error-container",style:s,children:(0,j.jsxs)("div",{className:"error-content",children:[(0,j.jsxs)("div",{className:"error-500",children:[(0,j.jsx)("img",{src:w,alt:"Error 500",className:"error-500-img error-img"}),(0,j.jsx)("h1",{className:"title",children:"500"})]}),(0,j.jsxs)("div",{className:"error-message",children:[(0,j.jsx)("h4",{children:"Something went Wrong!"}),(0,j.jsx)("p",{children:"Please contact admin for help."}),(0,j.jsx)("a",{href:"/",className:"back-home",onClick:i,children:"Back to home"})]})]})})};var E=function(e){var r=e.onError,n=e.excludedKeywords;return(0,a.useEffect)((function(){var e=function(e){if(e.error&&e.error.message){var t=e.error.message.toLowerCase();n.some((function(e){return t.includes(e.toLowerCase())}))||r(e.error.message)}};return window.addEventListener("error",e),function(){return window.removeEventListener("error",e)}}),[r,n]),null};const A=function(e){var r,n,t=e.children,o=e.excludedKeywords,s=void 0===o?[]:o,c=(r=(0,a.useState)(!1),n=2,function(e){if(Array.isArray(e))return e}(r)||function(e,r){var n=null==e?null:"undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(null!=n){var t,o,i,a,s=[],c=!0,u=!1;try{if(i=(n=n.call(e)).next,0===r){if(Object(n)!==n)return;c=!1}else for(;!(c=(t=i.call(n)).done)&&(s.push(t.value),s.length!==r);c=!0);}catch(e){u=!0,o=e}finally{try{if(!c&&null!=n.return&&(a=n.return(),Object(a)!==a))return}finally{if(u)throw o}}return s}}(r,n)||function(e,r){if(e){if("string"==typeof e)return i(e,r);var n={}.toString.call(e).slice(8,-1);return"Object"===n&&e.constructor&&(n=e.constructor.name),"Map"===n||"Set"===n?Array.from(e):"Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)?i(e,r):void 0}}(r,n)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()),u=c[0],l=c[1],d="rinshadReactGuardAlert";return(0,j.jsxs)(j.Fragment,{children:[u?(0,j.jsx)(S,{onBackToHome:function(){sessionStorage.removeItem(d),l(!1)}}):t,(0,j.jsx)(E,{onError:function(e){console.error("Error caught by ErrorBoundary:",e),l(!0),sessionStorage.setItem(d,e)},excludedKeywords:s})]})},T=A;return o})()));