Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create LitElement without Shadow DOM?

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:
enter image description here

like image 780
user7331530 Avatar asked Mar 12 '19 16:03

user7331530


People also ask

Can I use slots without Shadow DOM?

You don't even need Shadow DOM to use slots, you just need a template with named slots that will put values in place.

Can you use web components Without Shadow DOM?

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.

Why do we need Shadow DOM?

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.

Does Shadow DOM improve performance?

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.


Video Answer


1 Answers

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.

like image 118
Justin Fagnani Avatar answered Sep 28 '22 01:09

Justin Fagnani