Sub topic 14
When to use base components and when to build your own
This chapter assumes you can read a Lightning web component (template, JavaScript class, CSS file). It also assumes you have used lightning-input, SLDS utility classes and the grid from earlier chapters. Here you will learn how to choose among three options for any piece of UI.
The three options
Every piece of interface falls into one of three groups.
- Base component. A
lightning-*element such aslightning-datatable. Salesforce maintains its markup, styling, keyboard behavior and accessibility. - SLDS-styled custom element. Plain HTML (
div,ul,button) with SLDS classes applied. You write the markup and behavior. SLDS supplies the look. - Fully custom component. Your own markup and your own CSS. It may follow SLDS visually or not at all.
Start with a base component
Reach for a base component first. You get these benefits without writing code:
- Built-in accessibility, such as labels, focus handling and ARIA attributes.
- Consistent appearance across Lightning Experience and Experience Cloud, including theme changes.
- Built-in validation, formatting and locale handling.
lightning-inputwithtype="date"is a good example. - Upgrades from Salesforce. Your component improves when the base component does.
A base component is usually the right choice when it already does what you need, even if you must configure it with several attributes.
Drop down a level when the base component does not fit
Move to an SLDS-styled custom element when the base component cannot produce the structure you need, but the look should still match Salesforce. Typical cases are a custom list item layout, a timeline row, or a badge arrangement with specific content.
The cost is that you now own accessibility. If you build a clickable element, use a real button or a, not a div with a click handler.
<template>
<ul class="slds-has-dividers_bottom-space">
<template for:each={tasks} for:item="task">
<li key={task.id} class="slds-item slds-var-p-around_small">
<span class="slds-text-title_bold">{task.title}</span>
<span class="slds-badge slds-var-m-left_small">{task.status}</span>
</li>
</template>
</ul>
</template>Choose a fully custom component only when neither option works. Examples are a canvas chart, a drag-and-drop board, or a brand-specific design that SLDS cannot express. Expect to spend the most effort here on keyboard support, screen reader behavior and testing.
Decision flow
The middle branch matters. If the only problem is color or spacing, try [styling hooks](#) before abandoning the base component. Hooks keep the component's behavior and accessibility.
Comparing the options
| Factor | Base component | SLDS-styled element | Fully custom |
|---|---|---|---|
| Effort | Lowest | Medium | Highest |
| Accessibility | Provided | You maintain it | You maintain it |
| Visual flexibility | Limited to attributes and hooks | High within SLDS | Unlimited |
| Upgrade risk | Low | Medium, class names can change | You own all changes |
| Testing | Stub in Jest | Test the DOM directly | Test everything |
Common mistakes
- Rebuilding a feature that a base component already has, such as writing your own table when
lightning-datatablefits. - Overriding the internals of a base component with CSS. Shadow DOM blocks this, and it breaks on upgrades.
- Using SLDS classes that you copied from old documentation without checking that they still exist.
- Treating a
divwithonclickas a button.
Exercise
Exercise
Your team needs a list of cases where each row shows a title, a colored status badge and a "Escalate" action that only appears on high-priority rows. No base component produces this row layout. Which option do you pick, and what must you pay attention to?
Show answer
Use an SLDS-styled custom element. The row layout is custom, but the look should match Salesforce, so build it with SLDS list and badge classes. Because you write the markup, you are responsible for accessibility. Use a real lightning-button or button for the Escalate action, make sure it is reachable with the keyboard, and give the badge text that does not depend on color alone.