Re: [WICG/webcomponents] Proposal: Custom attributes for all elements, enhancements for more complex use cases (Issue #1029)

@LeaVerou 

I agree very much with two things you have said:  

>Also, I think adding custom attributes to other custom elements, while important, is probably one of the least prominent use cases (compared to adding them to built-ins, and adding them to your own components).

and I *try* to abide by the principal you've brought up -- about making simple or common things, easy to do, while not making complex things impossible.  So, thinking about it last night, here's my simple use case, and I will add to it my proposed solution to that simple case, that I think typifies the "common" use cases in my mind:

Say all you need to do is to create an isolated behavior/enhancement/hook/whatever associated with an attribute, say "log-to-console" anytime the user clicks on elements adorned with that attribute, where we can specify the message.  Here's how that would be done with the custom enhancements proposal:

```JS
customEnhancements.define('log-to-console', class extends ElementEnhancement{
    attachedCallback(enhancedElement: Element){
        const msg = enhancedElement.getAttribute('log-to-console');
        enhancedElement.addEventListener('click', e => {
            console.log(msg);
        });
    }
});
```

```HTML
<svg log-to-console="clicked on an svg"></svg>
    ...
<div log-to-console="clicked on a div"></div>

...

<some-custom-element enh-log-to-console="clicked on some custom element"></some-custom-element>
```

Done!

If your proposed alternative is as simple, then my question to you regarding vue and mavo was truly out of left field and irrevelant.  (I really do want to peg my hopes on someone else's proposal, not my own, so hoping you can address this concern I have).  How would your proposal solve this?  How many lines of code would be required to cover all the DOM elements?

@sorvell , you had me in complete agreement until you proposed the has option -- it seems to me solving the problem above 
 with the has option would require introducing two attributes (one of them data-) which doubles the chances of conflicts with other developers.  

And can we go to teams like HTMX and Alpine and Wordpress and say "sorry, to get the platform's blessings, what you've been doing is wrong -- you need two attributes to solve this problem, which I know is more complicated, few if anyone has or is doing that, and increases the chances of naming conflicts, but the benefits are..." 

And that's where I'm drawing a blank, without attempting to mind read, and probably getting the benefits wrong.  Could you explain what I'm missing?





-- 
Reply to this email directly or view it on GitHub:
https://github.com/WICG/webcomponents/issues/1029#issuecomment-1719589160
You are receiving this because you are subscribed to this thread.

Message ID: <WICG/webcomponents/issues/1029/1719589160@github.com>

Received on Thursday, 14 September 2023 14:41:05 UTC