|
1 | | -import React from 'react'; |
2 | | -import {isSvg, isSvgUri} from '../../utils/imageUtils'; |
3 | | -// import {SvgPackage} from '../../optionalDependencies'; |
4 | | - |
5 | | -// const SvgXml = SvgPackage?.SvgXml; |
6 | | -// const SvgCssUri = SvgPackage?.SvgCssUri; |
7 | | -// const SvgProps = SvgPackage?.SvgProps; TODO: not sure how (or if) we can use their props |
| 1 | +import React, {useState} from 'react'; |
| 2 | +import {isSvg, isSvgUri, isBase64ImageContent} from '../../utils/imageUtils'; |
8 | 3 |
|
| 4 | +const EMPTY_STYLE = '{}'; |
9 | 5 | export interface SvgImageProps { |
10 | 6 | /** |
11 | 7 | * the asset tint |
12 | 8 | */ |
13 | 9 | tintColor?: string | null; |
14 | 10 | data: any; // TODO: I thought this should be string | React.ReactNode but it doesn't work properly |
| 11 | + style?: object[]; |
15 | 12 | } |
16 | 13 |
|
17 | 14 | function SvgImage(props: SvgImageProps) { |
18 | | - // tintColor crashes Android, so we're removing this until we properly support it. |
19 | | - // eslint-disable-next-line no-unused-vars, @typescript-eslint/no-unused-vars |
20 | | - // const {data, tintColor, ...others} = props; |
21 | | - const {data} = props; |
| 15 | + const { |
| 16 | + data, |
| 17 | + style, |
| 18 | + ...other |
| 19 | + } = props; |
| 20 | + |
| 21 | + const styleObj = Object.assign({}, ...(style || [])); |
22 | 22 |
|
23 | | - // if (!SvgXml) { |
24 | | - // // eslint-disable-next-line max-len |
25 | | - // console.error(`RNUILib Image "svg" prop requires installing "react-native-svg" and "react-native-svg-transformer" dependencies`); |
26 | | - // return null; |
27 | | - // } |
| 23 | + |
| 24 | + const [svgStyleCss, setSvgStyleCss] = useState<string>(EMPTY_STYLE); |
| 25 | + const [postCssStyleCalled, setPostCssStyleCalled] = useState(false); |
| 26 | + |
| 27 | + const createStyleSvgCss = async (PostCssPackage: {postcss: any, cssjs:any}) => { |
| 28 | + setPostCssStyleCalled(true); |
| 29 | + const {postcss, cssjs} = PostCssPackage; |
| 30 | + postcss().process(styleObj, {parser: cssjs}) |
| 31 | + .then((style: {css: any}) => setSvgStyleCss(`{${style.css}}`)); |
| 32 | + }; |
28 | 33 |
|
29 | 34 | if (isSvgUri(data)) { |
30 | | - return <img src={data.uri}/>; |
31 | | - // return <SvgCssUri {...others} uri={data.uri}/>; |
32 | | - // } |
33 | | - // else if (typeof data === 'string') { |
34 | | - // return <SvgXml xml={data} {...others}/>; |
| 35 | + return <img {...other} src={data.uri} style={styleObj}/>; |
| 36 | + } else if (isBase64ImageContent(data)) { |
| 37 | + return <img {...other} src={data} style={styleObj}/>; |
35 | 38 | } else if (data) { |
36 | | - return <img src={data}/>; |
| 39 | + const PostCssPackage = require('../../optionalDependencies').PostCssPackage; |
| 40 | + if (PostCssPackage) { |
| 41 | + if (!postCssStyleCalled) { |
| 42 | + createStyleSvgCss(PostCssPackage); |
| 43 | + return null; |
| 44 | + } |
| 45 | + const svgStyleTag = `<style> svg ${svgStyleCss} </style>`; |
| 46 | + |
| 47 | + return ( |
| 48 | + <div |
| 49 | + {...other} |
| 50 | + // eslint-disable-next-line react/no-danger |
| 51 | + dangerouslySetInnerHTML={{__html: svgStyleTag + data}} |
| 52 | + /> |
| 53 | + ); |
| 54 | + } |
| 55 | + |
37 | 56 | } |
38 | | - |
39 | 57 | return null; |
40 | 58 | } |
41 | 59 |
|
|
0 commit comments