Skip to content

Commit c8c656b

Browse files
chore(runway): cherry-pick fix: cp-7.47.0 Use SVG width/height when available (#16026)
- fix: cp-7.47.0 Use SVG width/height when available (#16016)
1 parent f245366 commit c8c656b

1 file changed

Lines changed: 29 additions & 24 deletions

File tree

app/components/Snaps/SnapUIImage/SnapUIImage.tsx

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,48 +11,54 @@ export interface SnapUIImageProps {
1111
borderRadius?: number;
1212
}
1313

14-
function getViewBox(svg: string) {
14+
function parseSvg(svg: string) {
1515
const viewBoxMatch = svg.match(/viewBox="([^"]+)"/);
16+
const widthMatch = svg.match(/width="([^"]+)"/);
17+
const heightMatch = svg.match(/height="([^"]+)"/);
18+
19+
const viewBox = viewBoxMatch?.[1];
20+
const width = widthMatch?.[1];
21+
const height = heightMatch?.[1];
1622

17-
if (viewBoxMatch?.[1]) {
18-
return viewBoxMatch?.[1];
23+
if (viewBox && width && height) {
24+
return { viewBox, height, width };
1925
}
2026

21-
const widthMatch = svg.match(/width="([^"]+)"/);
22-
const heightMatch = svg.match(/height="([^"]+)"/);
27+
if (viewBox) {
28+
const viewBoxElements = viewBox.split(' ');
29+
return { viewBox, width: viewBoxElements[2], height: viewBoxElements[3] };
30+
}
2331

24-
if (widthMatch?.[1] && heightMatch?.[1]) {
25-
const width = widthMatch[1];
26-
const height = heightMatch[1];
27-
return `0 0 ${width} ${height}`;
32+
if (width && height) {
33+
return { viewBox: `0 0 ${width} ${height}`, width, height };
2834
}
2935

30-
return undefined;
36+
return null;
3137
}
3238

33-
function getDimensions(viewBox?: string) {
34-
if (!viewBox) {
39+
function getDimensions(svg: string) {
40+
const result = parseSvg(svg);
41+
42+
if (!result) {
3543
return null;
3644
}
3745

38-
const viewBoxElements = viewBox.split(' ');
39-
const originalWidth = viewBoxElements[2];
40-
const originalHeight = viewBoxElements[3];
46+
const { viewBox, height, width } = result;
4147

42-
if (!originalHeight || !originalWidth) {
43-
return null;
48+
if (!height || !width) {
49+
return { viewBox };
4450
}
4551

46-
const parsedWidth = parseInt(originalWidth, 10);
47-
const parsedHeight = parseInt(originalHeight, 10);
52+
const parsedWidth = parseInt(width, 10);
53+
const parsedHeight = parseInt(height, 10);
4854

4955
const aspectRatio = parsedWidth / parsedHeight;
5056

5157
if (!Number.isFinite(aspectRatio)) {
52-
return null;
58+
return { viewBox };
5359
}
5460

55-
return { aspectRatio, height: parsedHeight, width: parsedWidth };
61+
return { viewBox, aspectRatio, height: parsedHeight, width: parsedWidth };
5662
}
5763

5864
export const SnapUIImage: React.FC<SnapUIImageProps> = ({
@@ -62,8 +68,7 @@ export const SnapUIImage: React.FC<SnapUIImageProps> = ({
6268
style,
6369
borderRadius,
6470
}) => {
65-
const viewBox = getViewBox(value);
66-
const dimensions = getDimensions(viewBox);
71+
const dimensions = getDimensions(value);
6772
const aspectRatio = dimensions?.aspectRatio ?? 1;
6873
const width = propWidth ?? dimensions?.width;
6974
const height = propHeight ?? dimensions?.height;
@@ -93,7 +98,7 @@ export const SnapUIImage: React.FC<SnapUIImageProps> = ({
9398
}}
9499
height="100%"
95100
width="100%"
96-
viewBox={viewBox}
101+
viewBox={dimensions?.viewBox}
97102
/>
98103
</View>
99104
);

0 commit comments

Comments
 (0)