A FIELD GUIDE FOR BUILDERS
Salesforce LWC,
without the mystery.
A friendly, code-first guide to Lightning Web Components. Learn what they are, when to use them, and how HTML, CSS, and JavaScript come together to build Salesforce experiences.
Made for curious developers
and Salesforce newcomers.
LWC is a way to build custom, reusable interface pieces that run inside Salesforce.
↓FIRST, THE HUMAN VERSION
Think of a component
like a well-designed room.
A room has a layout, a look, and things that happen inside it. An LWC does too. It is a small, self-contained part of a Salesforce page—such as a contact card, search form, or task dashboard.
The useful part? You can build a component once, then reuse it wherever that experience belongs. Salesforce provides ready-made components, and you can combine them with components of your own.
HTML gives it structure.
Define what appears on screen: headings, fields, buttons, and Salesforce base components.
THE SKELETONCSS gives it a feel.
Set spacing, typography, and layout. LWC component styles are scoped to that component.
THE PERSONALITYJavaScript makes it work.
Handle clicks, manage state, filter data, and connect the interface to Salesforce.
THE ENGINESALESFORCE BASE COMPONENTS
Start with the building blocks.
These commonly used components save time and provide familiar Salesforce behavior. Search by name or by what you want to do.
Give the page a clear structure.
Use cards to group content, layouts to create responsive columns, and tabs or accordions to organize details.
<lightning-card title="Account overview">
<lightning-layout multiple-rows>
<lightning-layout-item
size="12" medium-device-size="6">
<p>Company details</p>
</lightning-layout-item>
</lightning-layout>
</lightning-card>
Collect information with less friction.
Choose an input for the data type. Give every field a label, and handle its change event in JavaScript.
<lightning-input
label="Contact name"
value={contactName}
onchange={handleNameChange}>
</lightning-input>
<lightning-combobox
label="Status"
value={status}
options={statusOptions}
onchange={handleStatusChange}>
</lightning-combobox>
Make the next step obvious.
Use button variants to communicate emphasis, then connect a click to a JavaScript handler.
<lightning-button
label="Save changes"
variant="brand"
onclick={handleSave}>
</lightning-button>
<lightning-button-icon
icon-name="utility:edit"
alternative-text="Edit record">
</lightning-button-icon>
Make records easier to scan.
Display rows with a datatable, show hierarchy with a tree grid, or format values for people—not machines.
<lightning-datatable
key-field="id"
data={contacts}
columns={columns}
hide-checkbox-column>
</lightning-datatable>
<lightning-formatted-number
value={revenue}
format-style="currency">
</lightning-formatted-number>
Don’t leave users guessing.
Show when work is happening. Explain unfamiliar fields. Confirm success—or clearly describe what went wrong.
<lightning-spinner
alternative-text="Loading contacts"
size="medium">
</lightning-spinner>
// In your component JavaScript:
this.dispatchEvent(new ShowToastEvent({
title: 'Saved',
message: 'Your changes are ready.',
variant: 'success'
}));
Help people understand where they are.
Breadcrumbs show a path through content. Salesforce navigation APIs can take users to records and pages.
<lightning-breadcrumbs>
<lightning-breadcrumb
label="Accounts"
href="/lightning/o/Account/list">
</lightning-breadcrumb>
<lightning-breadcrumb label="Northstar">
</lightning-breadcrumb>
</lightning-breadcrumbs>
Make a building block that’s yours.
A parent can pass information down to a child. A child can announce an event back up. That’s how components work together.
<!-- Parent template -->
<c-contact-card
contact={selectedContact}
onselect={handleSelect}>
</c-contact-card>
// Child JavaScript:
this.dispatchEvent(new CustomEvent('select', {
detail: this.contact.Id
}));
Connect the interface to real records.
Use Lightning Data Service and wire adapters for supported Salesforce data. Use Apex when your use case needs server-side logic.
<lightning-record-view-form
record-id={recordId}
object-api-name="Account">
<lightning-output-field
field-name="Name">
</lightning-output-field>
<lightning-output-field
field-name="Industry">
</lightning-output-field>
</lightning-record-view-form>
No matching examples
Try a component name like “button” or a task like “navigation”.
Looking for every available component? Salesforce’s catalog is extensive and evolves with releases. Use the official Lightning Component Reference for the complete, current list and supported attributes.
HOW AN LWC IS PUT TOGETHER
One component.
Three files with clear jobs.
An LWC usually lives in its own folder. The folder name becomes the component’s name, and the files inside define how it renders, behaves, and becomes available in Salesforce.
THE IMPORTANT IDEA
Each file has one job.
Keep the template focused on what users see. Keep JavaScript focused on state and behavior. Keep the component stylesheet focused on its own appearance.
Clearer to understand
Another developer can find the right place to make a change.
Easier to reuse
A component can be placed in supported Salesforce pages.
Safer styling
Component CSS is scoped, so it is less likely to affect unrelated page content.
Change state. Update the view.
When a component’s tracked values change, LWC can render the updated template. Use getters to prepare display values.
get hasContacts() {
return this.contacts.length > 0;
}
Pass data down. Send events up.
Public properties let a parent configure a child. Custom events let a child report something the parent should handle.
@api contact;
this.dispatchEvent(
new CustomEvent('select', {
detail: this.contact.Id
})
);
Style your component, not the app.
Use local class names, design tokens, and supported styling hooks. Avoid depending on the internal markup of base components.
:host {
display: block;
--accent: #0b8278;
}
.contact-card {
border-radius: 1rem;
}
A SIMPLE WORKING SHAPE
Meet the helloCard.
This small example has a title, a message, and a button. It demonstrates a template, component state, an event handler, scoped CSS, and page configuration.
Where does the component go? In Salesforce, the metadata file controls where the component can be used. Add supported targets, deploy the component to your org, then place it on a Lightning page with Lightning App Builder.
THE POLISH IS IN THE DETAILS
Good components are
thoughtful components.
A professional interface does more than look polished. It helps people understand what is happening and what to do next.
Design every state
Plan for loading, empty results, success, and errors—not just the happy path.
Respect accessibility
Use labels, meaningful alternative text, keyboard-friendly controls, and clear feedback.
Use the right data tool
Choose Lightning Data Service or wire adapters when they fit; use Apex for server-side logic when needed.
Keep permissions in mind
Client-side code is not a security boundary. Protect records and actions with Salesforce permissions and secure server-side patterns.
Check the small screen
Make sure layouts, tables, and actions remain usable at narrow widths and with keyboard navigation.
THE TAKEAWAY
Start small.
Make it useful.
Pick one user problem. Compose Salesforce base components where they fit. Add your own HTML, scoped CSS, and JavaScript where your experience needs something more. That’s the heart of building with LWC.
Back to the beginning
Comments
Post a Comment