lit / rfcs

RFCs for changes to Lit
BSD 3-Clause "New" or "Revised" License
15 stars 10 forks source link

[RRFC] Keeping and reverting to default values for properties #36

Open augustjk opened 8 months ago

augustjk commented 8 months ago

Motivation

Currently the common pattern of setting default values for properties to do it in the constructor or class field instantiation. These are really more initial values that we lose after instantiation. Users may actually want some behavior where "unsetting" a property will revert to a default value.

It's possible to manually add this behavior by falling back to some value in render(), willUpdate(), or custom accessors[1] or even attribute converters if we wish to consider removing an attribute as "unsetting" as well.

There have also been calls for this behavior for or React wrapper[2] which we could add on at the wrapper level but would also be nice to have it for the underlying Lit element.

We do need to decide what "unsetting" means

How

Eventually when the only way to declare a property is with standard decorators, I believe we get access to the initial value set on the class field (with auto-accessor). Until then, we would need to add a property option for the default value.

Once we collect the default value, we can add it as a fallback value in an appropriate place like property setter or attribute converter.

References

augustjk commented 8 months ago

Having an alternate way of specifying a default value will also allow not reflecting initial property value to attribute.