UNPKG

3.73 kBJavaScriptView Raw
1"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var _extends=require("@babel/runtime/helpers/extends"),_objectWithoutPropertiesLoose=require("@babel/runtime/helpers/objectWithoutPropertiesLoose"),_inheritsLoose=require("@babel/runtime/helpers/inheritsLoose"),svgInjector=require("@tanem/svg-injector"),React=require("react");function _interopDefaultLegacy(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}function _interopNamespace(e){if(e&&e.__esModule)return e;var t=Object.create(null);return e&&Object.keys(e).forEach((function(r){if("default"!==r){var n=Object.getOwnPropertyDescriptor(e,r);Object.defineProperty(t,r,n.get?n:{enumerable:!0,get:function(){return e[r]}})}})),t.default=e,Object.freeze(t)}var _extends__default=_interopDefaultLegacy(_extends),_objectWithoutPropertiesLoose__default=_interopDefaultLegacy(_objectWithoutPropertiesLoose),_inheritsLoose__default=_interopDefaultLegacy(_inheritsLoose),React__namespace=_interopNamespace(React),shallowDiffers=function(e,t){for(var r in e)if(!(r in t))return!0;for(var n in t)if(e[n]!==t[n])return!0;return!1},_excluded=["afterInjection","beforeInjection","evalScripts","fallback","loading","renumerateIRIElements","src","useRequestCache","wrapper"],svgNamespace="http://www.w3.org/2000/svg",xlinkNamespace="http://www.w3.org/1999/xlink",ReactSVG=function(e){function t(){for(var t,r=arguments.length,n=new Array(r),a=0;a<r;a++)n[a]=arguments[a];return(t=e.call.apply(e,[this].concat(n))||this).initialState={hasError:!1,isLoading:!0},t.state=t.initialState,t._isMounted=!1,t.reactWrapper=void 0,t.nonReactWrapper=void 0,t.refCallback=function(e){t.reactWrapper=e},t}_inheritsLoose__default.default(t,e);var r=t.prototype;return r.renderSVG=function(){var e=this;if(this.reactWrapper instanceof Node){var t,r,n=this.props,a=n.beforeInjection,o=n.evalScripts,i=n.renumerateIRIElements,s=n.src,c=n.useRequestCache,u=this.props.afterInjection,l=this.props.wrapper;"svg"===l?((t=document.createElementNS(svgNamespace,l)).setAttribute("xmlns",svgNamespace),t.setAttribute("xmlns:xlink",xlinkNamespace),r=document.createElementNS(svgNamespace,l)):(t=document.createElement(l),r=document.createElement(l)),t.appendChild(r),r.dataset.src=s,this.nonReactWrapper=this.reactWrapper.appendChild(t);svgInjector.SVGInjector(r,{afterEach:function(t,r){!t||(e.removeSVG(),e._isMounted)?e._isMounted&&e.setState((function(){return{hasError:!!t,isLoading:!1}}),(function(){u(t,r)})):u(t)},beforeEach:a,cacheRequests:c,evalScripts:o,renumerateIRIElements:i})}},r.removeSVG=function(){var e;null!=(e=this.nonReactWrapper)&&e.parentNode&&(this.nonReactWrapper.parentNode.removeChild(this.nonReactWrapper),this.nonReactWrapper=null)},r.componentDidMount=function(){this._isMounted=!0,this.renderSVG()},r.componentDidUpdate=function(e){var t=this;shallowDiffers(e,this.props)&&this.setState((function(){return t.initialState}),(function(){t.removeSVG(),t.renderSVG()}))},r.componentWillUnmount=function(){this._isMounted=!1,this.removeSVG()},r.render=function(){var e=this.props,t=e.fallback,r=e.loading,n=e.wrapper,a=_objectWithoutPropertiesLoose__default.default(e,_excluded);return React__namespace.createElement(n,_extends__default.default({},a,{ref:this.refCallback},"svg"===n?{xmlns:svgNamespace,xmlnsXlink:xlinkNamespace}:{}),this.state.isLoading&&r&&React__namespace.createElement(r,null),this.state.hasError&&t&&React__namespace.createElement(t,null))},t}(React__namespace.Component);ReactSVG.defaultProps={afterInjection:function(){},beforeInjection:function(){},evalScripts:"never",fallback:null,loading:null,renumerateIRIElements:!0,useRequestCache:!0,wrapper:"div"},exports.ReactSVG=ReactSVG;
2//# sourceMappingURL=react-svg.cjs.production.js.map