LightComponent is a base class that extends LitElement and renders directly to the light DOM instead of shadow DOM. Use this when you want your component to:
Components like PersistantCollapsible and ThemeSwitcher extend LightComponent.
Create a custom component by extending LightComponent and implementing renderLightDom():
import LightComponent from './LightComponent.js';
import { html } from '../lit-all.min.js';
export default class MyLightComponent extends LightComponent {
static properties = {
message: { type: String }
};
constructor() {
super();
this.message = 'Hello';
}
renderLightDom() {
return html`
<div class="card p">
<p>${this.message} from Light DOM!</p>
</div>
`;
}
}
customElements.define('my-light-component', MyLightComponent);
Important: Always call super.updated() when overriding the updated()
lifecycle method:
updated(changedProperties) {
super.updated(); // Required!
// Your custom logic here
if(changedProperties.has('message')) {
console.log('Message changed');
}
}
Use LightComponent when:
Use ShadowComponent instead when you need style encapsulation and scoped CSS.
Use HybridComponent when you need both shadow DOM styles and light DOM rendering.
new LightComponent()Creates a new LightComponent instance that renders to the light DOM.
lightRoot: HTMLElementA container element with display: contents that holds the rendered light DOM content. Created
automatically by createRenderRoot().
createRenderRoot(): ElementCreates the light DOM render container instead of shadow DOM. This method:
div with display: contents for layout transparencylightRootthis instead of a shadow rootupdated(): voidCalled automatically by Lit when the component updates. Renders the result of renderLightDom() into
lightRoot. Override this method if needed, but always call super.updated().
childrenUpdated(): voidA custom lifecycle callback that is automatically triggered whenever a child element is added or removed from the
component. After this callback returns, requestUpdate() is automatically called to trigger a
re-render.
Override this method to react to child changes:
export default class MyComponent extends LightComponent {
childrenUpdated() {
console.log('Children changed!');
this.validateChildren();
}
}
renderLightDom(): TemplateResultOverride this method to define what should be rendered to the light DOM. Returns an empty template by default.
Use Lit's html tagged template literal to return your content.