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

@bahrus 
> 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. 

Sure, here you go:

```js
class LogAttribute extends Attribute {
 connectedCallback() {
  this.ownerElement.addEventListener("click", e => console.log(this.value));
 }
};

HTMLElement.attributeRegistry.define("log-to-console", LogAttribute);
```

Roughly the same amount of code I believe.

> Here's how that would be done with the custom enhancements proposal:

Btw your code will log the old message even if the attribute changes (this is easily fixable by moving the `msg` declaration in the event listener).

When does `attachedCallback` fire? If it can fire multiple times for an element (e.g. if it's removed and re-added to the DOM multiple times), you will have multiple listeners, and thus log the message multiple times. Depending on how `connectedCallback()` works in `Attribute`, my example above may have the same issue, but I opted to keep it as close to your example as possible.



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

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

Received on Thursday, 14 September 2023 15:35:47 UTC