Skip to content

chore: infer SVG size from style #2742

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

Merged
merged 2 commits into from
Sep 18, 2023
Merged
Changes from 1 commit
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
9 changes: 8 additions & 1 deletion src/components/svgImage/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React from 'react';
import {ImageStyle, StyleProp, StyleSheet} from 'react-native';
import {isSvg, isSvgUri} from '../../utils/imageUtils';
import {SvgPackage} from '../../optionalDependencies';

Expand All @@ -12,6 +13,7 @@ export interface SvgImageProps {
*/
tintColor?: string | null;
data: any; // TODO: I thought this should be string | React.ReactNode but it doesn't work properly
style?: StyleProp<ImageStyle>;
}

function SvgImage(props: SvgImageProps) {
Expand All @@ -28,7 +30,12 @@ function SvgImage(props: SvgImageProps) {
if (isSvgUri(data)) {
return <SvgCssUri {...others} uri={data.uri}/>;
} else if (typeof data === 'string') {
return <SvgXml xml={data} {...others}/>;
const flattenStyle = StyleSheet.flatten(props.style) as Record<string, any>;
Copy link
Collaborator

Choose a reason for hiding this comment

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

We use flatten very rarely, why is this required here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

to extract the width and height from the provided styles.
do you have suggestion how to do it differently?

Copy link
Contributor

Choose a reason for hiding this comment

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

are we sure its coming with more than one style object ?
if not, and we always have just single style object (and not as array).?
we can remove that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes i'm sure. we can't remove it

Copy link
Collaborator

Choose a reason for hiding this comment

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

Can you please provide the scenario this is used in?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, for example when using Button with SVG Icon, we're passing iconStyle with width & height.
in this case it's been passed to the SVG component, and without this PR the height and width is not respected.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

    <Button
      id={id}
      testID={TestIds.buttonElement}
      style={style}
      color={color}
      iconSource={iconSource}
      iconStyle={{width: Spacings.s6, height: Spacings.s6}}
      round
      size={Button.sizes.large}
    />

Copy link
Collaborator

Choose a reason for hiding this comment

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

As discussed, lets allow passing iconProps to the Button instead, which will allow sending width and height

const dimensions = {
...(flattenStyle?.width ? {width: flattenStyle.width} : {}),
...(flattenStyle?.height ? {height: flattenStyle.height} : {})
};
return <SvgXml xml={data} {...dimensions} {...others}/>;
} else if (data) {
const File = data; // Must be with capital letter
return <File {...others}/>;
Expand Down