Skip to content

Commit 32ad53c

Browse files
[WEB] Allow pass id to SvgImage (#2740)
* Allow changing SVG size in style * depend on className instead of id --------- Co-authored-by: Amit Shwarts <[email protected]>
1 parent e31c8ad commit 32ad53c

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

src/components/svgImage/index.web.tsx

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,12 @@ export interface SvgImageProps {
1515
style?: object[];
1616
height?: number;
1717
width?: number;
18+
id?: string;
1819
}
1920

2021
function SvgImage(props: SvgImageProps) {
2122
const {data, style = [], tintColor, width, height, ...others} = props;
22-
const [id] = useState(`svg-${new Date().getTime().toString()}`);
23+
const [className] = useState(`svg-${new Date().getTime().toString()}`);
2324
const [svgStyleCss, setSvgStyleCss] = useState<string | undefined>(undefined);
2425

2526
useEffect(() => {
@@ -29,11 +30,11 @@ function SvgImage(props: SvgImageProps) {
2930
postcss()
3031
.process({width, height, ...styleObj}, {parser: cssjs})
3132
.then((style: {css: any}) => {
32-
const svgPathCss = (styleObj?.tintColor) ? `#${id} svg path {fill: ${styleObj?.tintColor}}` : '';
33-
setSvgStyleCss(`#${id} svg {${style.css}} #${id} {${style.css}} ${svgPathCss}}`);
33+
const svgPathCss = styleObj?.tintColor ? `.${className} > svg path {fill: ${styleObj?.tintColor}}` : '';
34+
setSvgStyleCss(`.${className} > svg {${style.css}} ${svgPathCss}}`);
3435
});
3536
}
36-
}, [style, id, width, height]);
37+
}, [style, className, width, height]);
3738

3839
if (isSvgUri(data)) {
3940
return <img {...others} src={data.uri} style={StyleSheet.flatten(style)}/>;
@@ -53,9 +54,9 @@ function SvgImage(props: SvgImageProps) {
5354
} else if (data && svgStyleCss) {
5455
const svgStyleTag = `<style> ${svgStyleCss} </style>`;
5556
return (
56-
<div
57-
id={id}
57+
<div
5858
{...others}
59+
className={className}
5960
// eslint-disable-next-line react/no-danger
6061
dangerouslySetInnerHTML={{__html: svgStyleTag + data}}
6162
/>

0 commit comments

Comments
 (0)