Skip to content

fix/svg image respect width height props on web #2748

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/components/icon/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ const Icon = forwardRef((props: Props, ref: any) => {
/>
);

const renderSvg = () => <SvgImage fsTagName={recorderTag} data={source} {...props} {...iconSize}/>;
const renderSvg = () => <SvgImage fsTagName={recorderTag} data={source} {...iconSize} {...props}/>;

if (typeof source === 'string' && isBase64ImageContent(source) && Constants.isWeb) {
return renderImage();
Expand Down
8 changes: 5 additions & 3 deletions src/components/svgImage/index.web.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ export interface SvgImageProps {
tintColor?: string | null;
data: any; // TODO: I thought this should be string | React.ReactNode but it doesn't work properly
style?: object[];
width?: number | string;
height?: number | string;
}

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

Expand All @@ -25,13 +27,13 @@ function SvgImage(props: SvgImageProps) {
const {postcss, cssjs} = PostCssPackage;
const styleObj: Record<string, any> = StyleSheet.flatten(style);
postcss()
.process(styleObj, {parser: cssjs})
.process({width, height, ...styleObj}, {parser: cssjs})
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add someone that looked at web recently for this part

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.then((style: {css: any}) => {
const svgPathCss = (styleObj?.tintColor) ? `#${id} svg path {fill: ${styleObj?.tintColor}}` : '';
setSvgStyleCss(`#${id} svg {${style.css}} #${id} {${style.css}} ${svgPathCss}}`);
});
}
}, [style, id]);
}, [style, id, width, height]);

if (isSvgUri(data)) {
return <img {...others} src={data.uri} style={StyleSheet.flatten(style)}/>;
Expand Down
6 changes: 4 additions & 2 deletions webDemo/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,12 @@ const itemsToRender: ItemToRender[] = [
size={Button.sizes.large}
iconSource={svgData}
iconStyle={{
width: size,
height: size,
tintColor: '#ffffff'
}}
iconProps={{
width: size,
height: size
}}
onPress={() => {
const newSize = size === Spacings.s4 ? Spacings.s6 : Spacings.s4;
setSize(newSize);
Expand Down