Consider making `styleElement` force a host to shim custom properties
In order for shimmed custom properties to work correctly, (1) an element must have prepared its styles via prepareTemplateStyles, (2) must be in the DOM tree when it calls styleElement and (3) its host ancestors must already have called styleElement (shimming must go in tree order).
In some cases, an element will perform steps (1) and (2) in connectedCallback. However, this can introduce an ordering issue that prevents step (3) from working correctly.
- host
- calls
prepareTemplateStyles - renders client DOM
- client
- client calls
prepareTemplateStyles - client renders DOM
- client calls
styleElement
- client calls
- client
- calls
styleElement
- calls
In this case, shimming does not go in tree order. To address this, we could ensure that when styleElement is called and a styling host is found, it is forced to calculate custom properties (here) if they do not exist. This was previously done in Polymer 1.x and would help with this issue in lit-html: https://github.com/Polymer/lit-html/issues/655#issuecomment-442648651
At https://github.com/webcomponents/shadycss/blob/master/src/scoping-shim.js#L342:
It seems like the minimal change we could make is to add after line 342:
if (!ownerProperties) {
this._updateProperties(owner);
ownerProperties = ownerStyleInfo.styleProperties;
}