Sub topic 18
Standard features versus LWC additions
This chapter assumes you have read the earlier chapters on custom elements, shadow DOM, slots, ES modules, and the anatomy of an LWC bundle. You should be comfortable with JavaScript classes and DOM events. The goal is a practical sorting skill. For any line in a component, you should be able to say whether the browser provides it or the LWC compiler and framework provide it.
Why the distinction matters
LWC is built on web standards, but it is not only a thin wrapper around them. Three practical consequences follow from knowing where the line sits:
- Portability. Standard code works in any browser and in any web component. LWC additions work only where the LWC compiler and engine are present.
- Debugging. When something behaves oddly, you need to know whether to read MDN or the LWC documentation.
- Longevity. Features that start as LWC additions can later be replaced by standards, as happened with module loading and shadow DOM. Code that leans on standards ages better.
The sorting table
| Feature | Standard or LWC addition | Notes |
|---|---|---|
| `import` / `export default` | Standard (ES modules) | The compiler resolves special specifiers such as `lwc` and `@salesforce/...`. |
| Class that becomes an HTML tag | Standard (custom elements) | LWC registers the element for you. You never call `customElements.define`. |
| `LightningElement` base class | LWC addition | It extends `HTMLElement` and adds lifecycle, `this.template`, and `@api` support. |
| Shadow root | Standard, with a caveat | Salesforce has used a synthetic shadow polyfill. Native shadow DOM is available in the open-source engine. |
| `<template>` as the root of the HTML file | Looks standard, is an LWC addition | The compiler consumes it. It is not an inert HTML `template` element at runtime. |
| `{name}` expressions | LWC addition | Compiled into JavaScript. |
| `onchange={handleChange}` | LWC syntax over a standard API | Compiles to a call to `addEventListener`. |
| `for:each`, `if:true`, `lwc:if` | LWC addition | Compile-time template directives. |
| Reactive class fields | LWC addition | The framework observes reassignment. |
| `@api`, `@wire`, `@track` | LWC addition | Decorators processed by the compiler. |
| `CustomEvent`, `dispatchEvent` | Standard | Used exactly as in plain JavaScript. |
| `connectedCallback`, `disconnectedCallback` | Standard names | Called by the LWC engine, matching the custom element callbacks. |
| `renderedCallback`, `errorCallback` | LWC addition | No browser equivalent. |
| `slot` | Standard concept | Works with the shadow DOM slot model. |
| `lightning-*` base components | Salesforce library | Built with LWC but not part of the framework core. |
What the compiler does
The compiler turns each bundle into plain JavaScript. The HTML template becomes a function that produces virtual DOM nodes. Decorators are replaced with registration code. Template syntax such as {name} becomes property reads on the component. The browser never sees for:each or @api. It only sees the compiled module.
The module on the right is standard JavaScript. The engine is a runtime library that creates the element and keeps the rendered output in sync with your fields.
A component with both kinds of code
The following class mixes standard and framework features. The comments mark which is which.
import { LightningElement, api } from 'lwc';
export default class StatusBadge extends LightningElement {
// LWC addition: @api makes this a public property
@api status = 'idle';
// LWC addition: a plain field is reactive when reassigned
clicks = 0;
// Standard: a plain arrow function stored as a field
handleHostClick = () => {
this.clicks += 1;
// Standard: CustomEvent and dispatchEvent
this.dispatchEvent(
new CustomEvent('badgeclick', { detail: { clicks: this.clicks } })
);
};
// Standard callback name, invoked by the LWC engine
connectedCallback() {
// Standard: addEventListener on the host element
this.addEventListener('click', this.handleHostClick);
}
// Standard callback name
disconnectedCallback() {
this.removeEventListener('click', this.handleHostClick);
}
// LWC addition: renderedCallback has no browser equivalent
renderedCallback() {
// LWC addition: this.template is the component's shadow root
const label = this.template.querySelector('span');
if (label) {
// Standard DOM API on the returned element
label.setAttribute('data-status', this.status);
}
}
}<!-- statusBadge.html -->
<template>
<span>{status} ({clicks})</span>
</template>Two details are worth noticing. First, querySelector on this.template is the standard shadow root method, but you reach the root through the LWC accessor. Use this.template, not this.shadowRoot. Second, the native element methods are all still available because LightningElement extends HTMLElement.
Where beginners confuse the two
Events are standard, so their rules are standard
A CustomEvent created with no options does not bubble and is not composed. This is browser behavior, not an LWC rule. A parent can listen to a direct child with oncountchange={handler}, but an event dispatched deeper in the tree will not reach an ancestor unless you set bubbles and composed. LWC documentation advises caution with these options. Setting them widens the event's reach and makes the event name part of a contract with every ancestor.
Attributes and properties follow the standard split
HTML attributes are strings, and properties are JavaScript values. LWC maps a public property firstName to the attribute first-name in markup. In a template, LWC sets the property on the child, not the attribute, so objects and arrays can be passed. That property-first binding is an LWC choice. Standard custom elements often use observedAttributes and attributeChangedCallback, and LWC does not use that mechanism for its public API.
Naming rules come from the tooling
The folder counterButton becomes the tag c-counter-button. The c is the default namespace. The kebab-case conversion exists because custom element names must contain a hyphen and cannot contain uppercase letters, which are standard rules. The folder-to-tag mapping itself is an LWC convention.
Style scoping depends on the shadow implementation
With native shadow DOM, a component's CSS cannot leak out and outside CSS cannot reach in, except through inherited properties and CSS custom properties. With synthetic shadow, the engine achieves a similar effect by rewriting selectors and adding attributes. The visible behavior is close, but edge cases differ. For example, some global DOM queries can see into synthetic shadow trees and cannot see into native ones. Write components against the standard behavior and do not depend on those leaks.
Decision criteria
Use these questions when choosing between a framework feature and a plain standard approach:
- Does a standard API do the job? For events, DOM queries, and element lifecycle, use it. This is the usual case.
- Is the feature about rendering or data flow? Templates, directives, and field reactivity belong to LWC, so use the framework approach.
- Will the code run outside LWC? If it might, keep the logic in plain ES modules with no
lwcimports, and call it from the component.
Keeping business logic in plain modules and importing them into the component class is a cheap way to follow the third question. Those modules can be tested without the LWC engine.
Exercise
A teammate says "dispatchEvent(new CustomEvent('save')) is an LWC feature, so it will not work in a plain web component." Is that correct? Name one LWC feature in the same component that would not work outside LWC.
Show answer
It is not correct. dispatchEvent and CustomEvent are browser standards and work in any custom element. Any of these LWC additions would fail outside the framework: the @api decorator, the {expression} template syntax, for:each, or renderedCallback.
Exercise
A child component dispatches new CustomEvent('change') from inside its own template, and a grandparent listens with onchange={handler} on the child's parent. The handler never runs. Which part of this is standard behavior, and what would you change?
Show answer
The default for bubbles and composed being false is standard CustomEvent behavior. The parent of the child can listen on the child tag directly, but the grandparent will not receive the event. Fix it by handling the event in the parent and dispatching a new event upward, or by setting bubbles: true (and composed: true only if it must cross shadow boundaries), accepting the wider contract.