1
- import React , { useState } from 'react' ;
1
+ import React , { useEffect , useState } from 'react' ;
2
2
import { StyleSheet } from 'react-native' ;
3
3
import Image from '../image' ;
4
4
import { isSvg , isSvgUri , isBase64ImageContent } from '../../utils/imageUtils' ;
@@ -13,21 +13,41 @@ export interface SvgImageProps {
13
13
style ?: object [ ] ;
14
14
}
15
15
16
+ function injectID ( data : SvgImageProps [ 'data' ] , id : string ) {
17
+ const tempDiv = document . createElement ( 'div' ) ;
18
+ tempDiv . innerHTML = data ;
19
+
20
+ const svgElement = tempDiv . querySelector ( 'svg' ) ;
21
+ svgElement ?. setAttribute ( 'id' , id ) ;
22
+
23
+ const modifiedData = tempDiv . innerHTML ;
24
+
25
+ return modifiedData ;
26
+ }
27
+
16
28
function SvgImage ( props : SvgImageProps ) {
17
29
const { data, style = [ ] , tintColor, ...others } = props ;
30
+ const [ id ] = useState ( `svg-${ new Date ( ) . getTime ( ) . toString ( ) } ` ) ;
31
+ const [ _data , setData ] = useState ( ( injectID ( data , id ) ) ) ;
18
32
const [ svgStyleCss , setSvgStyleCss ] = useState < string | undefined > ( undefined ) ;
19
- const [ postCssStyleCalled , setPostCssStyleCalled ] = useState ( false ) ;
20
33
21
- const createStyleSvgCss = async ( PostCssPackage : { postcss : any ; cssjs : any } , styleObj ?: Record < string , any > ) => {
22
- setPostCssStyleCalled ( true ) ;
23
- const { postcss, cssjs} = PostCssPackage ;
24
- postcss ( )
25
- . process ( styleObj , { parser : cssjs } )
26
- . then ( ( style : { css : any } ) => {
27
- const svgPathCss = ( styleObj ?. tintColor ) ? `svg path {fill: ${ styleObj ?. tintColor } }` : '' ;
28
- setSvgStyleCss ( `svg {${ style . css } } ${ svgPathCss } }` ) ;
29
- } ) ;
30
- } ;
34
+ useEffect ( ( ) => {
35
+ const PostCssPackage = require ( '../../optionalDependencies' ) . PostCssPackage ;
36
+ if ( PostCssPackage ) {
37
+ const { postcss, cssjs} = PostCssPackage ;
38
+ const styleObj : Record < string , any > = StyleSheet . flatten ( style ) ;
39
+ postcss ( )
40
+ . process ( styleObj , { parser : cssjs } )
41
+ . then ( ( style : { css : any } ) => {
42
+ const svgPathCss = ( styleObj ?. tintColor ) ? `svg path {fill: ${ styleObj ?. tintColor } }` : '' ;
43
+ setSvgStyleCss ( `svg {${ style . css } } ${ svgPathCss } }` ) ;
44
+ } ) ;
45
+ }
46
+ } , [ style ] ) ;
47
+
48
+ useEffect ( ( ) => {
49
+ setData ( injectID ( data , id ) ) ;
50
+ } , [ data , id ] ) ;
31
51
32
52
if ( isSvgUri ( data ) ) {
33
53
return < img { ...others } src = { data . uri } style = { StyleSheet . flatten ( style ) } /> ;
@@ -44,26 +64,15 @@ function SvgImage(props: SvgImageProps) {
44
64
) ;
45
65
}
46
66
return < img { ...others } src = { data } style = { StyleSheet . flatten ( style ) } /> ;
47
- } else if ( data ) {
48
-
49
- const PostCssPackage = require ( '../../optionalDependencies' ) . PostCssPackage ;
50
- if ( PostCssPackage ) {
51
- if ( ! postCssStyleCalled ) {
52
- createStyleSvgCss ( PostCssPackage , StyleSheet . flatten ( style ) ) ;
53
- return null ;
54
- }
55
-
56
- if ( svgStyleCss ) {
57
- const svgStyleTag = `<style> ${ svgStyleCss } </style>` ;
58
- return (
59
- < div
60
- { ...others }
61
- // eslint-disable-next-line react/no-danger
62
- dangerouslySetInnerHTML = { { __html : svgStyleTag + data } }
63
- />
64
- ) ;
65
- }
66
- }
67
+ } else if ( data && svgStyleCss ) {
68
+ const svgStyleTag = `<style> ${ svgStyleCss } </style>` ;
69
+ return (
70
+ < div
71
+ { ...others }
72
+ // eslint-disable-next-line react/no-danger
73
+ dangerouslySetInnerHTML = { { __html : svgStyleTag + _data } }
74
+ />
75
+ ) ;
67
76
}
68
77
return null ;
69
78
}
0 commit comments