Sub topic 9
The SVG icon
This chapter covers the optional SVG file in a Lightning web component bundle. It assumes you know the bundle layout from the earlier chapters, including the naming rules and the js-meta.xml configuration file. It also assumes you can read basic SVG markup, meaning shapes, viewBox, and fill.
What the icon file does
A bundle can contain one SVG file. Salesforce uses it as the component's icon in the builder tools where an administrator picks components, such as the component list in Lightning App Builder. It identifies the component in a list of many components. It has no effect on what the component renders for end users.
The icon is optional. If the bundle has no SVG file, Salesforce shows a default icon. A component that is never exposed to a builder tool doesn't need an icon at all.
The icon is also separate from the component's own markup. You can't reference this file from your template with an <img> tag. Salesforce reads it at the bundle level, as metadata about the component.
Where the file goes and what it is called
The SVG sits in the bundle folder next to the other files. Its base name must match the folder name, as with every other file in the bundle.
force-app/main/default/lwc/
└── greetingCard/
├── greetingCard.html
├── greetingCard.js
├── greetingCard.css
├── greetingCard.js-meta.xml
└── greetingCard.svgThree rules apply:
- One icon per bundle. The file is
<componentName>.svg, and there is no way to attach a second one. - Exact name match.
greetingCard.svgbelongs to thegreetingCardfolder. A file namedgreeting-card.svgorGreetingCard.svgis not the component's icon. The name is case-sensitive in the same way the folder name is. - No reference in the configuration file.
greetingCard.js-meta.xmldoes not mention the SVG. Salesforce finds it through the naming convention alone. This is why a misspelled name fails silently. Nothing in the XML points at the file, so nothing reports that it's missing.
A minimal icon
An icon is ordinary SVG markup. Here is a small, self-contained example that draws a rounded square with a white circle in it.
<?xml version="1.0" encoding="UTF-8"?>
<svg xmlns="http://www.w3.org/2000/svg" width="120" height="120" viewBox="0 0 120 120">
<rect width="120" height="120" rx="12" fill="#032d60" />
<circle cx="60" cy="60" r="28" fill="#ffffff" />
</svg>A few practical points about this file:
- Declare the namespace. The
xmlns="http://www.w3.org/2000/svg"attribute is required for the file to be valid SVG. - Use a
viewBox. The builder scales the icon into a small slot. AviewBoxlets the drawing scale cleanly. A 120 by 120 square is a common choice. - Keep it flat and static. Use plain shapes and paths with solid fills. Don't rely on scripts, external images, external fonts, or linked stylesheets. Everything the icon needs should be in the one file.
- Make it readable when small. The icon is displayed at a small size, so fine detail disappears. Use bold shapes and two or three colors.
How Salesforce picks the icon
The flow is simple, and the diagram below shows it.
The component must be exposed before any icon matters. Exposure is controlled by isExposed in the configuration file, which you saw in the earlier chapter.
<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>60.0</apiVersion>
<isExposed>true</isExposed>
<masterLabel>Greeting Card</masterLabel>
<targets>
<target>lightning__AppPage</target>
</targets>
</LightningComponentBundle>With this configuration and a matching greetingCard.svg, an administrator editing a Lightning page sees "Greeting Card" in the component list with your icon beside it. If isExposed is false, the component doesn't appear in the list, so its icon is never shown.
Checking the name before you deploy
Because a wrong name fails silently, a check is worth having. The script below takes a bundle folder and reports whether it contains a correctly named SVG. You can run it locally or in a pre-commit hook.
const fs = require('fs');
const path = require('path');
const bundleDir = process.argv[2];
if (!bundleDir) {
console.error('Usage: node checkIcon.js <path-to-bundle-folder>');
process.exit(2);
}
const componentName = path.basename(path.resolve(bundleDir));
const svgFiles = fs.readdirSync(bundleDir).filter((file) => file.endsWith('.svg'));
const expected = `${componentName}.svg`;
if (svgFiles.length === 0) {
console.log(`No icon in ${componentName}. The default icon will be used.`);
} else if (svgFiles.length === 1 && svgFiles[0] === expected) {
console.log(`OK: ${expected}`);
} else {
console.error(`Problem in ${componentName}: found ${svgFiles.join(', ')}, expected only ${expected}`);
process.exit(1);
}Run it with node checkIcon.js force-app/main/default/lwc/greetingCard. It exits with code 1 when the name is wrong or when there is more than one SVG, so a build step can fail on it.
Common mistakes
| Symptom | Likely cause | Fix |
|---|---|---|
| Component shows the default icon | The SVG name doesn't match the folder name, including case | Rename the file to `<componentName>.svg` |
| Component is missing from the builder list | `isExposed` is `false`, or no `targets` match the page type | Fix the configuration file. The icon is not the cause. |
| Icon looks blurry or cut off | No `viewBox`, or a very detailed drawing | Add `viewBox` and simplify the shapes |
| Icon doesn't render at all | Missing `xmlns` or malformed XML | Validate the file by opening it in a browser |
| You want the image inside the component's UI | The bundle icon is not available to the template | Use a static resource, or `lightning-icon` for SLDS icons |
The last row confuses many developers. If you need to show an image to the end user, the bundle SVG is the wrong tool. Upload the image as a static resource, or use lightning-icon with an icon name from the Salesforce Lightning Design System.
Version control and deployment
The SVG deploys with the rest of the bundle, because it lives in the same folder. Commit it with the other files. When you retrieve a component from an org with the Salesforce CLI, an existing icon comes down into the bundle folder along with the HTML, JavaScript, and configuration files.
Two habits keep this tidy:
- Don't rename a component folder without renaming the SVG in the same change. A rename that leaves the SVG behind quietly drops the icon.
- Keep the file small. An icon is a few hundred bytes of markup. If you paste in a large export from a design tool, remove the editor metadata and unused definitions first.
When to include one
Include an icon when administrators will place the component by hand. Examples are components exposed to App Builder pages, or components shared across many teams where a recognizable picture helps people find the right one. Skip the icon for components used only as children of other components, since they are never listed in a builder tool. Skip it for early prototypes too, because you can add the file later without changing any other file in the bundle.
Exercise
A bundle folder is named orderSummary and contains orderSummary.html, orderSummary.js, orderSummary.js-meta.xml and order-summary.svg. The component is exposed with isExposed set to true. An administrator opens Lightning App Builder and sees the component in the list, but with the default icon. Why, and what is the fix?
Show answer
The SVG name doesn't match the folder name, so Salesforce doesn't treat it as the component's icon. The configuration file doesn't reference the SVG, so nothing reports the mismatch. Rename order-summary.svg to orderSummary.svg and deploy again. The component appears in the list because it is exposed. The icon is a separate matter.
Exercise
You want to show a company logo inside the component's rendered output. Should you put it in the bundle's SVG file? Explain.
Show answer
No. The bundle SVG is used only as the component's icon in builder tools, and the template can't use it as an image. Store the logo as a static resource and reference it from the component, or use lightning-icon if an SLDS icon fits.