1
1
import React , { useState } from 'react' ;
2
+ import { StyleSheet } from 'react-native' ;
2
3
import Image from '../image' ;
3
4
import { isSvg , isSvgUri , isBase64ImageContent } from '../../utils/imageUtils' ;
4
5
5
- const EMPTY_STYLE = '{}' ;
6
6
const DEFAULT_SIZE = 16 ;
7
7
export interface SvgImageProps {
8
8
/**
@@ -14,22 +14,23 @@ export interface SvgImageProps {
14
14
}
15
15
16
16
function SvgImage ( props : SvgImageProps ) {
17
- const { data, style = { } , tintColor, ...others } = props ;
18
- const [ svgStyleCss , setSvgStyleCss ] = useState < string > ( EMPTY_STYLE ) ;
17
+ const { data, style = [ ] , tintColor, ...others } = props ;
18
+ const [ svgStyleCss , setSvgStyleCss ] = useState < string | undefined > ( undefined ) ;
19
19
const [ postCssStyleCalled , setPostCssStyleCalled ] = useState ( false ) ;
20
20
21
- const styleObj = JSON . parse ( JSON . stringify ( style ) ) ;
22
-
23
- const createStyleSvgCss = async ( PostCssPackage : { postcss : any ; cssjs : any } ) => {
21
+ const createStyleSvgCss = async ( PostCssPackage : { postcss : any ; cssjs : any } , styleObj ?: Record < string , any > ) => {
24
22
setPostCssStyleCalled ( true ) ;
25
23
const { postcss, cssjs} = PostCssPackage ;
26
24
postcss ( )
27
25
. process ( styleObj , { parser : cssjs } )
28
- . then ( ( style : { css : any } ) => setSvgStyleCss ( `{${ style . css } }` ) ) ;
26
+ . then ( ( style : { css : any } ) => {
27
+ const svgPathCss = ( styleObj ?. tintColor ) ? `svg path {fill: ${ styleObj ?. tintColor } }` : '' ;
28
+ setSvgStyleCss ( `svg {${ style . css } } ${ svgPathCss } }` ) ;
29
+ } ) ;
29
30
} ;
30
31
31
32
if ( isSvgUri ( data ) ) {
32
- return < img { ...others } src = { data . uri } style = { styleObj } /> ;
33
+ return < img { ...others } src = { data . uri } style = { StyleSheet . flatten ( style ) } /> ;
33
34
} else if ( isBase64ImageContent ( data ) ) {
34
35
if ( tintColor ) {
35
36
return (
@@ -42,23 +43,26 @@ function SvgImage(props: SvgImageProps) {
42
43
/>
43
44
) ;
44
45
}
45
- return < img { ...others } src = { data } style = { styleObj } /> ;
46
+ return < img { ...others } src = { data } style = { StyleSheet . flatten ( style ) } /> ;
46
47
} else if ( data ) {
48
+
47
49
const PostCssPackage = require ( '../../optionalDependencies' ) . PostCssPackage ;
48
50
if ( PostCssPackage ) {
49
51
if ( ! postCssStyleCalled ) {
50
- createStyleSvgCss ( PostCssPackage ) ;
52
+ createStyleSvgCss ( PostCssPackage , StyleSheet . flatten ( style ) ) ;
51
53
return null ;
52
54
}
53
- const svgStyleTag = `<style> svg ${ svgStyleCss } </style>` ;
54
-
55
- return (
56
- < div
57
- { ...others }
58
- // eslint-disable-next-line react/no-danger
59
- dangerouslySetInnerHTML = { { __html : svgStyleTag + data } }
60
- />
61
- ) ;
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
+ }
62
66
}
63
67
}
64
68
return null ;
0 commit comments