Trying to show custom icons from my static resource zip file using lightning:icon
within a community, but it's not rendering the icons. It works for img tag, but I need to use fill
to change the colour of the icons from css.
Sample Code:
<lightning:icon src="{!$Resource.StaticResource+'/y.svg'}"/> <!--not working-->
<img src="{!$Resource.StaticResource+'/y.png'}"/> <!--works, but can't modify colour using fill css attribute-->
Not sure if using $Resource
is the right approach but according to sf docs, I should be able to use it to reference path for a static resource asset https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/expr_resource_value_provider.htm
Also saw the documentation around showing custom icons in lightning:icon which shows that you need to specify the path to your icon, but I don't know what path to use for my static resource. https://developer.salesforce.com/docs/component-library/bundle/lightning:icon/documentation
<lightning:icon src="path/to/customSvgSprite.svg#my-icon"/>
found some old posts around using custom icons in lightning component, but they are already outdated:
Best Answer
I had the same issue and ask in Trailblazer Community here.
I got the answer from Greg Rewis: "You need to have an id on the
<g>
element of the SVG. Then, simply concatenate the name together like this:<lightning:icon src="{! $Resource.sign + '#custIcon'}" alternativeText="My Custom Icon" />
."To do that, you need to open your svg file with a text editor, and you'll see something like this:
Just add an id to the
<g>
tag, something like:And then add it to the name of your resource:
Doc is supposed to be updated but you can add your voice to the thread :)
Also, I wasn't able to set the proper size to my svg as you'll see in the post, I'll be interested if you know how to do it :)