The Problem
So I have a Lit element I am trying to move to a new document. I can get it to move and all of the functionality is still in place, as well as the communicated between it and the original document. However it is missing all of its styles.
For instance if I have a simple Lit element as such:
@customElement("my-el")
export class customEl extends LitElement {
popout () {
const newWindow = window.open('', '_blank', 'title=Search Controls,height=1447,width=405,top=0,…o,status=no,menubar=no,scrollbars=no,resizable=no');
newWindow.document.body.appendChild( newWindow.document.adoptNode( this ) );
}
static styles = css`
.custom-el {
background-color: #666;
color: #fff;
}
.custom-el .custom-el_text {
padding: 15px;
border: 1px solid red;
font-family: "Comic Sans", "Arial", "Helvetica", sans-serif;
font-size: 44px;
font-weight: 900;
}
`;
render() {
return html`
<div class="custom-el">
<p class="custom-el_text">Hello World</p>
</div>
`;
}
}
If the popout
method is called, the element is moved to the new window with everything intact except the styles.
What I've Tried
Digging into the Lit NPM package I tried tracking down where it was setting the adoptedStyleSheets
property. I didn't find it on window
, document
, renderRoot
, or shadowRoot
. I was hoping that I could somehow migrate the adopted stylesheet from one document to another.
Using importNode
instead of adoptNode
. This results in re-setting the state of the element, which is highly undesirable for my use case, but also gives the error Failed to set the 'adoptedStyleSheets' property on 'ShadowRoot': Sharing constructed stylesheets in multiple documents is not allowed
. I figure this is probably the root of why the styles aren't moved over, but I'm not sure what to do about it.
What I Need
Need a way to move element from one document to another while preserving state, functionality, and styles. State and functionality are covered with the current method, but styles are not preserved. Looking for a way to maintain all three while opening the element in a new window (popup).
Working Example
You can find a working example of this problem here: https://codepen.io/Arrbjorn/pen/bGaPMBa