You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
It is possible to define custom-elements built-in but there is an inconsistent behavior regarding these:
if the node is already live, with its own is attribute, styles that target such attribute are applied
if a custom element is registered, and its class is used to create a new instance, the is attribute is not automatically set
if a custom element is registered, and a node created via document.createElement(tag, {is: ceName}) is appended, the is attribute is not automatically set
In both latter cases the is attribute is revealed only after an explicit element.outerHTML or through its parentElement.innerHTML.
This behavior makes styling custom elements cumbersome, unless the constructor doesn't explicitly also set the attribute.
Example
// this makes BlueButton not styled via button[is="blue-button"]classBlueButtonextendsHTMLButtonElement{constructor(){super().textContent='is it blue?';}}customElements.define('blue-button',BlueButton,{extends: 'button'});// this makes RedButton styled via button[is="red-button"]classRedButtonextendsHTMLButtonElement{constructor(){super()this.textContent='is it red?';this.setAttribute('is','red-button');}}customElements.define('red-button',RedButton,{extends: 'button'});// testdocument.head.appendChild(document.createElement('style')).textContent=` button[is="blue-button"] { color: blue; } button[is="red-button"] { color: red; }`;document.body.append(newBlueButton,newRedButton);// note only the red button is red
As summary
While it's trivial enough to tell every developer that the constructormust explicitly set the is attribute, this looks like a bug in the current specs, as already live elements gets upgraded without such need, but elements created via class or document can't be styled as expected.
Thanks for eventually fixing this.
The text was updated successfully, but these errors were encountered:
CodePen Live Example
Issue
It is possible to define custom-elements built-in but there is an inconsistent behavior regarding these:
is
attribute, styles that target such attribute are appliedis
attribute is not automatically setdocument.createElement(tag, {is: ceName})
is appended, theis
attribute is not automatically setIn both latter cases the
is
attribute is revealed only after an explicitelement.outerHTML
or through itsparentElement.innerHTML
.This behavior makes styling custom elements cumbersome, unless the
constructor
doesn't explicitly also set the attribute.Example
As summary
While it's trivial enough to tell every developer that the
constructor
must explicitly set theis
attribute, this looks like a bug in the current specs, as already live elements gets upgraded without such need, but elements created via class ordocument
can't be styled as expected.Thanks for eventually fixing this.
The text was updated successfully, but these errors were encountered: