Skip to content

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

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

Open
1 task done
augustjk opened this issue Jan 18, 2024 · 5 comments
Open
1 task done

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

augustjk opened this issue Jan 18, 2024 · 5 comments

Comments

@augustjk
Copy link
Member

  • I searched for an existing RRFC which might be relevant to my RRFC

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

  • Setting property to undefined
  • Setting property to null (probably not this?)
  • delete el[prop]; (unlikely to happen?)
  • Removing associated attribute
  • If we implement spread, encountering a missing property from a previously rendered spread.

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
Copy link
Member Author

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

@justinfagnani
Copy link
Contributor

I think we should do this as soon as we can. It's a frequent customer request.

@maxpatiiuk
Copy link

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

This will affect attribute-based styling (example).

If user explicitly sets the property to a default value, would that remove the attribute? If not, the .css selector would need to handle both no-attribute and default value attribute case - quite a complication.

@augustjk
Copy link
Member Author

I feel like it's more expected that HTML elements with tied attributes and properties don't spawn attributes on their own, e.g. <input> element's default type is "text" but it doesn't automatically spawn that attribute, but it will reflect to attribute if you explicitly set inputEl.type = 'text'.

However, reflecting default property values to attributes has been Lit's behavior so far and we have customers that depend on that behavior. It could be reasonable to keep that behavior as a property option, if we make non-reflection the default behavior.

@justinfagnani
Copy link
Contributor

Non-initial-reflection will only be the default behavior if you specify the defaultValue option. If an element depends on initial reflection it shouldn't use defaultValue, but could use an attribute converter that handles null attribute values.

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

No branches or pull requests

3 participants