Skip to content

SVG rendering problem #17645

Closed
Closed

Description

@burnmaniac

Do you want to request a feature or report a bug?
Report a bug.

What is the current behavior?
SVG is not rendered properly.

What is the expected behavior?
SVG is rendered properly.

Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React?
Chrome: latest version. Everything else is below.

We have SVG optimizer in our pipeline, but that's clearly not a problem as the sprite I get is completely okay and works properly.

I suspect this has something to do with React rendering this SVG because I've tried to load the same SVG in CSS and it works (using background-image). I guess React applies some mapping/transforming when rendering SVG and this is failing with this SVG.

Screenshot:
Screenshot 2019-12-18 at 10 56 06

Original SVG:

<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="234" height="74" viewBox="0 0 234 74">
    <defs>
        <linearGradient id="a" x1="50%" x2="48.864%" y1="48.434%" y2="51.159%">
            <stop offset="0%" stop-color="#0080FF" stop-opacity=".5"/>
            <stop offset="100%" stop-color="#0080FF" stop-opacity="0"/>
        </linearGradient>
        <linearGradient id="d" x1="100%" x2="0%" y1="50%" y2="50%">
            <stop offset="0%" stop-color="#404752"/>
            <stop offset="85.856%" stop-color="#0080FF"/>
            <stop offset="100%" stop-color="#0080FF"/>
        </linearGradient>
        <path id="c" d="M43 17h10.694C73.625 17 89.181 37 113 37"/>
        <filter id="b" width="157.1%" height="300%" x="-28.6%" y="-100%" filterUnits="objectBoundingBox">
            <feMorphology in="SourceAlpha" operator="dilate" radius="2" result="shadowSpreadOuter1"/>
            <feOffset in="shadowSpreadOuter1" result="shadowOffsetOuter1"/>
            <feMorphology in="SourceAlpha" radius="2" result="shadowInner"/>
            <feOffset in="shadowInner" result="shadowInner"/>
            <feComposite in="shadowOffsetOuter1" in2="shadowInner" operator="out" result="shadowOffsetOuter1"/>
            <feGaussianBlur in="shadowOffsetOuter1" result="shadowBlurOuter1" stdDeviation="6"/>
            <feColorMatrix in="shadowBlurOuter1" values="0 0 0 0 0 0 0 0 0 0.501960784 0 0 0 0 1 0 0 0 1 0"/>
        </filter>
        <path id="e" d="M3.549 13.222l-2.771-2.77L7.059 4.16H2.178V.778h11.044v11.044H9.85V6.941z"/>
        <path id="h" d="M120 37h5.132a30 30 0 0 0 9.487-1.54l2.881-.96a23.717 23.717 0 0 1 15 0l13.013 4.338a30 30 0 0 0 18.974 0L197.5 34.5a23.717 23.717 0 0 1 15 0l2.881.96a30 30 0 0 0 9.487 1.54H230"/>
        <filter id="g" width="136.4%" height="470.9%" x="-18.2%" y="-187.2%" filterUnits="objectBoundingBox">
            <feMorphology in="SourceAlpha" operator="dilate" radius="2" result="shadowSpreadOuter1"/>
            <feOffset in="shadowSpreadOuter1" result="shadowOffsetOuter1"/>
            <feMorphology in="SourceAlpha" radius="2" result="shadowInner"/>
            <feOffset in="shadowInner" result="shadowInner"/>
            <feComposite in="shadowOffsetOuter1" in2="shadowInner" operator="out" result="shadowOffsetOuter1"/>
            <feGaussianBlur in="shadowOffsetOuter1" result="shadowBlurOuter1" stdDeviation="6"/>
            <feColorMatrix in="shadowBlurOuter1" values="0 0 0 0 0.454901961 0 0 0 0 0.498039216 0 0 0 0 0.533333333 0 0 0 1 0"/>
        </filter>
    </defs>
    <g fill="none" fill-rule="evenodd">
        <path fill="url(#a)" d="M0 19h53.694C73.625 19 89.181 39 113 39L2.867 34.127A3 3 0 0 1 0 31.13V19z"/>
        <g stroke-linecap="round">
            <use fill="#000" filter="url(#b)" xlink:href="#c"/>
            <use stroke="url(#d)" stroke-width="4" xlink:href="#c"/>
        </g>
        <path fill="#0080FF" d="M32.284 0H3a3 3 0 0 0-3 3v28a3 3 0 0 0 3 3h29.284a3 3 0 0 0 2.586-1.479l8.235-14a3 3 0 0 0 0-3.042l-8.235-14A3 3 0 0 0 32.284 0z"/>
        <g transform="translate(12 10)">
            <mask id="f" fill="#fff">
                <use xlink:href="#e"/>
            </mask>
            <use fill="#000" fill-rule="nonzero" xlink:href="#e"/>
            <g fill="#FFF" fill-rule="nonzero" mask="url(#f)">
                <path d="M-1-1h16v16H-1z"/>
            </g>
        </g>
        <g stroke-dasharray="0,8" stroke-linecap="round">
            <use fill="#000" filter="url(#g)" xlink:href="#h"/>
            <use stroke="#404752" stroke-width="4" xlink:href="#h"/>
        </g>
    </g>
</svg>

Sprite, copied from Network tab in DevTools:

<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
    <defs>
        <style>
            .sprite-symbol-usage {display: none;}
            .sprite-symbol-usage:target {display: inline;}
        </style>
        <symbol viewBox="0 0 234 74" id="symbol-voiceLevelStartHigh">
            <defs>
                <path id="symbol-voiceLevelStartHigh_c" d="M43 17h10.694C73.625 17 89.181 37 113 37"/>
                <path id="symbol-voiceLevelStartHigh_e"
                      d="M3.549 13.222l-2.771-2.77L7.059 4.16H2.178V.778h11.044v11.044H9.85V6.941z"/>
                <path id="symbol-voiceLevelStartHigh_h"
                      d="M120 37h5.132a30 30 0 009.487-1.54l2.881-.96a23.717 23.717 0 0115 0l13.013 4.338a30 30 0 0018.974 0L197.5 34.5a23.717 23.717 0 0115 0l2.881.96a30 30 0 009.487 1.54H230"/>
                <filter id="symbol-voiceLevelStartHigh_b" width="157.1%" height="300%" x="-28.6%" y="-100%"
                        filterUnits="objectBoundingBox">
                    <feMorphology in="SourceAlpha" operator="dilate" radius="2"
                                  result="shadowSpreadOuter1"></feMorphology>
                    <feOffset in="shadowSpreadOuter1" result="shadowOffsetOuter1"></feOffset>
                    <feMorphology in="SourceAlpha" radius="2" result="shadowInner"></feMorphology>
                    <feOffset in="shadowInner" result="shadowInner"></feOffset>
                    <feComposite in="shadowOffsetOuter1" in2="shadowInner" operator="out"
                                 result="shadowOffsetOuter1"></feComposite>
                    <feGaussianBlur in="shadowOffsetOuter1" result="shadowBlurOuter1" stdDeviation="6"></feGaussianBlur>
                    <feColorMatrix in="shadowBlurOuter1"
                                   values="0 0 0 0 0 0 0 0 0 0.501960784 0 0 0 0 1 0 0 0 1 0"></feColorMatrix>
                </filter>
                <filter id="symbol-voiceLevelStartHigh_g" width="136.4%" height="470.9%" x="-18.2%" y="-187.2%"
                        filterUnits="objectBoundingBox">
                    <feMorphology in="SourceAlpha" operator="dilate" radius="2"
                                  result="shadowSpreadOuter1"></feMorphology>
                    <feOffset in="shadowSpreadOuter1" result="shadowOffsetOuter1"></feOffset>
                    <feMorphology in="SourceAlpha" radius="2" result="shadowInner"></feMorphology>
                    <feOffset in="shadowInner" result="shadowInner"></feOffset>
                    <feComposite in="shadowOffsetOuter1" in2="shadowInner" operator="out"
                                 result="shadowOffsetOuter1"></feComposite>
                    <feGaussianBlur in="shadowOffsetOuter1" result="shadowBlurOuter1" stdDeviation="6"></feGaussianBlur>
                    <feColorMatrix in="shadowBlurOuter1"
                                   values="0 0 0 0 0.454901961 0 0 0 0 0.498039216 0 0 0 0 0.533333333 0 0 0 1 0"></feColorMatrix>
                </filter>
            </defs>
            <g fill="none" fill-rule="evenodd">
                <path fill="url(#symbol-voiceLevelStartHigh_a)"
                      d="M0 19h53.694C73.625 19 89.181 39 113 39L2.867 34.127A3 3 0 010 31.13V19z"/>
                <g stroke-linecap="round">
                    <use fill="#000" filter="url(#symbol-voiceLevelStartHigh_b)"
                         xlink:href="#symbol-voiceLevelStartHigh_c"/>
                    <use stroke="url(#symbol-voiceLevelStartHigh_d)" stroke-width="4"
                         xlink:href="#symbol-voiceLevelStartHigh_c"/>
                </g>
                <path fill="#0080FF"
                      d="M32.284 0H3a3 3 0 00-3 3v28a3 3 0 003 3h29.284a3 3 0 002.586-1.479l8.235-14a3 3 0 000-3.042l-8.235-14A3 3 0 0032.284 0z"/>
                <g transform="translate(12 10)">
                    <use fill="#000" fill-rule="nonzero" xlink:href="#symbol-voiceLevelStartHigh_e"/>
                    <g fill="#FFF" fill-rule="nonzero" mask="url(#symbol-voiceLevelStartHigh_f)">
                        <path d="M-1-1h16v16H-1z"/>
                    </g>
                </g>
                <g stroke-dasharray="0,8" stroke-linecap="round">
                    <use fill="#000" filter="url(#symbol-voiceLevelStartHigh_g)"
                         xlink:href="#symbol-voiceLevelStartHigh_h"/>
                    <use stroke="#404752" stroke-width="4" xlink:href="#symbol-voiceLevelStartHigh_h"/>
                </g>
            </g>
        </symbol>
        <linearGradient id="symbol-voiceLevelStartHigh_a" x1="50%" x2="48.864%" y1="48.434%" y2="51.159%">
            <stop offset="0%" stop-color="#0080FF" stop-opacity=".5"/>
            <stop offset="100%" stop-color="#0080FF" stop-opacity="0"/>
        </linearGradient>
        <linearGradient id="symbol-voiceLevelStartHigh_d" x1="100%" x2="0%" y1="50%" y2="50%">
            <stop offset="0%" stop-color="#404752"/>
            <stop offset="85.856%" stop-color="#0080FF"/>
            <stop offset="100%" stop-color="#0080FF"/>
        </linearGradient>
        <mask id="symbol-voiceLevelStartHigh_f" fill="#fff">
            <use xlink:href="#symbol-voiceLevelStartHigh_e"/>
        </mask>
    </defs>
    <use id="symbol-voiceLevelStartHigh-usage" xlink:href="#symbol-voiceLevelStartHigh" class="sprite-symbol-usage"/>
</svg>

package.json

{
  "main": "index.js",
  "engines": {
    "node": ">=8"
  },
  "devDependencies": {
    "@babel/core": "^7.6.2",
    "@babel/runtime-corejs3": "^7.6.2",
    "@hot-loader/react-dom": "^16.9.0",
    "autoprefixer": "^9.6.4",
    "babel-core": "^7.0.0-bridge.0",
    "babel-eslint": "^10.0.3",
    "babel-jest": "^24.9.0",
    "babel-loader": "^8.0.5",
    "case-sensitive-paths-webpack-plugin": "^2.2.0",
    "chalk": "^2.4.2",
    "connect-history-api-fallback": "^1.6.0",
    "core-js": "^3.2.1",
    "css-loader": "^2.1.1",
    "cssnano": "^4.1.10",
    "dotenv": "^8.1.0",
    "dotenv-expand": "^5.1.0",
    "enzyme": "^3.9.0",
    "enzyme-adapter-react-16": "^1.12.1",
    "eslint": "^5.16.0",
    "eslint-loader": "^2.1.2",
    "eslint-plugin-import": "^2.16.0",
    "eslint-plugin-jsx-a11y": "^6.2.1",
    "eslint-plugin-react": "^7.16.0",
    "eslint-plugin-react-hooks": "^1.7.0",
    "express": "^4.16.4",
    "file-loader": "^3.0.1",
    "fs-extra": "^7.0.1",
    "html-webpack-plugin": "^3.2.0",
    "isomorphic-fetch": "^2.2.1",
    "jest": "^24.9.0",
    "mini-css-extract-plugin": "^0.6.0",
    "node-sass": "^4.13.0",
    "nodemon": "^1.19.3",
    "object-assign": "^4.1.1",
    "optimize-css-assets-webpack-plugin": "^5.0.1",
    "postcss-flexbugs-fixes": "^4.1.0",
    "postcss-loader": "^3.0.0",
    "promise": "^8.0.3",
    "raf": "^3.4.1",
    "regenerator-runtime": "^0.13.2",
    "require-from-string": "^2.0.2",
    "resolve-url-loader": "^3.1.0",
    "sass-loader": "^7.3.1",
    "style-loader": "^0.23.1",
    "stylelint": "^9.10.1",
    "stylelint-scss": "^3.11.1",
    "stylelint-selector-bem-pattern": "^2.1.0",
    "stylelint-webpack-plugin": "^0.10.5",
    "svg-sprite-loader": "^3.8.0",
    "svg-transform-loader": "^2.0.7",
    "svg-url-loader": "^2.3.2",
    "svgo": "^1.2.1",
    "svgo-loader": "^2.2.0",
    "svgxuse": "^1.2.6",
    "sw-precache-webpack-plugin": "^0.11.5",
    "terser-webpack-plugin": "^1.4.1",
    "url-loader": "^1.1.2",
    "url-search-params-polyfill": "^5.1.0",
    "webpack": "^4.41.0",
    "webpack-dev-middleware": "^3.7.2",
    "webpack-hot-middleware": "^2.24.3",
    "webpack-manifest-plugin": "^2.2.0",
    "yargs": "^13.2.2"
  },
  "dependencies": {
    "axios": "^0.19.0",
    "classnames": "^2.2.6",
    "connected-react-router": "^6.5.2",
    "history": "^4.10.1",
    "immutable": "^4.0.0-rc.12",
    "include-media": "^1.4.9",
    "lodash.debounce": "^4.0.8",
    "moment": "^2.24.0",
    "normalize.css": "^8.0.1",
    "prop-types": "^15.7.2",
    "query-string": "^6.8.3",
    "rc-slider": "^8.7.1",
    "react": "^16.10.2",
    "react-circular-progressbar": "^2.0.2",
    "react-datepicker": "^2.9.6",
    "react-dom": "^16.10.2",
    "react-hot-loader": "^4.12.15",
    "react-redux": "^7.1.1",
    "react-router": "^5.1.2",
    "react-router-config": "^5.1.1",
    "react-select": "^3.0.8",
    "react-smooth-collapse": "^2.1.0",
    "react-switch": "^5.0.1",
    "react-tabs": "^3.0.0",
    "react-transition-group": "^4.3.0",
    "recompose": "^0.30.0",
    "redux": "^4.0.4",
    "redux-devtools-extension": "^2.13.8",
    "redux-logger": "^3.0.6",
    "redux-saga": "^1.1.1",
    "reselect": "^4.0.0",
    "roboto-fontface": "^0.10.0"
  },
  "scripts": {
    "start": "node scripts/start.js",
    "build": "node scripts/build.js",
    "test": "node scripts/test.js --env=jsdom"
  },
  "browserslist": [
    "last 2 Chrome versions"
  ]
}

SVG is rendered using SVGSpriteSymbol component which looks like this:

import React, {PureComponent} from 'react';
import PropTypes from 'prop-types';

export default class SVGSpriteSymbol extends PureComponent {
    static propTypes = {
        className: PropTypes.string,
        spriteSymbol: PropTypes.object.isRequired,
    };

    static defaultProps = {
        className: '',
    };

    render() {
        const {className, spriteSymbol, ...restProps} = this.props;
        return (
            <svg viewBox={spriteSymbol.viewBox} className={className} {...restProps}>
                <use xlinkHref={spriteSymbol.url} />
            </svg>
        );
    }
}

In case I can help in any case, I'd love to, just let me know.

Activity

  1. kunukn commented on Dec 18, 2019

    @kunukn
    Contributor

    Can you give a reproduced example online where it is broken?

    I suspect some mis-mapping might be the issue.
    Convertion from html to React might not be fully correct.

    I tried to reproduce and it seems to render correctly.

    Demo:
    https://codesandbox.io/s/reactjs-svg-rendering-ll5te

  2. bvaughn commented on Dec 19, 2019

    @bvaughn
    Contributor

    @burnmaniac a runnable repro would help here.

  3. bvaughn commented on Jan 9, 2020

    @bvaughn
    Contributor

    Closing due to lack of repro information and the fact that it seems to be working as expected when a Code Sandbox repro was created.

    If you'd like to share a repro case with us that shows the original bug, we can re-open this.

  4. burnmaniac commented on Jan 23, 2020

    @burnmaniac
    Author

    @bvaughn Sorry for my absence.

    I can't reproduce it on CodeSandbox for some reason. Is it okay if I create a project using create-react-app and try to reproduce it there?

  5. bvaughn commented on Jan 23, 2020

    @bvaughn
    Contributor

    Yes.

  6. burnmaniac commented on Feb 3, 2020

    @burnmaniac
    Author

    @bvaughn Sorry, I finally caught some time to test this a bit more.

    I've tried to reproduce the issue in a sample app made by create-react-app without success. It works well if I import it as import {ReactComponent as SomeSVG} from 'some.svg'.

    I've tried to eject and then add svg-sprite-loader, but without luck. If someone can help with this, I'm happy to try to reproduce it that way, so we can be sure that it's not a React issue, but svg-sprite-loader issue.

    I will open the ticket on their repo, unfortunately, I don't see much activity there. Thanks for the help and sorry to bother you with this.

  7. bvaughn commented on Feb 3, 2020

    @bvaughn
    Contributor

    That's okay. It happens.

    At this point it seems unlikely to me that it's a React issue, so I hope you're able to get a response on the other repo!

  8. burnmaniac commented on Feb 4, 2020

    @burnmaniac
    Author

    Yeah, I agree. I opened the issue on their repo. Hopefully, it'll be resolved.

    For now, I'm just loading the SVG in CSS using background-image.

    Thanks again!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions