Overview
Lightning Web Components typically render in Shadow DOM, which provides style encapsulation and DOM isolation. However, you can opt into Light DOM rendering for specific use cases where Shadow DOM restrictions are too limiting.Light DOM is available in API version 58.0 and later. Use it when you need direct DOM access from parent components or better integration with global styles.
Shadow DOM vs Light DOM
Shadow DOM (Default)
- Encapsulated styles: CSS defined in the component only affects that component
- Isolated DOM: Parent components cannot query child component internals
- Standard behavior: Default for all Lightning Web Components
Light DOM
- Shared styles: Component inherits styles from parent and global CSS
- Accessible DOM: Parent components can query child component elements
- Legacy compatibility: Better integration with older Salesforce features
Enabling Light DOM
Set therenderMode static property to 'light':
force-app/main/default/lwc/lightDomQueryChild/lightDomQueryChild.js:3-11
Update the template with the lwc:render-mode directive:
force-app/main/default/lwc/lightDomQueryChild/lightDomQueryChild.html:1-11
Querying Light DOM Elements
From Within the Component
Usethis.querySelector() instead of this.template.querySelector():
From Parent Components
Parent components can directly access elements inside Light DOM children:force-app/main/default/lwc/lightDomQuery/lightDomQuery.js:4-8
In Shadow DOM, parent components cannot query elements inside child components. Light DOM removes this restriction.
Key Differences
DOM Queries
Shadow DOM:Event Handling
Shadow DOM:- Events bubble through shadow boundaries
event.targetmay be retargeted
- Standard DOM event propagation
event.targetis the actual element
Styling
Shadow DOM:Use Cases for Light DOM
1. Third-Party Library Integration
Some libraries need direct DOM access:2. Global Style Application
Apply Salesforce Lightning Design System (SLDS) utilities:3. Accessibility Testing Tools
Some accessibility testing tools work better with Light DOM:4. Parent-Child DOM Manipulation
When parent needs direct control over child elements:Limitations
Light DOM has important limitations compared to Shadow DOM. Consider these carefully before using Light DOM.
- No style encapsulation: Component styles can conflict with parent/global styles
- No slot support:
<slot>elements don’t work in Light DOM - Security considerations: More vulnerable to XSS if not careful with DOM manipulation
- Performance: Shadow DOM can have better performance in some scenarios
Migration Considerations
Converting from Shadow DOM to Light DOM
- Add
static renderMode = 'light';to the class - Add
lwc:render-mode="light"to the template - Replace
this.template.querySelector()withthis.querySelector() - Replace
this.template.querySelectorAll()withthis.querySelectorAll() - Remove
<slot>usage (not supported in Light DOM) - Review CSS for potential conflicts with global styles
Example
Before (Shadow DOM):Best Practices
- Default to Shadow DOM: Only use Light DOM when you have a specific need
- Namespace CSS classes: Prevent style conflicts by using unique class names
- Document the choice: Comment why Light DOM is needed for future maintainers
- Test thoroughly: Ensure no style conflicts with parent components
- Validate accessibility: Light DOM can affect screen reader behavior
When NOT to Use Light DOM
- Reusable components: Shadow DOM provides better encapsulation
- Style isolation needed: Components that should look the same everywhere
- Using slots: Slots only work in Shadow DOM
- Security-sensitive: Shadow DOM provides an extra layer of isolation
