material-ui icon indicating copy to clipboard operation
material-ui copied to clipboard

How to use a custom Icon

Open cbrake opened this issue 10 years ago • 17 comments

Hi,

Trying to figure out a way to use a custom icon in material-ui. I have the custom icon in a SVG file. Does it need processed into a sprite before I can use it in:

<SvgIcon {...this.props}>
        <path d="?????"></path>
</SvgIcon>

If so, what is the best way to make this happen? Or, can I simply put the SVG in the public dir, and reference it directly?

Thanks, Cliff

cbrake avatar Jan 04 '16 15:01 cbrake

Hi, we've updated the docs. Let me know if your question still isn't answered.

mbrookes avatar Feb 02 '16 23:02 mbrookes

got it -- thanks!

cbrake avatar Feb 03 '16 15:02 cbrake

Would you please give a resource to do it? Can't seem to find it. Thanks!

dukuo avatar Nov 24 '17 17:11 dukuo

http://www.material-ui.com/#/components/svg-icon See Custom SVG Icon example code.

mbrookes avatar Nov 24 '17 17:11 mbrookes

And here for v1: https://material-ui.com/style/icons/

mbrookes avatar Nov 24 '17 17:11 mbrookes

I think it might be a good idea to include this use case in the SvgIcon docs:

[...]
import MyAwesomeCustomSVG from './Route/to/my/file.svg
[....]

render() {
 <SvgIcon>
    <img src={MyAwesomeCustomSVG} alt="" width="300" height="200" />
 </SvgIcon>
}

I seriously spent like two days trying to reach the optimal code combination to get custom SVGs going in react haha

dukuo avatar Nov 27 '17 00:11 dukuo

To any readers, @dukuo's answer doesn't seem possible now, and only <path /> works.

braco avatar Feb 03 '18 00:02 braco

For v1, what is the example that shows how to load content for SvgIcon from a .svg file? Should I just avoid using SvgIcon and use <img src={MyImportedSvgFile}/>.

glenne avatar Aug 16 '18 15:08 glenne

Good question @glenne I currently have the similar concern.

KKrisu avatar Sep 03 '18 14:09 KKrisu

@glenne @KKrisu - Hopefully this will help.

const Icon = (props) => ( <SvgIcon {...props} width="width" height=".height" viewBox="0 0 width height" All the 3 values are actually given in the SVG file. Just take from there.

<path
  d="..." - Please fill the value of d that is there in the SVG file.
/> 
Incase multiple paths are given in the SVG file just copy all <path ...>
);

const SvgIconExampleSimple = () => Icon Component -- Somehow not able to past the exact code.

export default SvgIconExampleSimple;

Also with the iconStyles you will be able to use the icon for different sizes. const iconStyles = { marginLeft: 24, width: 100, height: 100 };

nagarajay avatar Sep 30 '18 13:09 nagarajay

For any readers who are still wondering about how to use your own SVG you can simply ignore the SvgIcon component and use an img in place of it. Here is an example of a StepLabel using it.

<StepLabel icon={ <img src={require("images/milestones/bootstrap.svg")} alt="" width="50" height="50" /> } />

harrycollin avatar Mar 16 '19 17:03 harrycollin

what is StepLabel then?

Swainstha avatar Mar 24 '19 06:03 Swainstha

what is StepLabel then?

I've updated my original comment.

harrycollin avatar Mar 25 '19 01:03 harrycollin

Check my answer at StackOverflow How to use an SVG file in a SvgIcon in Material-UI.

I used an <Icon/> component and addes an <img/> element inside with the SVG file. Added height: 100% to the image and text-align: center to the root elelement of the <Icon/> and that did the trick:

Material UI Drawer Icon from SVG

clytras avatar Jun 02 '19 19:06 clytras

import { ReactComponent as Github } from "./images/github.svg";

// then
<Github />

in CRA projects

vvenv avatar Sep 25 '19 07:09 vvenv

<StepLabel icon={ <img src={require("images/milestones/bootstrap.svg").default} alt="" width="50" height="50" /> } /> For me, I need to add .default after the require syntax.

ZenStudioLab avatar Dec 13 '21 07:12 ZenStudioLab

import { ReactComponent as Github } from "./images/github.svg";

// then
<Github />

That solution does not work if the element must be used multiple times. Only one element will be rendered, the rest will be undefined.

The only consistent solution, to me anyway, is glenne's solution:

import someIconSrc from './path/to/assets/some-icon.svg';

<img src={someIconSrc} />

Where the SVG file is actually downloaded as a resource bundle. Since it is using the browser cache and there is no JS parsing, so this is, to me, the most optimal one.

yanickrochon avatar May 08 '23 20:05 yanickrochon