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

On the Android platform, when using currentColor as the fill value, the opacity property is ignored. #1345

Closed
gaodeng opened this issue Apr 14, 2020 · 12 comments · Fixed by #2080

Comments

@gaodeng
Copy link

gaodeng commented Apr 14, 2020

Unexpected behavior

On the Android platform, when using currentColor as the fill value, the opacity property is ignored. The iOS platform has no such problems

Simulator Screen Shot - iPhone 11 - 2020-04-14 at 14 02 29
Screenshot_1586844684

Environment info

 "react-native-svg": "^12.1.0",

Steps To Reproduce

 let xml = `<?xml version="1.0" encoding="utf-8"?>
  <svg version="1.1" id="e98f3141-fbb2-4194-a12f-8ac40d4ee285"
     xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 200 200"
     style="enable-background:new 0 0 200 200;" xml:space="preserve">
  <title>listen_now_inactive</title>
  <path fill="currentColor" d="M85.3,24.3h-59c-6.2,0-11.3,5-11.3,11.1c0,0.1,0,0.1,0,0.2v8.7h92.8l-8.3-12.5C96.2,27.2,90.9,24.4,85.3,24.3z"
    />
  <g>
    <path fill="currentColor" opacity="0.1" d="M173.7,44.3H15v119c0,0.1,0,0.1,0,0.2c0,6.2,5.1,11.2,11.3,11.1h147.4c0.1,0,0.1,0,0.2,0
      c6.2,0,11.2-5.1,11.1-11.3V55.6c0-0.1,0-0.1,0-0.2C185,49.3,179.9,44.3,173.7,44.3z M127.6,114.1l-47.3,20.8
      c-0.6,0.3-1.3,0.4-2,0.4c-2.8,0-5.1-2.3-5.1-5.1V88.7c0-0.7,0.1-1.4,0.4-2c1.1-2.6,4.1-3.7,6.7-2.6l47.3,20.8
      c1.3,0.5,2.2,1.5,2.8,2.7C131.5,110.1,130.2,113,127.6,114.1z"/>
    <path fill="currentColor" d="M127.6,104.8L80.3,84c-2.6-1.1-5.5,0-6.7,2.6c-0.3,0.6-0.4,1.3-0.4,2v41.5c0,2.8,2.3,5.1,5.1,5.1
      c0.7,0,1.4-0.1,2-0.4l47.3-20.8c2.6-1.1,3.8-4,2.8-6.6C129.9,106.2,128.9,105.3,127.6,104.8z"/>
  </g>
  </svg>`;

 <SvgCss xml={xml} width="100" height="100" color="red" />

Describe what you expected to happen:
The renderings of iOS and Android platforms should look the same

@stale
Copy link

stale bot commented Jun 13, 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.

@stale stale bot added stale and removed stale labels Jun 13, 2020
@deansimcox
Copy link

I'm also seeing this issue in version 11.0.1.
Both the opacity and fill-opacity attributes are ignored when currentColor is used.

@NuckChorris
Copy link

NuckChorris commented Aug 9, 2020

I am also seeing this issue, I have not found any workaround. I tried putting it in a group with an opacity and that did not work either.

@vishwasHK7
Copy link

vishwasHK7 commented Sep 24, 2020

I am also facing this issue. Any workaround please. Thanks in advance.

"react-native-svg": "12.1.0",

@vishwasHK7
Copy link

vishwasHK7 commented Sep 24, 2020

I am also facing this issue. Any workaround please. Thanks in advance.

"react-native-svg": "12.1.0",

Hi All,

I created wrapper to make it work in iOS And Android:

Usage:
<SVGWrapper width={128} height={128} color={theme.primaryColor} xml={<xmlString>} />

SVGWrapper.js

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

const SVGWrapper = props => {
  const { color = "FF0000" xml = '' } = props;
  const icon = xml.replace('currentColor', color);
  return <SvgFromXml {...props} xml={icon} />;
};

export default SVGWrapper;

@psnfrench
Copy link

I faced the same issue too, but a workaround that worked for me was to change the opacity to the style prop ie.
change:
opacity="0.4"
to:
style="opacity:0.4"

@ecklf
Copy link

ecklf commented Jun 5, 2021

Facing the same issue on Android with stroke="currentColor". I work around by setting the color via prop for now.

@niklavsBariss
Copy link

niklavsBariss commented Jul 14, 2021

@vishwasHK7 , how do you import the svg as string to use replace?

@thgh
Copy link

thgh commented Aug 17, 2021

Here is a workaround:

const ExampleIcon = () =>
  opacityWorkaround(
    <Svg>
      <Path opacity={0.3} d="M5 5h14v2H5V5z" fill="currentColor" />
    </Svg>,
    'blue'
  )

function opacityWorkaround(path: ReactElement, color: Color) {
  if (Array.isArray(path.props.children)) {
    path = deepSet(
      path,
      'props.children',
      path.props.children.map(child => opacityWorkaround(child, color))
    )
  }
  if (path.props.stroke === 'currentColor') {
    path = deepSet(path, 'props.stroke', color)
  }
  if (path.props.fill === 'currentColor') {
    path = deepSet(path, 'props.fill', color)
  }
  return path
}

@robrechtme
Copy link

@stvmachine
Copy link

Here is a workaround:

const ExampleIcon = () =>
  opacityWorkaround(
    <Svg>
      <Path opacity={0.3} d="M5 5h14v2H5V5z" fill="currentColor" />
    </Svg>,
    'blue'
  )

function opacityWorkaround(path: ReactElement, color: Color) {
  if (Array.isArray(path.props.children)) {
    path = deepSet(
      path,
      'props.children',
      path.props.children.map(child => opacityWorkaround(child, color))
    )
  }
  if (path.props.stroke === 'currentColor') {
    path = deepSet(path, 'props.stroke', color)
  }
  if (path.props.fill === 'currentColor') {
    path = deepSet(path, 'props.fill', color)
  }
  return path
}

From where are you getting that definition of deepSet that you are using?

@thgh
Copy link

thgh commented Nov 28, 2022

laptou added a commit to laptou/react-native-svg that referenced this issue Jul 2, 2023
WoLewicki added a commit that referenced this issue Jul 5, 2023
I implemented this by multiplying the alpha of the currentColor by the opacity when setting a currentColor brush.

Co-authored-by: Wojciech Lewicki <wojciech.lewicki@swmansion.com>
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 a pull request may close this issue.

10 participants