The is=""
API can be confusing, and awkward. For example (in current v1 API):
class MyButton extends HTMLButtonElement {} // 1st time customElements.define('my-button', MyButton, { extends: 'button' }) // 2nd time
<button is="my-button"></button> <!-- 3rd time -->
But, I believe it could be better:
inheritance specified once, easier non-awkward API:class MyButton extends HTMLButtonElement {} // 1st and only time customElements.define('my-button', MyButton) <my-button></my-button>
But, there's problems that the is=""
attribute solves, like making it easy to specify that a <tr>
element is
actually a my-row
element, without tripping up legacy parser behavior (among other problems). So, after discussing in this issue, I've implemented an idea in #727:
mixin-like "element behaviors", without extending builtinsThe idea uses a
For example:has=""
attribute to apply more than one behavior/functionality to an element (is=""
can only apply a single behavior/functionality to an element), using lifecycle methods similar to Custom Elements.// define behaviors elementBehaviors.define('foo', class { // no inheritance necessary connectedCallback(el) { ... } disconnectedCallback(el) { ... } static get observedAttributes() { return ['some-attribute'] } attributeChangedCallback(el, attr, oldVal, newVal) { ... } }) elementBehaviors.define('bar', class { ... }) elementBehaviors.define('baz', class { ... })<!-- apply any number of behaviors to any number of elements: --> <div has="bar baz"></div> <table> <tr has="foo baz" some-attribute="lorem"></tr> <!-- yay! --> </table> <button has="bar foo" some-attribute="ipsum"></button> <input has="foo bar baz" some-attribute="dolor"></input>In a variety of cases, this has advantages over Custom Elements (with and without
is=""
and):
- No problems with parsing (f.e. the
table/tr
example)- No inheritance, just simple classes
- Works alongside Custom Elements (even if they use
is=""
!)- "element behaviors" is similar to "entity components" (but the word "component" is already used in Web Components and many other web frameworks, so "behaviors" was a better fit).
- Lastly, "element behaviors" makes it easier to augment existing HTML applications without having to change the tags in an HTML document.
See #727 for more details.
Original Post:The spec says:
Trying to use a customized built-in element as an autonomous custom element will not work; that is,
<plastic-button>Click me?</plastic-button>
will simply create anHTMLElement
with no special behaviour.
But, in my mind, that's just what the spec says, but not that it has to be that way. Why has it been decided for it to be that way?
I believe that we can specify this type of information in the customElement.define()
call rather than in the markup. For example, that very same document shows this example:
customElements.define("plastic-button", PlasticButton, { extends: "button" });
Obviously, plastic-button
extends button
as defined right there in that call to define()
, so why do we need a redundant is=""
attribute to be applied onto button
? I think the following HTML and JavaScript is much nicer:
<!-- before: <button is="plastic-button">Click me</button> --> <plastic-button>Click me</plastic-button>
// before: const plasticButton = document.createElement("button", { is: "plastic-button" }); const plasticButton = document.createElement("plastic-button");
The necessary info for the HTML engine to upgrade that element properly is right there, in { extends: "button" }
. I do believe there's some way to make this work (as there always is with software, because we make it, it is ours, we make software do what we want, and this is absolutely true without me having to read a single line of browser source code to know it), and that is=""
is not required and can be completely removed from the spec because it seems like a hack to some limitation that can be solved somehow else (I don't know what that "somehow" is specifically, but I do know that the "somehow" exists because there's always some way to modify software to make it behave how we want within the limitations of current hardware where current hardware is not imposing any limitation on this feature).
rafalradomski, SergioMorchon, zzarcon, JoseBarrios, trustedtomato and 82 morenoopole, andr3, joekukish, plesiecki, aahoo and 10 moretoriningen, cornerCao, stackt52, thiagobustamante, Mirodil and 9 morelkraav, toastal, xgqfrms, anlexN, brainkim and 2 more
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4