react-image
Version:
React Image is an <img> tag replacement for react, featuring preloader and multiple image fallback support
3 lines (2 loc) • 3.75 kB
JavaScript
(function(a,b){"object"==typeof exports&&"undefined"!=typeof module?b(exports,require("@babel/runtime/helpers/defineProperty"),require("@babel/runtime/helpers/objectWithoutPropertiesLoose"),require("react")):"function"==typeof define&&define.amd?define(["exports","@babel/runtime/helpers/defineProperty","@babel/runtime/helpers/objectWithoutPropertiesLoose","react"],b):(a="undefined"==typeof globalThis?a||self:globalThis,b(a.Img={},a._defineProperty,a._objectWithoutPropertiesLoose,a.React))})(this,function(a,b,c,d){"use strict";function e(a){return a&&"object"==typeof a&&"default"in a?a:{default:a}}function f(a,b){var c=Object.keys(a);if(Object.getOwnPropertySymbols){var d=Object.getOwnPropertySymbols(a);b&&(d=d.filter(function(b){return Object.getOwnPropertyDescriptor(a,b).enumerable})),c.push.apply(c,d)}return c}function g(a){for(var b,c=1;c<arguments.length;c++)b=null==arguments[c]?{}:arguments[c],c%2?f(Object(b),!0).forEach(function(c){l["default"](a,c,b[c])}):Object.getOwnPropertyDescriptors?Object.defineProperties(a,Object.getOwnPropertyDescriptors(b)):f(Object(b)).forEach(function(c){Object.defineProperty(a,c,Object.getOwnPropertyDescriptor(b,c))});return a}function h(a){var b=a.srcList,c=a.imgPromise,e=void 0===c?o({decode:!0}):c,f=a.useSuspense,h=void 0===f||f,i=d.useState(!1),j=i[1],k=p(q(b)),l=k.join("");if(r[l]||(r[l]={promise:s(k,e),cache:"pending",error:null}),"resolved"===r[l].cache)return{src:r[l].src,isLoading:!1,error:null};if("rejected"===r[l].cache){if(h)throw r[l].error;return{isLoading:!1,error:r[l].error,src:void 0}}if(r[l].promise.then(function(a){r[l]=g(g({},r[l]),{},{cache:"resolved",src:a}),h||j(l)})["catch"](function(a){r[l]=g(g({},r[l]),{},{cache:"rejected",error:a}),h||j(l)}),h)throw r[l].promise;return{isLoading:!0,src:void 0,error:null}}function j(a,b){var c=Object.keys(a);if(Object.getOwnPropertySymbols){var d=Object.getOwnPropertySymbols(a);b&&(d=d.filter(function(b){return Object.getOwnPropertyDescriptor(a,b).enumerable})),c.push.apply(c,d)}return c}function k(a){for(var b,c=1;c<arguments.length;c++)b=null==arguments[c]?{}:arguments[c],c%2?j(Object(b),!0).forEach(function(c){l["default"](a,c,b[c])}):Object.getOwnPropertyDescriptors?Object.defineProperties(a,Object.getOwnPropertyDescriptors(b)):j(Object(b)).forEach(function(c){Object.defineProperty(a,c,Object.getOwnPropertyDescriptor(b,c))});return a}var l=e(b),m=e(c),n=e(d),o=function(a){var b=a.decode,c=a.crossOrigin,d=void 0===c?"":c;return function(a){return new Promise(function(c,e){var f=new Image;d&&(f.crossOrigin=d),f.onload=function(){(!(void 0!==b)||b)&&f.decode?f.decode().then(c)["catch"](e):c()},f.onerror=e,f.src=a})}},p=function(b){return b.filter(function(a){return a})},q=function(a){return Array.isArray(a)?a:[a]},r={},s=function(a,b){var c=!1;return new Promise(function(d,e){var f=function(a){return b(a).then(function(){c=!0,d(a)})};a.reduce(function(a,b){return a["catch"](function(){if(!c)return f(b)})},f(a.shift()))["catch"](e)})},t=["decode","src","loader","unloader","container","loaderContainer","unloaderContainer","imgPromise","crossorigin","useSuspense"],u=function(a){return a};a.Img=function(a){var b=a.decode,c=a.src,d=void 0===c?[]:c,e=a.loader,f=void 0===e?null:e,g=a.unloader,i=void 0===g?null:g,j=a.container,l=void 0===j?u:j,p=a.loaderContainer,q=void 0===p?u:p,r=a.unloaderContainer,s=void 0===r?u:r,v=a.imgPromise,w=a.crossorigin,x=a.useSuspense,y=void 0!==x&&x,z=m["default"](a,t);v=v||o({decode:!(void 0!==b)||b,crossOrigin:w});var A=h({srcList:d,imgPromise:v,useSuspense:y}),B=A.src,C=A.isLoading;return B?l(n["default"].createElement("img",k({src:B},z))):!y&&C?q(f):!y&&i?s(i):null},a.useImage=h,Object.defineProperty(a,"__esModule",{value:!0})});
//# sourceMappingURL=index.js.map