Sub topic 1
The Web Component Standards LWC Builds On
Prerequisites
This chapter assumes you know basic HTML and CSS, JavaScript classes and arrow functions, and how the browser DOM and event listeners work. You also need a general idea of what a UI component is. You don't need any Salesforce experience.
Why start with standards
Lightning Web Components (LWC) is a framework for building web components. It does not replace the browser's component model. It sits on top of it. Before you write an LWC component, it helps to know which parts the browser already provides. Later chapters cover each standard in detail. This chapter gives you the map.
The four standards
Four web platform features do most of the work.
| Standard | What it provides | Where it shows up in LWC |
|---|---|---|
| Custom elements | Your own HTML tags, backed by a JavaScript class | Every component is a custom element |
| Shadow DOM | A private DOM tree and scoped CSS per element | Each component renders into a shadow root |
| HTML template and slot elements | Inert markup and placeholders for child content | Templates and slots in component HTML |
| ES modules | `import` and `export` for sharing code | Each component class is a module |
Custom elements
A custom element is an HTML tag you define by extending HTMLElement and registering the class with the browser. The tag name must contain a hyphen.
class HelloGreeting extends HTMLElement {
connectedCallback() {
const root = this.attachShadow({ mode: 'open' });
const name = this.getAttribute('name') ?? 'World';
root.innerHTML = `<p>Hello, ${name}!</p>`;
}
}
customElements.define('hello-greeting', HelloGreeting);Once registered, <hello-greeting name="Ada"></hello-greeting> works in any HTML page. The browser calls connectedCallback when the element is added to the page.
Shadow DOM
A shadow root is a separate DOM tree attached to an element. In the example above, attachShadow creates it. Markup and styles inside the shadow root are hidden from the rest of the page, and page styles do not leak in. This is called encapsulation.
Templates and slots
The <template> element holds markup that the browser parses but does not render. The <slot> element marks a place where a parent can insert its own content. Together they let a component define its structure and accept child content.
ES modules
An ES module is a JavaScript file that uses import and export. Each file has its own scope, and dependencies are explicit. LWC component classes are written as ES modules.
How the standards fit together
Read the diagram from top to bottom. A module delivers the class. The browser treats the class as a custom element. The element owns a shadow root. The template fills that root, and slots let a parent supply content.
What LWC adds
The plain example above works without any framework, but you must write the DOM updates yourself. LWC adds a compiler, a template syntax with {name} expressions, reactive class fields, and decorators such as @api. Chapter by chapter, you will learn to separate these additions from the standards underneath.
Exercise
Which of the four standards gives a component its own private DOM tree and scoped styles, and which gives it a tag name?
Show answer
Shadow DOM provides the private DOM tree and scoped styles. Custom elements provide the tag name, through customElements.define.
Exercise
In the HelloGreeting example, what triggers the code that creates the shadow root?
Show answer
The browser calls connectedCallback when the element is added to the page, and that method calls attachShadow.