Sub topic 6
DOM access: what your component can reach
This chapter assumes you can build a basic Lightning web component (HTML template, JavaScript class, metadata file). It also assumes you know modern JavaScript and the idea of the same-origin policy. You should already know that Lightning Web Security (LWS) isolates code by namespace. Here you will see what that means for the DOM, the browser's tree of page elements.
The working model
Under LWS, your component does not get the raw document and window objects. It gets versions that the sandbox has changed (distorted). These versions let you reach your own rendered content and block most of the rest of the page.
Three rules cover most cases:
- Use
this.templateto reach elements inside your own component. - Do not expect to find elements that belong to other namespaces, even when they are on the same page.
- Treat the global
documentas a limited view, not as the whole page.
Querying your own template
this.template is the root of your component's shadow tree. A shadow tree is a private DOM subtree attached to a component. Calling querySelector on it searches only inside your component.
import { LightningElement } from 'lwc';
export default class FocusInput extends LightningElement {
handleClick() {
// Query only inside this component's own template.
const input = this.template.querySelector('lightning-input');
if (input) {
input.focus();
}
}
}Always check the result for null. The element may not have rendered yet, or it may sit inside a conditional block that is currently hidden.
Call querySelector from renderedCallback or from an event handler, not from connectedCallback. When connectedCallback runs, the template has not rendered yet, so the query returns null.
What reaches outside your template
Code such as document.querySelector('.some-class') searches from the document. Under LWS it can return elements your namespace owns, but it will not give you a path into another namespace's content. Code that reads or edits elements outside your component is fragile even where it works, because the page structure can change without notice.
| Goal | Preferred approach | Risky approach |
|---|---|---|
| Focus or read an element you render | `this.template.querySelector` | `document.querySelector` |
| Read a child component's state | A public `@api` property or method | Reaching into the child's internal DOM |
| Change another component's display | Pass data in, or fire an event out | Editing its elements directly |
| Find an element by id | A `data-` attribute or `lwc:ref` inside your template | Global id lookups |
Child components and slots
A child component you render from your own template, such as lightning-input, is an element in your template. You can query for it and call its public API, such as focus(). You cannot rely on reaching the elements inside its own shadow tree. Those belong to the child.
Slotted content passed from a parent is a different case. It is part of the parent's DOM, so query it with this.querySelector on the host, not this.template.querySelector.
import { LightningElement } from 'lwc';
export default class Card extends LightningElement {
renderedCallback() {
// Slotted content supplied by a parent lives in the light DOM.
const slotted = this.querySelector('p');
// Elements in this component's own template use this.template.
const own = this.template.querySelector('.title');
console.log(Boolean(slotted), Boolean(own));
}
}Common errors
- A query returns
nullunexpectedly. The element has not rendered, a conditional hid it, or it belongs to another namespace. - Code works in a test page but not in the org. The test page ran outside the LWS sandbox. Test inside the org.
- A script that edits the page directly stops working. Scripts that depend on a global view of the DOM break under isolation. Move the logic into your own template and drive it with data.
- A selector is too broad. A selector such as
divmatches many elements. Use a specific class ordata-attribute.
Summary of the rules
Query within your own template. Use public properties, methods, and events to talk to other components. Leave other namespaces' elements alone. The next chapters cover standard web APIs and cross-namespace communication in more detail.
Exercise
A component calls document.querySelector('.result') in connectedCallback to find a div in its own template, and gets null. Give two reasons it fails and the fix.
Show answer
First, connectedCallback runs before the template renders, so the element does not exist yet. Second, the global document is a limited view under LWS and is not the right way to reach your own elements. The fix is to call this.template.querySelector('.result') from renderedCallback or an event handler, and check the result for null.
Exercise
A parent passes a <p> into your component's default slot. Which call finds it from inside your component: this.template.querySelector('p') or this.querySelector('p')?
Show answer
this.querySelector('p'). Slotted content is light DOM supplied by the parent, so it is not inside your component's own template.