UNPKG

8.62 kBSource Map (JSON)View Raw
1{"version":3,"file":"react-svg.cjs.production.js","sources":["../compiled/shallow-differs.js","../compiled/ReactSVG.js"],"sourcesContent":["// Hat-tip: https://github.com/developit/preact-compat/blob/master/src/index.js#L402.\nconst shallowDiffers = (a, b) => {\n for (const i in a) {\n if (!(i in b)) {\n return true;\n }\n }\n for (const i in b) {\n if (a[i] !== b[i]) {\n return true;\n }\n }\n return false;\n};\nexport default shallowDiffers;\n","import { SVGInjector } from '@tanem/svg-injector';\nimport * as PropTypes from 'prop-types';\nimport * as React from 'react';\nimport shallowDiffers from './shallow-differs';\nconst svgNamespace = 'http://www.w3.org/2000/svg';\nconst xlinkNamespace = 'http://www.w3.org/1999/xlink';\nexport class ReactSVG extends React.Component {\n static defaultProps = {\n afterInjection: () => undefined,\n beforeInjection: () => undefined,\n evalScripts: 'never',\n fallback: null,\n httpRequestWithCredentials: false,\n loading: null,\n renumerateIRIElements: true,\n useRequestCache: true,\n wrapper: 'div',\n };\n static propTypes = {\n afterInjection: PropTypes.func,\n beforeInjection: PropTypes.func,\n evalScripts: PropTypes.oneOf(['always', 'once', 'never']),\n fallback: PropTypes.oneOfType([\n PropTypes.func,\n PropTypes.object,\n PropTypes.string,\n ]),\n httpRequestWithCredentials: PropTypes.bool,\n loading: PropTypes.oneOfType([\n PropTypes.func,\n PropTypes.object,\n PropTypes.string,\n ]),\n renumerateIRIElements: PropTypes.bool,\n src: PropTypes.string.isRequired,\n useRequestCache: PropTypes.bool,\n wrapper: PropTypes.oneOf(['div', 'span', 'svg']),\n };\n initialState = {\n hasError: false,\n isLoading: true,\n };\n state = this.initialState;\n _isMounted = false;\n reactWrapper;\n nonReactWrapper;\n refCallback = (reactWrapper) => {\n this.reactWrapper = reactWrapper;\n };\n renderSVG() {\n /* istanbul ignore else */\n if (this.reactWrapper instanceof Node) {\n const { beforeInjection, evalScripts, httpRequestWithCredentials, renumerateIRIElements, src, useRequestCache, } = this.props;\n /* eslint-disable @typescript-eslint/no-non-null-assertion */\n const afterInjection = this.props.afterInjection;\n const wrapper = this.props.wrapper;\n /* eslint-enable @typescript-eslint/no-non-null-assertion */\n let nonReactWrapper;\n let nonReactTarget;\n if (wrapper === 'svg') {\n nonReactWrapper = document.createElementNS(svgNamespace, wrapper);\n nonReactWrapper.setAttribute('xmlns', svgNamespace);\n nonReactWrapper.setAttribute('xmlns:xlink', xlinkNamespace);\n nonReactTarget = document.createElementNS(svgNamespace, wrapper);\n }\n else {\n nonReactWrapper = document.createElement(wrapper);\n nonReactTarget = document.createElement(wrapper);\n }\n nonReactWrapper.appendChild(nonReactTarget);\n nonReactTarget.dataset.src = src;\n this.nonReactWrapper = this.reactWrapper.appendChild(nonReactWrapper);\n const afterEach = (error, svg) => {\n if (error) {\n this.removeSVG();\n if (!this._isMounted) {\n afterInjection(error);\n return;\n }\n }\n // TODO (Tane): It'd be better to cleanly unsubscribe from SVGInjector\n // callbacks instead of tracking a property like this.\n if (this._isMounted) {\n this.setState(() => ({\n hasError: !!error,\n isLoading: false,\n }), () => {\n afterInjection(error, svg);\n });\n }\n };\n SVGInjector(nonReactTarget, {\n afterEach,\n beforeEach: beforeInjection,\n cacheRequests: useRequestCache,\n evalScripts,\n httpRequestWithCredentials,\n renumerateIRIElements,\n });\n }\n }\n removeSVG() {\n if (this.nonReactWrapper?.parentNode) {\n this.nonReactWrapper.parentNode.removeChild(this.nonReactWrapper);\n this.nonReactWrapper = null;\n }\n }\n componentDidMount() {\n this._isMounted = true;\n this.renderSVG();\n }\n componentDidUpdate(prevProps) {\n if (shallowDiffers(prevProps, this.props)) {\n this.setState(() => this.initialState, () => {\n this.removeSVG();\n this.renderSVG();\n });\n }\n }\n componentWillUnmount() {\n this._isMounted = false;\n this.removeSVG();\n }\n render() {\n /* eslint-disable @typescript-eslint/no-unused-vars */\n const { afterInjection, beforeInjection, evalScripts, fallback: Fallback, httpRequestWithCredentials, loading: Loading, renumerateIRIElements, src, useRequestCache, wrapper, ...rest } = this.props;\n /* eslint-enable @typescript-eslint/no-unused-vars */\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n const Wrapper = wrapper;\n return (React.createElement(Wrapper, { ...rest, ref: this.refCallback, ...(wrapper === 'svg'\n ? {\n xmlns: svgNamespace,\n xmlnsXlink: xlinkNamespace,\n }\n : {}) },\n this.state.isLoading && Loading && React.createElement(Loading, null),\n this.state.hasError && Fallback && React.createElement(Fallback, null)));\n }\n}\n"],"names":["shallowDiffers","a","b","i","svgNamespace","xlinkNamespace","ReactSVG","initialState","hasError","isLoading","state","_this","_isMounted","reactWrapper","nonReactWrapper","refCallback","renderSVG","this","Node","nonReactTarget","props","beforeInjection","evalScripts","httpRequestWithCredentials","renumerateIRIElements","src","useRequestCache","afterInjection","wrapper","document","createElementNS","setAttribute","createElement","appendChild","dataset","SVGInjector","afterEach","error","svg","_this2","removeSVG","setState","beforeEach","cacheRequests","_this$nonReactWrapper","parentNode","removeChild","componentDidMount","componentDidUpdate","prevProps","_this3","componentWillUnmount","render","Fallback","fallback","Loading","loading","rest","React","ref","xmlns","xmlnsXlink","Component","defaultProps"],"mappings":"89BACMA,eAAiB,SAACC,EAAGC,OAClB,IAAMC,KAAKF,OACNE,KAAKD,UACA,MAGV,IAAMC,KAAKD,KACRD,EAAEE,KAAOD,EAAEC,UACJ,SAGR,4KCRLC,aAAe,6BACfC,eAAiB,+BACVC,6JAgCTC,aAAe,CACXC,UAAU,EACVC,WAAW,KAEfC,MAAQC,EAAKJ,eACbK,YAAa,IACbC,sBACAC,yBACAC,YAAc,SAACF,KACNA,aAAeA,qEAExBG,UAAA,yBAEQC,KAAKJ,wBAAwBK,KAAM,KAM/BJ,EACAK,IAN+GF,KAAKG,MAAhHC,IAAAA,gBAAiBC,IAAAA,YAAaC,IAAAA,2BAA4BC,IAAAA,sBAAuBC,IAAAA,IAAKC,IAAAA,gBAExFC,EAAiBV,KAAKG,MAAMO,eAC5BC,EAAUX,KAAKG,MAAMQ,QAIX,QAAZA,IACAd,EAAkBe,S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