Skip to content

feat: calculate width from height #2399

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
wants to merge 4 commits into from
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
3 changes: 2 additions & 1 deletion apps/test-examples/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import Test2071 from './src/Test2071';
import Test2080 from './src/Test2080';
import Test2086 from './src/Test2086';
import Test2089 from './src/Test2089';
import Test2138 from './src/Test2138';
import Test2148 from './src/Test2148';
import Test2196 from './src/Test2196';
import Test2248 from './src/Test2248';
Expand All @@ -31,5 +32,5 @@ import Test2403 from './src/Test2403';
import Test2407 from './src/Test2407';

export default function App() {
return <ColorTest />;
return <Test2138 />;
}
61 changes: 61 additions & 0 deletions apps/test-examples/src/Test2138.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import * as React from 'react';

import {Alert, StyleSheet, Text, View} from 'react-native';
import Svg, {Path, Rect} from 'react-native-svg';

export default function App() {
return (
<View style={styles.outer}>
<View style={styles.inner}>
{/* Test Case 1 */}
<Shape width={100} height={100} />
{/* Test Case 2 */}
<Shape width="50%" />
{/* Test Case 3 */}
<Shape height="50%" />
{/* Test Case 4 */}
<Shape width="50%" height="50%" />
{/* Test Case 5 */}
<Shape />
{/* Test Case 6 */}
<Shape width={200} />
{/* Test Case 7 */}
<Svg viewBox="0 0 100 50" width="100%" height="50%">
<Rect x="0" y="0" width="100" height="50" fill="blue" />
</Svg>
</View>
</View>
);
}

function Shape(props: any) {
return (
<Svg
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 24 24"
fill="black"
{...props}>
<Path d="M12 2c5.514 0 10 4.486 10 10s-4.486 10-10 10S2 17.514 2 12 6.486 2 12 2zm0-2C5.373 0 0 5.373 0 12s5.373 12 12 12 12-5.373 12-12S18.627 0 12 0zm5.507 13.941c-1.512 1.195-3.174 1.931-5.506 1.931-2.334 0-3.996-.736-5.508-1.931L6 14.434C7.127 16.154 9.2 18 12.001 18c2.8 0 4.872-1.846 5.999-3.566l-.493-.493zM8.5 8a1.5 1.5 0 100 3 1.5 1.5 0 000-3zm7 0a1.5 1.5 0 100 3 1.5 1.5 0 000-3z" />
</Svg>
);
}

const styles = StyleSheet.create({
inner: {
flexDirection: 'row',
justifyContent: 'space-evenly',
height: 100,
flex: 1,
backgroundColor: 'green',
},

outer: {
marginTop: 100,
flexDirection: 'row',
justifyContent: 'center',
alignItems: 'center',
backgroundColor: 'red',
height: 150,
width: '100%',
},
});
26 changes: 25 additions & 1 deletion src/elements/Svg.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,7 @@
}
const handle = findNodeHandle(this.root as Component);
const RNSVGSvgViewModule: Spec =
// eslint-disable-next-line @typescript-eslint/no-var-requires
require('../fabric/NativeSvgViewModule').default;

Check warning on line 90 in src/elements/Svg.tsx

View workflow job for this annotation

GitHub Actions / build

Require statement not part of import statement
RNSVGSvgViewModule.toDataURL(handle, options, callback);
};

Expand Down Expand Up @@ -127,6 +126,31 @@
strokeLinejoin,
strokeMiterlimit,
} = stylesAndProps;

// Ensure width and height are treated correctly if they are percentages
const isWidthPercentage = typeof width === 'string' && width.includes('%');
const isHeightPercentage =
typeof height === 'string' && height.includes('%');

if (viewBox) {
const viewBoxValues = viewBox.split(' ');
if (viewBoxValues.length === 4) {
const viewBoxWidth = parseFloat(viewBoxValues[2]);
const viewBoxHeight = parseFloat(viewBoxValues[3]);
if (!isNaN(viewBoxWidth) && !isNaN(viewBoxHeight)) {
if (width !== undefined && height === undefined) {
height = isWidthPercentage
? `${(parseFloat(width) * viewBoxHeight) / viewBoxWidth}%`
Copy link
Member

Choose a reason for hiding this comment

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

I don't think the % value is being calculated correctly. For instance, if the screen width is 200 and the height is 600, and we have an SVG with a viewBox proportion of 1 (such as 100/100) and a width set to 50%, the height should match the width. However, in this implementation, the height ends up being 50% of the screen height, which is 300, rather than the expected 100.

: `${(parseFloat(width) * viewBoxHeight) / viewBoxWidth}`;
} else if (height !== undefined && width === undefined) {
width = isHeightPercentage
? `${(parseFloat(height) * viewBoxWidth) / viewBoxHeight}%`
: `${(parseFloat(height) * viewBoxWidth) / viewBoxHeight}`;
}
}
}
}

if (width === undefined && height === undefined) {
width = height = '100%';
}
Expand Down Expand Up @@ -185,7 +209,7 @@
props.transform = gStyle.transform;
gStyle.transform = undefined;
}
props.transform = extractTransformSvgView(props as any);

Check warning on line 212 in src/elements/Svg.tsx

View workflow job for this annotation

GitHub Actions / build

Unexpected any. Specify a different type
}

const RNSVGSvg = Platform.OS === 'android' ? RNSVGSvgAndroid : RNSVGSvgIOS;
Expand Down
Loading