Skip to content

Commit f1e5557

Browse files
authored
fix: [WEB] SvgImage update styles (#2728)
* fix: [WEB] SvgImage update styles * fix sizing of svg wrapper * code review
1 parent f93e222 commit f1e5557

File tree

2 files changed

+53
-32
lines changed

2 files changed

+53
-32
lines changed

src/components/svgImage/index.web.tsx

Lines changed: 26 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
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';
55

6+
const PostCssPackage = require('../../optionalDependencies').PostCssPackage;
7+
68
const DEFAULT_SIZE = 16;
79
export interface SvgImageProps {
810
/**
@@ -15,19 +17,21 @@ export interface SvgImageProps {
1517

1618
function 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
}

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)