[reactive-element] Adds skipping initial attribute reflection for default values.#4895
[reactive-element] Adds skipping initial attribute reflection for default values.#4895
Conversation
🦋 Changeset detectedLatest commit: 3dfc1ce The changes in this PR will be included in the next version bump. This PR includes changesets to release 3 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
|
Nice! My main comment before looking at code is that I don't think we should have the option If for some reason in the future enabling initial value reflection is important, we could add an I also think the option name should be |
| } | ||
| return v; | ||
| this._$changeProperty(name, undefined, options, true); | ||
| return v ?? (options.defaultValue as V); |
There was a problem hiding this comment.
What do you think about a dev mode warning that they have both a default value and an initializer?
There was a problem hiding this comment.
I think it's not really possible to detect for all cases, and having a warning that sometimes shows just seems confusing and hard to maintain.
For example, decorator v. not, accessor v. field, instance shadowing, etc.
There was a problem hiding this comment.
I hope in the future we can move to standard decorators only and this will be the place we warn. I guess not that many people would use it now. We can try to add a lint warning somewhere.
| continue; | ||
| } | ||
| const value = this[p as keyof this]; | ||
| // Ensure any initially set value is seen as a change. |
There was a problem hiding this comment.
Do we want to do this for default values? It kind of makes sense to me to consider the initial default value not changed.
Or maybe that's what this does? Might be clearer if the comments are inside the conditions? If so, maybe mention that in the jsdocs for the defaultValue option too.
There was a problem hiding this comment.
Made the comments clearer. Properties with defaultValue set are currently included in initial changes.
We could choose to change this but the number of different ways properties can be configured makes this really hard to manage. (In fact there are some issues with this currently: accessor defined properties are only handled specially with the decorator and not in JS.)
Co-authored-by: Justin Fagnani <justin@fagnani.com>
Co-authored-by: Justin Fagnani <justin@fagnani.com>
|
Awesome @sorvell!! We need to update lit.dev for this feature. Can you do that? |
|
Docs PR: lit/lit.dev#1396 |
|
defaultValue shouldn't be a function ? It can be eager when the default value is an object. With a function, we can delay the creation only when needed. |
|
19963dd adds setting the default value when the attribute is removed. It also changes the strategy used to ensure the default value is the value of the property at construction time. We need to modify the getter to do this since we can't set the value before the user's constructor runs (at least for The default value is returned from the getter when it would otherwise return undefined and the setter for the property has been called (this is recorded via a set). Maintaining this set is a slight tax/complication. Concerns and Alternatives
|
Adds support for lit/rfcs#36 and helps facilitate shoelace-style/shoelace#2339.
Spawning an initial attribute can cause a NextJS hydration error, see repro.
To use, set
defaultValuein property options, and avoid setting the default in the field/accessor.Previously, this would spawn a
variant="foo"attribute. With this PR and the above settings, it will not.In addition, the default value will be set when the attribute is removed.