lit / lit

Lit is a simple library for building fast, lightweight web components.
https://lit.dev
BSD 3-Clause "New" or "Revised" License
18.74k stars 923 forks source link

Not able to render web component in grapesJS container for lit-element 3.0 #2068

Open GaikwadShubham opened 3 years ago

GaikwadShubham commented 3 years ago

Description

I have created a web-component using lit and trying to render it in grapesJS container. The component is rendering successfully in the grapesJS container in deprecated lit-element versions [2.4.0, 2.5.1]. But as we move to latest lit-element versions (3.0.0 and above) it is giving Failed to set the 'adoptedStyleSheets' property on 'ShadowRoot': Sharing constructed stylesheets in multiple documents is not allowed as the error.

Live Reproduction Link

CodeSandbox : https://codesandbox.io/embed/musing-grothendieck-9hved?fontsize=14&hidenavigation=1&theme=dark

Expected Results

Should be able to successfully render the web-component in grapesJS container for higher version of lit-element

Actual Results

Error : Failed to set the 'adoptedStyleSheets' property on 'ShadowRoot': Sharing constructed stylesheets in multiple documents is not allowed

Browsers Affected

Only using Chrome and Edge. And the above error is present in both

justinfagnani commented 3 years ago

It sounds like grapeJS might be moving components across iframe boundaries. Is there any way to confirm that?

GaikwadShubham commented 3 years ago

Hi @justinfagnani. Yes, grapesJS is moving the components into its own created iframe. As we can see in the Elements. image

justinfagnani commented 3 years ago

Constructible stylesheets do not work across document boundaries like iframes. The best solution would be for grapeJS to define elements in the iframe that they will be in - by loading the scripts into that iframe. Short of a change to graphjs though, I think the best you can do is disable LitElement's usage of constructible stylesheets with:

delete document.adoptedStyleSheets;
sriya-srinivasan commented 3 years ago

Actually in grapesJS, we are recreating the style sheet manually, as shown here, so there is no crossing of document boundaries. So we are not sharing the stylesheet across documents. This was working perfectly for previous version of lit-element (2.x). The issue only arises when we directly use lit, or lit-element 3.x.

After going through the change log for lit-element (3.0.0), I think the error might be related to one of these changes:

If there is any info on why it is breaking now, the implications of the above changes, or if there is any work around to this, it would be great, since grapesJS and Constructible stylesheets are both important to the project.

GaikwadShubham commented 3 years ago

It sounds like grapeJS might be moving components across iframe boundaries. Is there any way to confirm that?

Constructible stylesheets do not work across document boundaries like iframes. The best solution would be for grapeJS to define elements in the iframe that they will be in - by loading the scripts into that iframe. Short of a change to graphjs though, I think the best you can do is disable LitElement's usage of constructible stylesheets with:

delete document.adoptedStyleSheets;

Hi @justinfagnani, I tried implementing the changes which you specified. But still getting the same error of 'AdoptedStyleSheet'

vasicvuk commented 10 months ago

any news about this issue?