Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attrs are never observed unless added with @attr decorator #208

Open
kristoforsalmin opened this issue Apr 29, 2022 · 1 comment
Open

Attrs are never observed unless added with @attr decorator #208

kristoforsalmin opened this issue Apr 29, 2022 · 1 comment
Labels
Milestone

Comments

@kristoforsalmin
Copy link

Hi folks,

While I was testing the attributes — trying to work my way around #207 — I found another subtle detail. Manually initialized attributes are never observed.

Catalyst's guide provides the following example for vanilla users: https://github.com/github/catalyst/blob/main/docs/_guide/attrs.md#what-about-without-decorators.

Please notice the second argument there defineObservedAttributes(HelloWorldElement, ['foo']) and compare it with the actual function declaration:

export function defineObservedAttributes(classObject: CustomElement): void {

It doesn't expect the second argument (arguments aren't used either).

Now, internally defineObservedAttributes() pulls attribute names from getAttrNames(), which, in turn, pulls them from attrs (WeakMap). The only way to populate attrs is to use @attr decorator:

attrs.get(proto)!.push(key)

Maybe I'm missing something, but it seems like there's no way to specify observed attributes without decorators. In any case, it'd be very nice to clarify that 🙂

Thank you!

@kristoforsalmin
Copy link
Author

kristoforsalmin commented Apr 29, 2022

Using @attr as a function (like in tests) is the best workaround I found so far:

import {attr, controller} from '@github/catalyst'

class HelloWorldElement extends HTMLElement {
  foo = 1
}

attr(HelloWorldElement.prototype, 'foo');

controller(HelloWorldElement);

Attributes are correctly initialized (solving #207) and being observed... This can actually be a recommended way to add attributes without decorators?.. A bit verbose, but no more than calling initializeAttrs() and defineObservedAttributes() manually. With a tiny wrapper on user's end it could look like defineAttrs(HelloWorldElement, ['foo']).

Thoughts?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants