question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Svg loses gradients displaying a black color

See original GitHub issue

Bug

I’m trying to use svg’s which have some gradients, when I import them using as a file with react-native-svg-transformer I get the svg displayed but with a black color instead of keeping the defined colors/gradients. Same happens if I convert the web svg into a react-native-svg component using svgr.now.sh

Svg file:

Svg displayed on react-native:

Environment info

info Fetching system and libraries information...
System:
    OS: Linux 5.0 Ubuntu 18.04.3 LTS (Bionic Beaver)
    CPU: (4) x64 Intel(R) Core(TM) i5-7200U CPU @ 2.50GHz
    Memory: 2.02 GB / 7.69 GB
    Shell: 4.4.20 - /bin/bash
  Binaries:
    Node: 10.16.2 - /usr/bin/node
    npm: 6.9.0 - /usr/bin/npm
    Watchman: 4.9.0 - /usr/local/bin/watchman
  SDKs:
    Android SDK:
      API Levels: 23, 27, 28, 29
      Build Tools: 23.0.1, 27.0.3, 28.0.3, 29.0.2
      System Images: android-29 | Google APIs Intel x86 Atom
  npmPackages:
    react: 16.9.0 => 16.9.0 
    react-native: 0.61.2 => 0.61.2 
  npmGlobalPackages:
    react-native-cli: 2.0.1
    react-native-debugger-open: 0.3.20

Versions

    "react": "16.9.0",
    "react-native": "0.61.2",
    "react-native-svg": "^9.11.1",
    "react-native-svg-transformer": "^0.13.0"

Describe what you expected to happen:

  1. To see the svg displayed correctly with the default gradients and colors

Reproducible sample code with svg file and react component

https://github.com/tiagocorreiaitcenter/react-native-svg-file-test

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:17 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
msandcommented, Nov 2, 2019

@tiagocorreiaitcenter The other svg file contains a style element with css inside it, and if you use svgr, you need to specify settings (onlyMatchedOnce to false) for the inlineStyles plugin, because the selectors in your css match more than a single element, and by default it doesn’t inline those css rules:

{
  "plugins": [
    {
      "inlineStyles": {
        "onlyMatchedOnce": false
      }
    }
  ]
}

@neoziro Perhaps this should be made the default when setting the react-native option in svgr?

Screenshot 2019-11-02 at 13 57 10

Alternatively, you can use the SvgCss element from react-native-svg like this:

import React from 'react';
import {SvgCss} from 'react-native-svg';

const xml = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 200 200">
  <style>.bones{fill:#ccc ;} .eye{fill:#666;}</style>

  <path class="bones"
        d="M38.8 97.7c-2.1 1.1-4.2 2.4-6.1 4-.9.8-3.4 3.1-3.1 4.5.6 3.1 6.4.1 7.9-.7 2.7-1.4 8.6-3.7 9.9-6.6 2.4-5.3-6.7-2.2-8.6-1.2zM143.4 82c-.9-4.1-4.2-8.9-7.7-11.2-5.1-3.3-14.6-8.2-20.1-3.1l.6-.4c-5 4.7 5.5 1.2 6.2 3.1.4 1.1-2.6 2.2-3.3 3-1.1 1.2-1.7 3.1-1.6 4.9.4 6.3 7.9 10.6 13.5 11.4 4.5.7 13.8-1.7 12.4-7.7z"/>

  <path class="bones"
        d="M179.1 128.8c-4.9-4.2-10.6.5-19-4-2.2-1.2-9.1-5.7-18.3-11.7 1.7-.9 3-1.9 4.1-3 3.9-4.1 4.5-7.1 4.6-8.2 1.2-1 4.8-4.7 5.2-13.2.3-6-2.6-8.7-7.1-11-.2-.1-.5-.3-.7-.4-1-.7-1.9-1.9-2.7-3-.5-.7-1-1.3-1.6-1.9-1.4-1.5-2.9-2.9-4.3-4.1 2.6-3.2 4.5-5.5 5.5-6.5 6.6-6.9 13.5-4.3 16.8-9.8 3.3-5.5 1.6-11.9-2-12.5-2.7-.4-5.2 1.1-7 .3-.7-.3-1.3-1-1.8-2.3-.9-2.5-1.3-5.8-4.6-6-4.5-.2-11.6.8-10.7 9 .9 8.2 5.1 6.2-5.9 21.7l-.1.1c-2-.7-3.9-1.1-5.7-1.1-1.2 0-2.5.2-3.6.5-5.9 1.6-9 3.6-11 4.9-1.2.8-2.1 1.3-2.8 1.3-.3 0-.6-.1-.9-.2-.6-.2-1.2-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.9 1.1 1.4 1.7.9 1.2 2 2.6 3.4 3.6.4.3.8.5 1.2.7 4.2 2.1 5.4 3.9 5.2 7.8-.3 8.1-3.9 10.7-4.1 10.8l-1.5 1 .4 1.4c0 .4-.4 2.6-3.6 6-3.9 4.6-13.7 6.8-35.5 10.5z"/>

  <g>
    <path class="eye"
          d="M128.1 79.6c1.6 1.3 3.1 2.6 4.7 3.9.6.5 2.7 2.8 3.2 1.4.5-1.3-2.4-3.5-3.2-4.3-1-1-2.2-2-3.2-3.1-1.2-1.2-4.2-5.1-6.4-3.4-.8.6-.3.9.3 1.5 1.5 1.4 3 2.7 4.6 4z"/>
    <path class="eye"
          d="M130.4 78.1c-1.5 1.4-2.9 2.9-4.3 4.3-.5.5-3.1 2.4-1.7 3.1 1.2.6 3.7-2.1 4.6-2.8 1.1-.9 2.2-2 3.4-2.9 1.3-1.1 5.4-3.8 4-6.1-.5-.8-.8-.4-1.5.2-1.6 1.4-3.1 2.8-4.5 4.2z"/>
  </g>
</svg>`;

export default () => {
  return <SvgCss xml={xml} width="100%" height="100%" />;
};
0reactions
stale[bot]commented, Feb 14, 2020

Closing this issue after a prolonged period of inactivity. Fell free to reopen this issue, if this still affecting you.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Svg loses gradients displaying a black color #1153 - GitHub
I'm trying to use svg's which have some gradients, when I import them using as a file with react-native-svg-transformer I get the svg...
Read more >
Why are the gradients in this SVG displaying as black?
For example, both SVGs have gradients named accend_1_ and decend_1_ . All id s need to be unique. Try giving them different ids....
Read more >
Gradients in SVG - SVG: Scalable Vector Graphics | MDN
For instance, this one tells the gradient to start at the color red, change to transparent-black in the middle, and end at the...
Read more >
Some SVG gradients are missing - Adobe XD: Feature Requests
I found a workaround. 1. Select the shape with the errant gradient. 2. Select the color fill in ...
Read more >
Use gradients in Photoshop - Adobe Support
A noise gradient is a gradient that contains randomly distributed colors within the range of colors that you specify. ... Select the Gradient...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found