Change the result icons in a Lightning component
Change the result icons in a Lightning component
When developing a search interface in your Experience Cloud site, you will sometimes want to change the icons of your result templates.
To do so outside of Lightning, you can follow the steps for using custom icons in result templates.
However, when using a Lightning component, you need to follow certain leading practices to ensure that your icons are always displayed correctly.
Step 1: Create and upload your CSS rule to Salesforce
The first step you need to do is to create a CSS rule in a separate file, and upload it to Salesforce. You will also usually want to upload the image of your icon alongside your CSS rule.
-
Following the Coveo JavaScript Search Framework guidelines, create a CSS rule for your icon.
-
Ensure that the
background-image
URL references your image relative to your CSS file. -
Save the CSS class as a
.css
file. -
Create a .zip of your files, and upload it to Salesforce (see Defining Static Resources).
Step 2: Reference your class in your component
Now that you have your static resource in Salesforce, you need to add it to your custom Lightning component.
-
Ensure that you’ve created a custom Lightning component that wraps the Coveo components (see Integrating the Coveo components in a custom Lightning component).
-
In the Salesforce Developer Console, open your custom Lightning component.
-
In the
.cmp
file, add the following line:<ltng:require styles="{!$Resource.<MY_CUSTOM_ICON> + '<MY_CSS_RULE_PATH>'}" />
Where you replace:
-
<MY_CUSTOM_ICON>
by the name of the static resource you uploaded. -
<MY_CSS_RULE_PATH
by the path to your CSS rule inside the static resource (for example,/cssClasses/MyCssRule.css
).
-
-
Save your
.cmp
file.
Step 3: Modify your result template
Your CSS rule should now be loaded alongside your Lightning component, which means you’re now ready to reference it in your result template to change the icon.
-
In the Experience Builder of your community, access the Interface Editor of your search component.
-
In the Interface Editor, either select an existing result template you want to modify, or create a new one.
-
In the Layout of your result template (see Organize components within a search result template grid), select the Icon component, and click its Edit icon to edit its options.
-
Under Value, enter the CSS class name you entered in your
.css
file. -
Save your result template, and then save your search page.
In the Experience Builder, you should now see the new icon for your result templates.
While in the Interface Editor, you will most likely not be able to see your new icon. This is because your static resource isn’t loaded while in the Interface Editor.