Standard Salesforce record page layouts do not always fit bespoke business requirements, such as custom search bars, multi-step wizards, or interactive calculation panels. Building custom form elements in Lightning Web Components (LWC) with integrated icons enhances visual hierarchy, speeds up data entry, and maintains full alignment with the Salesforce Lightning Design System (SLDS).
1. Methods for Adding Icons to Form Controls
Salesforce developers have two primary ways to incorporate icons with inputs and outputs:
- Base Component Attributes (
lightning-input): Base components support native icon properties (such asicon-name="utility:search") for search or text inputs with zero custom CSS. - Custom SLDS Form Elements: Using standard SLDS markup classes (like
slds-input-has-iconandslds-input__icon) to place icons inside inputs or beside custom output badges.
- Base Icon Component:
<lightning-icon>with categories likeutility:search,standard:user, orcustom:custom14. - SLDS Layout Wrapper:
slds-form-element>slds-form-element__control>slds-input-has-icon. - Icon Sizing: Prefer
xx-smallorx-smallinside form inputs to match platform line heights. - Accessibility: Always provide an
alternative-textproperty on icons unless they are purely decorative alongside text.
2. Step-by-Step Implementation
Below is a working custom form component demonstrating an input field with an embedded search icon and a formatted output field displaying a user badge icon.
iconFieldsDemo.html)Combines a base
lightning-input with a custom SLDS output group.
<template>
<lightning-card title="Custom Form with Icons" icon-name="custom:custom18">
<div class="slds-p-around_medium">
<!-- Option A: Base Input with Embedded Search Icon -->
<div class="slds-m-bottom_medium">
<lightning-input
type="search"
label="Search Records"
placeholder="Enter keywords..."
value={searchQuery}
onchange={handleInputChange}>
</lightning-input>
</div>
<!-- Option B: Custom SLDS Input with Left-Aligned Icon -->
<div class="slds-form-element slds-m-bottom_medium">
<label class="slds-form-element__label" for="customEmailInput">Email Address</label>
<div class="slds-form-element__control slds-input-has-icon slds-input-has-icon_left">
<lightning-icon
icon-name="utility:email"
size="x-small"
class="slds-icon slds-input__icon slds-input__icon_left slds-icon-text-default"
alternative-text="Email">
</lightning-icon>
<input
type="email"
id="customEmailInput"
placeholder="user@example.com"
class="slds-input"
value={emailValue}
oninput={handleEmailChange} />
</div>
</div>
<!-- Option C: Custom Output Display with Leading Icon -->
<div class="slds-form-element slds-m-bottom_small">
<span class="slds-form-element__label">Assigned Owner</span>
<div class="slds-form-element__control slds-m-top_xx-small">
<div class="output-badge">
<lightning-icon
icon-name="standard:user"
size="small"
alternative-text="User Record">
</lightning-icon>
<span class="slds-m-left_x-small slds-text-body_regular">
<strong>{assignedUser}</strong>
</span>
</div>
</div>
</div>
</div>
</lightning-card>
</template>
iconFieldsDemo.js)
import { LightningElement, track } from 'lwc';
export default class IconFieldsDemo extends LightningElement {
searchQuery = '';
emailValue = '';
@track assignedUser = 'Alex Morgan';
handleInputChange(event) {
this.searchQuery = event.target.value;
}
handleEmailChange(event) {
this.emailValue = event.target.value;
}
}
iconFieldsDemo.css)
.output-badge {
display: inline-flex;
align-items: center;
background-color: #f3f3f3;
padding: 6px 12px;
border-radius: 4px;
border: 1px solid #dddbda;
}
iconFieldsDemo.js-meta.xml)
<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>60.0</apiVersion>
<isExposed>true</isExposed>
<targets>
<target>lightning__AppPage</target>
<target>lightning__RecordPage</target>
<target>lightning__HomePage</target>
</targets>
</LightningComponentBundle>
3. Common Traps & Platform Best Practices
.js-meta.xmlOlder tutorials often include invalid tags like
<target:importStaticResources> inside the component metadata XML. Lightning Design System icons and styles are available in LWC out of the box and do not require static resource imports. Adding unsupported XML tags will fail component deployment.
- Keep Icon Proportions Balanced: Use
size="x-small"orsize="xx-small"inside standard form fields so the icon does not stretch the line height. - Ensure Accessible Forms: Always pair HTML
<label>elements with matchingfor="..."andid="..."attributes so screen readers connect labels with inputs. - Color Consistency: Use SLDS utility classes such as
slds-icon-text-defaultorslds-icon-text-weakto ensure icon colors adapt automatically to Salesforce themes.
Summary
Integrating icons into custom Lightning Web Component inputs and outputs makes user interfaces more intuitive and visually engaging. By taking advantage of native base components and standard SLDS icon wrapper classes, developers can build polished, accessible custom screens that integrate seamlessly into any Salesforce Lightning experience.