react-svg icon indicating copy to clipboard operation
react-svg copied to clipboard

How to use react-svg for images used in CSS background?

Open raghav1086 opened this issue 8 years ago • 8 comments

I am using few svg image icons as a background image and on hover it should change to different color. Right now I am using hover image for hover effect which is little bit slow to load on mouseover and not very smooth looks jerky. I am looking for a way if I could use svg fill property in css to change the svg color on hover rather having hover image.

raghav1086 avatar Feb 09 '18 08:02 raghav1086

Hey @raghav1086, the API exposes a wrapperClassName prop which you can then use to target the innards of your SVG with custom styles.

Using the example in this repo which sets wrapperClassName="wrapper-class-name", I could add the following style somewhere to change the fill of a part of the SVG on hover:

.wrapper-class-name:hover g {
  fill: black;
}

Does that answer your question?

tanem avatar Feb 10 '18 17:02 tanem

@tanem I got your point we can easily give hover effect if element is in direct DOM but my question was for images used as background using css like this...

<input type="button" name="" value="Click me" class="btn" />
.btn {
    background: url('./icon.svg') left center no-repeat;
    background-size: 16px 16px;
    border: none;
    font-size: 14px;
    height: 20px;
    padding-left: 20px;
}
.btn:hover {
    background: url('./icon-hover.svg') left center no-repeat;
    background-size: 16px 16px;
}

raghav1086 avatar Feb 12 '18 02:02 raghav1086

@raghav1086 I'm having trouble understanding your question exactly: Is it related to this library, or is it a general CSS question?

If you're looking to replicate what you just posted using this library, then you could just re-render this component with the new SVG path when it's appropriate.

If it's a CSS question I'll close this issue as it's not related to this library, but I did do some digging for you and it might be appropriate in your situation to use one of the methods outlined here.

tanem avatar Feb 12 '18 21:02 tanem

@tanem My bad for bad explanation but you got it correct. I am looking to replicate above code using this library but not able to do it. Could you please show me example how I can use this library for scenarios I have posted above. It will be a great help.

raghav1086 avatar Feb 13 '18 02:02 raghav1086

Hey @raghav1086, no worries, I'll have a go at a couple more examples soon once my day job quietens down a little 😉

tanem avatar Feb 14 '18 09:02 tanem

@raghav1086 I've added to working examples of what I'm talking about to an issue-49 branch. I'll merge this stuff to master at some point.

To view the examples on that branch, run npm start, then point a browser at either http://localhost:8080/issue-49/fill (changing fill via CSS) or http://localhost:8080/issue-49/swap (swapping the SVG out as per your earlier example).

As per your original issue description though, and my earlier example, I'd recommend just trying to change the fill colour via CSS as opposed to swapping different SVGs in and out.

tanem avatar Feb 15 '18 17:02 tanem

@tanem Thanks very much for your time. It is a great helps 👍 please close this issue.

raghav1086 avatar Feb 16 '18 08:02 raghav1086

No problem @raghav1086 👌

tanem avatar Feb 17 '18 16:02 tanem