Sub topic 14
When to use base components and when to build your own
This chapter assumes you can read an LWC (HTML template, JavaScript class, CSS file). It also assumes you have met the base components, the SLDS utility classes and the grid, and that you know about styling hooks and shadow DOM scoping. Here you will turn that knowledge into a decision process. For each piece of UI, you pick one of three options: a base component, an SLDS-styled custom element, or a fully custom component.
The three options
Every piece of UI you build falls into one of these categories.
| Option | What it is | Example |
|---|---|---|
| Base component | A `lightning-*` element that Salesforce ships and maintains | `lightning-datatable`, `lightning-input`, `lightning-record-edit-form` |
| SLDS-styled custom element | Your own markup (`div`, `ul`, `section`) that uses SLDS CSS classes | A card-like panel built from `slds-card` classes, a custom list with `slds-media` |
| Fully custom component | Your own markup and your own CSS, with no dependence on SLDS | A canvas chart, a drag-and-drop board, a brand-specific widget |
The options sit on a spectrum. You give up Salesforce-maintained behavior as you move from the first to the third, and you gain control over markup and appearance.
What base components give you
Base components do work that is easy to underestimate.
- Behavior.
lightning-inputvalidatesrequired,min,maxandpatternfor you.lightning-datatablehandles sorting events, row selection, inline editing and keyboard navigation. - Accessibility. Labels, ARIA attributes, focus handling and keyboard support are built in. You still have to supply the label text.
- Consistency. The components follow SLDS, so they look like the rest of Lightning Experience and pick up theme and density changes.
- Platform integration.
lightning-record-edit-formandlightning-input-fielduse Lightning Data Service. They respect field-level security, show the right input type for each field, and surface server errors throughlightning-messages. - Maintenance. When Salesforce updates SLDS or fixes a browser bug, your component benefits without a code change.
The record form from earlier in this tutorial shows the pay-off:
<template>
<lightning-record-edit-form object-api-name="Contact" onsuccess={handleSuccess}>
<lightning-messages></lightning-messages>
<lightning-input-field field-name="FirstName"></lightning-input-field>
<lightning-input-field field-name="LastName"></lightning-input-field>
<lightning-button
class="slds-m-top_small"
type="submit"
variant="brand"
label="Save"
></lightning-button>
</lightning-record-edit-form>
</template>There is no JavaScript here for field metadata, validation, saving or error display. Reproducing that with plain inputs would take far more code, and you would own every bug in it.
What base components cost you
Base components also have limits. Know them before you commit to one.
- Fixed markup. You cannot change the internal HTML. If the design needs a different arrangement of the parts, the component may not allow it.
- Limited styling. Shadow DOM stops your CSS from reaching inside the component. You can use utility classes on the host element, and you can use styling hooks where the component exposes them. Anything else is out of reach.
- Fixed feature set.
lightning-datatablesupports a defined list of column types. Custom cell content needs extra work, such as extending the datatable with a custom type. - Platform dependence. Base components work in Lightning Experience, Experience Cloud and Lightning Out, but they depend on the platform. Some behave differently outside Salesforce, and Jest tests need stubs for them.
- Performance at scale. A datatable with thousands of rows, or hundreds of inputs on one page, can be slow. Each base component is a full web component with its own internals.
The styling limit is the one people meet most often. This is the sanctioned way to change a button:
lightning-button {
--slds-c-button-brand-color-background: #0b5cab;
}A styling hook works only if the component documents it. If the hook you need does not exist, you cannot override the internals from outside. Do not reach for hacks that depend on the internal DOM, because they can break after any release.
SLDS-styled custom elements: the middle path
Sometimes no base component matches the structure you need, but you still want the Salesforce look. In that case, write your own markup and apply SLDS classes.
<template>
<article class="slds-card">
<div class="slds-card__header slds-grid">
<h2 class="slds-card__header-title">Recent contacts</h2>
</div>
<div class="slds-card__body slds-card__body_inner">
<ul class="slds-has-dividers_bottom-space">
<template for:each={contacts} for:item="contact">
<li key={contact.id} class="slds-item">
<span class="slds-text-title_bold">{contact.name}</span>
<span class="slds-text-body_small slds-m-left_small">{contact.email}</span>
</li>
</template>
</ul>
</div>
</article>
</template>You control the markup, so the layout can be anything. SLDS classes keep the visual style consistent. The trade-off is that you are now responsible for accessibility and behavior. A div with an onclick handler is not a button. If you build interactive controls yourself, you must supply roles, keyboard handling, focus order and labels.
SLDS class names also follow the BEM convention (block__element_modifier). Check the SLDS documentation for the markup each blueprint expects, because a class used with the wrong structure gives broken styling.
Fully custom components
Choose a fully custom component when neither base components nor SLDS blueprints describe what you need. Typical cases:
- Visualizations, such as charts drawn on a canvas or with SVG.
- Highly interactive widgets, such as a kanban board with drag and drop.
- Branded UI that must not follow the Salesforce theme, for example on an Experience Cloud site with a strict design system.
- Code that must work outside the Salesforce platform.
Even then, you can often use base components for the parts around the custom piece, such as lightning-button for the toolbar and lightning-card for the frame. A custom component does not have to be custom all the way down.
A decision process
Work through these questions in order and stop at the first one that settles the choice.
- Does a base component do the job? Check the Component Library. If one matches the behavior you need, start with it.
- Can styling hooks and utility classes cover the visual differences? If yes, keep the base component.
- Is the gap only in layout or presentation? Use SLDS classes on your own markup.
- Is the gap in behavior or structure that SLDS does not describe? Build a custom component, and use base components inside it where they fit.
Trade-offs to weigh
The flow gives a default answer. These factors can change it.
| Factor | Favors base components | Favors custom |
|---|---|---|
| Time to deliver | Less code to write | Only if the base component needs workarounds |
| Accessibility | Built in | You must build and test it |
| Look and feel | Matches Lightning Experience | Full control |
| Long-term upkeep | Salesforce maintains it | You maintain it |
| Data volume | Fine for typical sizes | Better for very large or virtualized lists |
| Reuse outside Salesforce | Not possible | Possible |
Common mistakes
- Rebuilding what exists. Writing a custom date picker or modal from
divelements spends time and loses accessibility. Check the Component Library first. - Fighting the shadow DOM. If you need many CSS workarounds to restyle a base component, the choice was probably wrong. Move to the next option instead.
- Mixing patterns in one form. Combining
lightning-inputand hand-builtinputelements in the same form leads to inconsistent validation messages and spacing. - Dropping labels in custom controls. Custom inputs need a visible label or an
aria-label, as base components require. - Choosing custom for a small visual difference. A different button color is a styling hook, not a new component.
Example: a contact list screen
Suppose a screen needs a search box, a table of contacts, and a "needs follow-up" badge with an unusual shape.
- The search box is
lightning-input type="search". It handles the label, clear icon and change event. - The table is
lightning-datatable. Sorting, selection and keyboard support come for free. - The page frame is
lightning-card, with alightning-buttonin the actions slot. - The badge has a custom shape that no hook can produce, so it is a small custom component with its own CSS, placed in a datatable column only if you add a custom column type. Otherwise show it beside the table in SLDS-styled markup.
Most of the screen uses base components. Only the badge is custom. That ratio is typical.
Exercise
A designer asks for a list where each row shows an avatar, a name, and a row of three small tags, laid out in a specific arrangement that lightning-datatable cannot produce. The page should still look like Lightning Experience. Which of the three options fits best, and why?
Show answer
An SLDS-styled custom element. The structure is the gap, so a base component does not fit, but the SLDS media object and utility classes describe this kind of layout and keep the Salesforce look. A fully custom component would give up the SLDS styling for no benefit. You must add the accessibility behavior yourself, for example list semantics and labels, because no base component supplies it.
Exercise
You use lightning-button variant="brand" and the product team wants a slightly different brand color. A developer proposes replacing the button with a custom button element styled from scratch. What do you recommend?
Show answer
Keep the base component and set the documented styling hook, for example --slds-c-button-brand-color-background, in the component's CSS. This changes the color without losing built-in accessibility and behavior, and without depending on internal markup. A custom button is justified only if the design needs something no hook can express.
Exercise
Name two things you become responsible for when you replace lightning-input with a hand-built input element.
Show answer
Any two of: the visible label and its association with the input, validation and error messages, keyboard and focus behavior, ARIA attributes, consistent styling with other fields, and ongoing maintenance when SLDS changes.