Sub topic 9
The SVG icon
This chapter assumes you know the bundle layout and naming rules from the earlier chapters, and that you can read a js-meta.xml file. You also need basic familiarity with SVG, the XML-based vector image format.
What the icon does
The SVG icon is an optional file in the bundle. It sets the picture next to your component's name in the component list of Lightning App Builder. Experience Builder also uses it. Without the file, Salesforce shows a default icon.
The icon is only for builder tools. It is never rendered inside your component, and users of the finished page never see it.
Naming and placement
The file follows the same rule as every other file in the bundle. Its base name matches the folder name, and its extension is .svg.
force-app/main/default/lwc/
└── greetingCard/
├── greetingCard.html
├── greetingCard.js
├── greetingCard.js-meta.xml
└── greetingCard.svgA bundle can hold only one icon. A file such as greetingCard-dark.svg is not picked up as an alternative.
Writing the file
An icon file is an ordinary SVG document. This example draws a white circle on a dark rounded square.
<?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="30" fill="#ffffff"/>
</svg>Three habits keep the icon reliable:
- Declare the
xmlnsnamespace on the rootsvgelement. Without it, some renderers treat the file as invalid. - Set a
viewBoxso the image scales cleanly. The builder shows the icon at a small size. - Use simple shapes and solid fills. Avoid scripts, external links, and embedded fonts, because the file is only a small picture.
How the icon is connected
Nothing in your code refers to the icon. You do not list it in greetingCard.js-meta.xml or import it in JavaScript. Salesforce finds it by name when you deploy the bundle.
The isExposed setting still matters. If it is false, the component does not appear in the builder at all, so nobody sees the icon.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Default icon appears | The file name does not match the folder, for example `greeting-card.svg` | Rename it to `greetingCard.svg` |
| Default icon appears | The SVG is malformed or missing the namespace | Open it in a browser and fix any errors |
| Icon is blurry or cropped | No `viewBox` | Add a `viewBox` matching the drawing size |
| No icon and no component | `isExposed` is `false`, or no targets are set | Fix `greetingCard.js-meta.xml` |
| Old icon still shows | The page was cached, or the change was not deployed | Deploy again and hard-refresh the builder |
The first two rows are the most common. A name mismatch gives no error. Salesforce silently falls back to the default icon.
When to include one
Add an icon when people other than you will drag the component onto pages. A distinct picture makes it easier to find in a long list of components. Skip it for components that are only used as children of other components, since those never appear in a builder. You can add the file later without changing any other file in the bundle.
Exercise
You add greeting-card.svg to the greetingCard bundle, deploy it, and the component still shows the default icon in Lightning App Builder. There is no deployment error. What is wrong, and what do you change?
Show answer
The file's base name does not match the bundle folder name. The folder is greetingCard, so the icon must be named greetingCard.svg. Salesforce does not report an error and falls back to the default icon. Rename the file, deploy again, and refresh the builder. Also check that isExposed is true in greetingCard.js-meta.xml, because the component must be visible in the builder for the icon to appear.