I am creating a web components using LitElement. This is from https://lit-element.polymer-project.org/guide/start
// Import the LitElement base class and html helper function import { LitElement, html } from 'lit-element'; // Extend the LitElement base class class MyElement extends LitElement { /** * Implement `render` to define a template for your element. * * You must provide an implementation of `render` for any element * that uses LitElement as a base class. */ render(){ /** * `render` must return a lit-html `TemplateResult`. * * To create a `TemplateResult`, tag a JavaScript template literal * with the `html` helper function: */ return html` <!-- template content --> <p>A paragraph</p> `; } } // Register the new element with the browser. customElements.define('my-element', MyElement);
How to create LitElement without Shadow DOM?
I want to create it without #shadow-root here:
You don't even need Shadow DOM to use slots, you just need a template with named slots that will put values in place.
Ultimately, shadow DOM is not a requirement in order to build Web Components. However, the great synergy between shadow DOM, custom elements and CSS variables is worth exploring.
Shadow DOM is very important for web components because it allows specific sections of the HTML document to be completely isolated from the rest of the document. This means CSS styles that are applied to the DOM are not applied to the Shadow DOM, and vice versa.
First off, it's true that shadow DOM can improve style performance, so our theory about style encapsulation holds up. However, ID and class selectors are fast enough that actually it doesn't matter much whether shadow DOM is used or not – in fact, they're slightly faster without shadow DOM.
Just to make sure this question is shown as answered:
createRenderRoot
allows you to override the operation that creates a shadow root. It's usually used to render to light dom:
createRenderRoot() { return this; }
Though it could be used to render somewhere else entirely.
I really recommend using shadow DOM. Composition is difficult if an element overwrites its own light DOM.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With