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
The attributeChangedCallback function is called whenever an observed attribute is set, added or removed. The attributeChangedCallback is called with the raw values of the attribute name, old value and new value.
This can be surprising when using the @attr decorator which gives conveniences over the attributes by allowing for camel-cased names eliding their data- prefix, and allowing types other than string. It can also be surprising to see attributeChangedCallback being called with null when you type an @attr as string.
When this element first connects, attributeChangedCallback('data-foo', null, '') will fire. attributeChangedCallback will never fire with 'foo' as the name, because that is a Catalyst concept and not a WC concept. The same is true of the boolean type; attributeChangedCallback will never fire with booleans for values. This means developers have to suddenly work around all of the conveniences @attr offers them:
@controllerclassFooBarElementextendsHTMLElement{
@attrfoo=trueattributeChangedCallback(name,oldValue,newValue){if(name==='data-foo'){// Smell: `data-foo` is the name of the attribute but we call if `foo` everywhere else in the fileif(newValue===null){// Smell: this should really be `if (oldValue === false)` to align with our expected typesdoStuff();}}}}
Possible Solutions
Document this gotcha and just let developers deal with it.
This burdens developers with the extra concepts they have to keep in their brain
It demonstrates that this is a leaky abstraction
The code they eventually have to write still smells
Add extra code to call attributeChangedCallback with the mapped name, oldValue, newValue
Potentially harmful: attributeChangedCallback has a fixed signature where it is only called with string|null and we're abusing that contract.
This will cause the attributeChangedCallback to fire far more often, effectively double for each change to an attr mapped value. Use cases which do not care about the argument values will see more - effectively redundant - calls.
Add extra code to call a new function (maybe attrChangedCallback) with the mapped name, oldValue, newValue.
This means further divergence from web component spec.
More to document, more stuff that Catalyst is responsible for.
Add a function which can be given the 3 arguments and map it back to prop names (e.g. const [realName, realOldValue, realNewValue] = this.attributeToAttr(name, oldValue, newValue))
I hate it
Still burdens developers with extra concepts
Still demonstrates the leaky abstraction
Still requires more documentation, more stuff that Catalyst is responsible for.
Add events which are automatically dispatched when attributes changed, which can be listened for. changing HelloWorldElement's @attr name = '' value could emit hello-world-name-changed.
It's still idiomatic to web components... sort of
Elements become very noisy
Required elements binding to their own events, so they're not in control of their own state.
Still requires more documentation
The text was updated successfully, but these errors were encountered:
I'm mostly leaning towards 2. It feels like the least amount of divergence from Web Components follows our thinking of making Web Components easier to work with.
How do you feel about it abusing/violating the contract that attributeChangedCallback has?
Also an added caveat with 2 is that we're effectively increasing the rate at which the callback is fired, meaning use cases which don't care about the arguments will see more effectively redundant calls. None of the other solutions have this problem. I've added this to the OP.
I'd perfer the 1st mentioned solution: Document this gotcha and just let developers deal with it.
It demonstrates that this is a leaky abstraction/ the code they eventually have to write still smells
In many cases the attributeChangedCallback is not necessary. And in my opinion, the link to mozilla.org/…/#using_the_lifecycle_callbacks is quite enough. And many developers have already come into contact with things like an "unchecked input checkbox".
The
attributeChangedCallback
function is called whenever an observed attribute is set, added or removed. TheattributeChangedCallback
is called with the raw values of the attribute name, old value and new value.This can be surprising when using the
@attr
decorator which gives conveniences over the attributes by allowing for camel-cased names eliding theirdata-
prefix, and allowing types other thanstring
. It can also be surprising to seeattributeChangedCallback
being called withnull
when you type an@attr
asstring
.For example:
When this element first connects,
attributeChangedCallback('data-foo', null, '')
will fire.attributeChangedCallback
will never fire with'foo'
as the name, because that is a Catalyst concept and not a WC concept. The same is true of theboolean
type;attributeChangedCallback
will never fire with booleans for values. This means developers have to suddenly work around all of the conveniences@attr
offers them:Possible Solutions
attributeChangedCallback
with the mappedname
,oldValue
,newValue
attributeChangedCallback
has a fixed signature where it is only called withstring|null
and we're abusing that contract.attributeChangedCallback
to fire far more often, effectively double for each change to anattr
mapped value. Use cases which do not care about the argument values will see more - effectively redundant - calls.attrChangedCallback
) with the mappedname
,oldValue
,newValue
.const [realName, realOldValue, realNewValue] = this.attributeToAttr(name, oldValue, newValue)
)HelloWorldElement
's@attr name = ''
value could emithello-world-name-changed
.The text was updated successfully, but these errors were encountered: