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' ;
5
5
6
+ const PostCssPackage = require ( '../../optionalDependencies' ) . PostCssPackage ;
7
+
6
8
const DEFAULT_SIZE = 16 ;
7
9
export interface SvgImageProps {
8
10
/**
@@ -15,19 +17,21 @@ export interface SvgImageProps {
15
17
16
18
function SvgImage ( props : SvgImageProps ) {
17
19
const { data, style = [ ] , tintColor, ...others } = props ;
20
+ const [ id ] = useState ( `svg-${ new Date ( ) . getTime ( ) . toString ( ) } ` ) ;
18
21
const [ svgStyleCss , setSvgStyleCss ] = useState < string | undefined > ( undefined ) ;
19
- const [ postCssStyleCalled , setPostCssStyleCalled ] = useState ( false ) ;
20
22
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
- } ;
23
+ useEffect ( ( ) => {
24
+ if ( PostCssPackage ) {
25
+ const { postcss, cssjs} = PostCssPackage ;
26
+ const styleObj : Record < string , any > = StyleSheet . flatten ( style ) ;
27
+ postcss ( )
28
+ . process ( styleObj , { parser : cssjs } )
29
+ . then ( ( style : { css : any } ) => {
30
+ const svgPathCss = ( styleObj ?. tintColor ) ? `#${ id } svg path {fill: ${ styleObj ?. tintColor } }` : '' ;
31
+ setSvgStyleCss ( `#${ id } svg {${ style . css } } #${ id } {${ style . css } } ${ svgPathCss } }` ) ;
32
+ } ) ;
33
+ }
34
+ } , [ style , id ] ) ;
31
35
32
36
if ( isSvgUri ( data ) ) {
33
37
return < img { ...others } src = { data . uri } style = { StyleSheet . flatten ( style ) } /> ;
@@ -44,26 +48,16 @@ function SvgImage(props: SvgImageProps) {
44
48
) ;
45
49
}
46
50
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
- }
51
+ } else if ( data && svgStyleCss ) {
52
+ const svgStyleTag = `<style> ${ svgStyleCss } </style>` ;
53
+ return (
54
+ < div
55
+ id = { id }
56
+ { ...others }
57
+ // eslint-disable-next-line react/no-danger
58
+ dangerouslySetInnerHTML = { { __html : svgStyleTag + data } }
59
+ />
60
+ ) ;
67
61
}
68
62
return null ;
69
63
}
0 commit comments