Skip to content

Commit 9d47a06

Browse files
committed
fix: [WEB] SvgImage update styles
1 parent 7c5e94c commit 9d47a06

File tree

2 files changed

+68
-32
lines changed

2 files changed

+68
-32
lines changed

src/components/svgImage/index.web.tsx

Lines changed: 41 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, {useState} from 'react';
1+
import React, {useEffect, useState} from 'react';
22
import {StyleSheet} from 'react-native';
33
import Image from '../image';
44
import {isSvg, isSvgUri, isBase64ImageContent} from '../../utils/imageUtils';
@@ -13,21 +13,41 @@ export interface SvgImageProps {
1313
style?: object[];
1414
}
1515

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+
1628
function SvgImage(props: SvgImageProps) {
1729
const {data, style = [], tintColor, ...others} = props;
30+
const [id] = useState(`svg-${new Date().getTime().toString()}`);
31+
const [_data, setData] = useState((injectID(data, id)));
1832
const [svgStyleCss, setSvgStyleCss] = useState<string | undefined>(undefined);
19-
const [postCssStyleCalled, setPostCssStyleCalled] = useState(false);
2033

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]);
3151

3252
if (isSvgUri(data)) {
3353
return <img {...others} src={data.uri} style={StyleSheet.flatten(style)}/>;
@@ -44,26 +64,15 @@ function SvgImage(props: SvgImageProps) {
4464
);
4565
}
4666
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+
);
6776
}
6877
return null;
6978
}

webDemo/src/App.tsx

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,32 @@ interface ItemToRender {
4141
const svgData = '<svg data-bbox="18.5 31.5 163.1 137.2" viewBox="0 0 200 200" height="200" width="200" xmlns="http://www.w3.org/2000/svg" data-type="color">\n <g>\n <path d="M18.5 99.5c0-5.7 2.3-10.8 6-14.5L72 37.5c3.7-3.7 8.8-6 14.5-6 11.4 0 20.5 9.2 20.5 20.5 0 5.7-2.3 10.8-6 14.5L88.1 79.4h72.3c11.7 0 21.2 9.5 21.2 21.2s-9.5 21.2-21.2 21.2H89.1l11.9 11.9c3.7 3.7 6 8.8 6 14.5 0 11.3-9.2 20.5-20.5 20.5-5.7 0-10.8-2.3-14.5-6L24.5 115c-3.7-3.7-6-8.8-6-14.5 0-.2 0-.4.1-.6 0 0-.1-.3-.1-.4z" fill="#000010" data-color="1"/>\n </g>\n</svg>\n';
4242

4343
const itemsToRender: ItemToRender[] = [
44+
{
45+
title: 'IconButton SVG Resize',
46+
FC: () => {
47+
const [size, setSize] = useState(Spacings.s4);
48+
49+
console.log('$$ IconButton SVG Resize', {size});
50+
51+
return (
52+
<Button
53+
round
54+
id={'iconButton_resize_svg'}
55+
size={Button.sizes.large}
56+
iconSource={svgData}
57+
iconStyle={{
58+
width: size,
59+
height: size,
60+
tintColor: '#ffffff'
61+
}}
62+
onPress={() => {
63+
const newSize = size === Spacings.s4 ? Spacings.s6 : Spacings.s4;
64+
setSize(newSize);
65+
}}
66+
/>
67+
);
68+
}
69+
},
4470
{
4571
title: 'Carousel',
4672
FC: CarouselWrapper
@@ -97,6 +123,7 @@ const itemsToRender: ItemToRender[] = [
97123
FC: () => (
98124
<Button
99125
label={'Svg tag'}
126+
id={'iconButton'}
100127
size={Button.sizes.large}
101128
iconSource={svgData}
102129
iconStyle={{

0 commit comments

Comments
 (0)