Skip to content

fix: apply x/y props on nested Svg elements as translate transform (#1283)#2973

Open
MuhammadSuleman97 wants to merge 1 commit into
software-mansion:mainfrom
MuhammadSuleman97:fix/svg-nested-x-y-props
Open

fix: apply x/y props on nested Svg elements as translate transform (#1283)#2973
MuhammadSuleman97 wants to merge 1 commit into
software-mansion:mainfrom
MuhammadSuleman97:fix/svg-nested-x-y-props

Conversation

@MuhammadSuleman97

Copy link
Copy Markdown

Summary

Fixes #1283 — Nested SVG elements do not respect x and y props for positioning.

Problem

When embedding an <Svg> inside another <Svg>, setting the x and y props on the inner SVG had no effect. On the web, nested SVG elements respect x/y for viewport positioning within their parent SVG.

Fix

The x and y props are now extracted in the Svg element's render method and converted to a translate transform that gets merged with any existing transform prop.

if (x != null || y != null) {
  const translateTransform = [
    { translateX: x != null ? parseFloat(String(x)) : 0 },
    { translateY: y != null ? parseFloat(String(y)) : 0 },
  ];
  props.transform = svgTransform
    ? [...svgTransformArray, ...translateTransform]
    : translateTransform;
}

The x and y props are also removed from the extracted props before passing to the native component to avoid unknown prop warnings.

Test Plan

<Svg width="400" height="400">
  <Svg x="50" y="100" width="200" height="200">
    <Rect width="200" height="200" fill="blue" />
  </Svg>
</Svg>

The inner SVG (blue rect) should be positioned at (50, 100) within the outer SVG.

When an Svg is nested inside another Svg, the x and y props were
not being applied. On the web, nested SVG elements respect x/y
for positioning within their parent SVG.

This fix extracts x and y from the Svg element props and converts
them to a translate transform that gets merged with any existing
transform prop, matching the behavior of standard SVG elements.

Fixes software-mansion#1283
Copilot AI review requested due to automatic review settings June 6, 2026 14:34

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

Adds support for positioning the root <Svg> using x/y by converting them into a translated React Native transform, and prevents x/y from being forwarded as native props.

Changes:

  • Destructure x/y from extracted props and remove them from the forwarded props object.
  • Extend transform extraction to optionally append translateX/translateY based on x/y.
  • Stop forwarding onLayout in the props object passed to the rendered component.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/elements/Svg.tsx
Comment on lines 184 to +191
const gStyle = Object.assign({}, StyleSheet.flatten(style));
if (transform) {
if (transform || x != null || y != null) {
if (gStyle.transform) {
props.transform = gStyle.transform;
gStyle.transform = undefined;
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
props.transform = extractTransformSvgView(props as any);
const svgTransform = extractTransformSvgView(props as any);
Comment thread src/elements/Svg.tsx
Comment on lines +191 to +198
const svgTransform = extractTransformSvgView(props as any);
if (x != null || y != null) {
const translateX = x != null ? parseFloat(String(x)) : 0;
const translateY = y != null ? parseFloat(String(y)) : 0;
const translateTransform = [
{ translateX },
{ translateY },
];
Comment thread src/elements/Svg.tsx
Comment on lines 140 to +142
const props: extractedProps = extracted as extractedProps;
delete (props as Record<string, unknown>).x;
delete (props as Record<string, unknown>).y;
Comment thread src/elements/Svg.tsx
Comment on lines 230 to 234
strokeLinecap,
strokeLinejoin,
strokeMiterlimit,
onLayout,
}}
/>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SVG in SVG does not have x/y props applied

2 participants