简体   繁体   English

无法使用 React 覆盖 SVG 图标上的填充属性

[英]Unable to override fill property on SVG icon using React

I have a bunch of icons I have exported from Figma and I would like to create a wrapper Component around those icons, using the benefit of <use> tag.我有一堆从 Figma 导出的图标,我想利用<use>标签的优势围绕这些图标创建一个包装组件。

So I created a SVG file with all the symbols in order to target them by their id.所以我创建了一个包含所有符号的 SVG 文件,以便通过它们的 id 来定位它们。

The problem is, I am not able to override the fill property on the reused icons.问题是,我无法覆盖重用图标的fill属性。 After a lot of reading, I know that I'm supposed to use CSS inheritance since the reused element is in the shadow DOM and its properties cannot be accessed as usual.经过大量阅读,我知道我应该使用 CSS inheritance 因为重用的元素在影子 DOM 中,并且它的属性不能像往常一样访问。

Just for debugging purposes, I copy pasted the source code of one of those icons directly in the component (and not using the import mechanism), and surprisingly, it works.仅出于调试目的,我将其中一个图标的源代码直接复制粘贴到组件中(而不使用导入机制),令人惊讶的是,它可以工作。

Here is the code:这是代码:

icon.svg图标.svg

<svg xmlns="http://www.w3.org/2000/svg">
  <symbol id="ic">
    <path fill="#000" d="M81,40.933c0-4.25-3-7.811-6.996-8.673c-0.922-5.312-3.588-10.178-7.623-13.844  c-2.459-2.239-5.326-3.913-8.408-4.981c-0.797-3.676-4.066-6.437-7.979-6.437c-3.908,0-7.184,2.764-7.979,6.442  c-3.078,1.065-5.939,2.741-8.396,4.977c-4.035,3.666-6.701,8.531-7.623,13.844C22.002,33.123,19,36.682,19,40.933  c0,2.617,1.145,4.965,2.957,6.589c0.047,0.195,0.119,0.389,0.225,0.568l26.004,43.873c0.383,0.646,1.072,1.04,1.824,1.04  c0.748,0,1.439-0.395,1.824-1.04L77.82,48.089c0.105-0.179,0.178-0.373,0.225-0.568C79.855,45.897,81,43.549,81,40.933z   M49.994,11.235c2.164,0,3.928,1.762,3.928,3.93c0,2.165-1.764,3.929-3.928,3.929s-3.928-1.764-3.928-3.929  C46.066,12.997,47.83,11.235,49.994,11.235z M27.842,36.301c0.014,0,0.027,0,0.031,0c1.086,0,1.998-0.817,2.115-1.907  c0.762-7.592,5.641-13.791,12.303-16.535c1.119,3.184,4.146,5.475,7.703,5.475c3.561,0,6.588-2.293,7.707-5.48  c6.664,2.742,11.547,8.944,12.312,16.54c0.115,1.092,1.037,1.929,2.143,1.907c2.541,0.013,4.604,2.087,4.604,4.631  c0,1.684-0.914,3.148-2.266,3.958H25.508c-1.354-0.809-2.268-2.273-2.268-3.958C23.24,38.389,25.303,36.316,27.842,36.301z   M50.01,86.723L27.73,49.13h44.541L50.01,86.723z"/>
  </symbol>
</svg>

LocalIcon.css LocalIcon.css

.icon {
    width: 100px;
    height: 125px;
}

use.ic-1 {
    fill: skyblue;
}
use.ic-2 {
    fill: #FDC646;
}

svg path {
    fill: inherit;
}

LocalIcon.js本地图标.js

import React, { FC } from 'react';
import Icons from '../../icons/ic.svg';
import './LocalIcon.css';

const LocalIcon = ({ name, color, size }) => (
  // <svg fill={color} width={size} height={size}>
  //   <use className="icon" xlinkHref={`${Icons}#Icon/Heart/Filled`}/>
  // </svg>
  <>
    <svg>
      <symbol id="ic">
        <path fill="#000" d="M81,40..."/>
      </symbol>
    </svg>
    <svg className='icon' viewBox='0 0 100 125'>
      <use className='ic-1' href={`${Icons}#ic`} x='0' y='0' /> // This one is still black
    </svg>
    <svg className='icon' viewBox='0 0 100 125'>
      <use className='ic-2' href='#ic' x='0' y='0' />   // This one works
    </svg>
  </>
);

Does someone have an idea about what am I doing wrong?有人知道我做错了什么吗? Is that somehow related to the way I import the file?这是否与我导入文件的方式有关? Thanks a lot for your help.非常感谢你的帮助。

In order for your CSS rule to apply, you need to change any fill rule in your SVG code to fill:'inherit' .为了应用您的 CSS 规则,您需要更改 SVG 代码中的任何fill规则以fill:'inherit' Otherwise, the SVG style will override your CSS style.否则,SVG 样式将覆盖您的 CSS 样式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM