1- import React , { useState } from 'react' ;
1+ import React , { useEffect , useState } from 'react' ;
22import { StyleSheet } from 'react-native' ;
33import Image from '../image' ;
44import { isSvg , isSvgUri , isBase64ImageContent } from '../../utils/imageUtils' ;
55
6+ const PostCssPackage = require ( '../../optionalDependencies' ) . PostCssPackage ;
7+
68const DEFAULT_SIZE = 16 ;
79export interface SvgImageProps {
810 /**
@@ -15,19 +17,21 @@ export interface SvgImageProps {
1517
1618function SvgImage ( props : SvgImageProps ) {
1719 const { data, style = [ ] , tintColor, ...others } = props ;
20+ const [ id ] = useState ( `svg-${ new Date ( ) . getTime ( ) . toString ( ) } ` ) ;
1821 const [ svgStyleCss , setSvgStyleCss ] = useState < string | undefined > ( undefined ) ;
19- const [ postCssStyleCalled , setPostCssStyleCalled ] = useState ( false ) ;
2022
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 ] ) ;
3135
3236 if ( isSvgUri ( data ) ) {
3337 return < img { ...others } src = { data . uri } style = { StyleSheet . flatten ( style ) } /> ;
@@ -44,26 +48,16 @@ function SvgImage(props: SvgImageProps) {
4448 ) ;
4549 }
4650 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+ ) ;
6761 }
6862 return null ;
6963}
0 commit comments