Skip to content
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

Nested SVGs not rendering gradients #1437

Closed
cmlarsen opened this issue Aug 13, 2020 · 4 comments · Fixed by #1948
Closed

Nested SVGs not rendering gradients #1437

cmlarsen opened this issue Aug 13, 2020 · 4 comments · Fixed by #1948
Labels

Comments

@cmlarsen
Copy link

cmlarsen commented Aug 13, 2020

I am drawing an image that pulls in icons defined as SVGs from config files. When the svgs are pulled in I am not seeing the gradients render in the nested SVGs.

I have experienced this on RN SVG ^9.13.0 to 12.1.0 in iOS

Minimal Example:

import * as React from 'react';
import {Svg, Defs, LinearGradient, Stop, G, Rect, Circle} from 'react-native-svg';

export default function App() {
  return (
    
       <Svg width={150} height={50}>
        <Defs>
          <LinearGradient id="grad1" x1={'0%'} y1={'0%'} x2={'100%'} y2={'100%'}>
            <Stop offset={0} stopColor="red" />
            <Stop offset={1} stopColor="blue" />
          </LinearGradient>
        </Defs>
        <Rect x={0} y={0} width={150} height={50} fill={'pink'} />
         {/* Renders gradient correctly*/}
        <Circle cx={25} cy={25} r={25} fill={'url(#grad1)'} stroke="black" />

        <G x={50} y={0}>
          <Svg width={50} height={50}>
            {/* Renders red BG correctly*/}
            <Circle cx={25} cy={25} r={25} fill={'red'} stroke="black" />
          </Svg>
        </G>

        <G x={100} y={0}>
          <Svg width={50} height={50}>
            <Defs>
              <LinearGradient id="grad2" x1={'0%'} y1={'0%'} x2={'100%'} y2={'100%'}>
                <Stop offset={0} stopColor="red" />
                <Stop offset={1} stopColor="blue" />
              </LinearGradient>
            </Defs>
            {/* Does not render gradient, fill is transparent on iOS*/}
            <Circle cx={25} cy={25} r={25} fill={'url(#grad2)'} strokeWidth={1} stroke="black" />
          </Svg>
        </G>
      </Svg>
    
  );
}

Output Screenshot:

image

Snack:

In this snack, it appears to work on Web and Android, but fails in iOS
https://snack.expo.io/eQ62NyiMSr

@cmlarsen
Copy link
Author

As a work around I've found that I if I wrap the nested SVG in a RN View and ForgienObject it works, like so:

<Svg>
...rest of the SVG code
<ForgienObject>
   <View>
         <Svg width={50} height={50}>
            <Defs>
              <LinearGradient id="grad2" x1={'0%'} y1={'0%'} x2={'100%'} y2={'100%'}>
                <Stop offset={0} stopColor="red" />
                <Stop offset={1} stopColor="blue" />
              </LinearGradient>
            </Defs>
            <Circle cx={25} cy={25} r={25} fill={'url(#grad2)'} strokeWidth={1} stroke="black" />
          </Svg>
    </View>
</ForgienObject>
</Svg>

@cmlarsen cmlarsen changed the title Nested SVGs don't render gradients Nested SVGs not rendering gradients Aug 14, 2020
@stale
Copy link

stale bot commented Oct 15, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. You may also mark this issue as a "discussion" and I will leave this open.

@Ttecs
Copy link

Ttecs commented May 1, 2022

I found a solution that instead of using the svgs having gradient,I converted the SVG into a Lottie file. that works great and as an extra advantage, we can transform the SVG into a simple animation :)

@WoLewicki
Copy link
Member

This should be fixed by #1939. Can you check if it is so?

@WoLewicki WoLewicki linked a pull request Jan 3, 2023 that will close this issue
WoLewicki added a commit that referenced this issue Jan 3, 2023
PR making the nested svgs parse the references. Originally authored by @tiredBlueWhale.

Co-authored-by: Luca Meyer <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
3 participants