Skip to content

Commit de69588

Browse files
author
Steve Orvell
committed
Address feedback, add best practices
1 parent 2f4be53 commit de69588

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

packages/lit-dev-content/samples/properties/attributereflect/my-element.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class MyElement extends LitElement {
66
@property({type: Boolean, reflect: true})
77
active: boolean = false;
88

9-
@property({type: String, reflect: true, useDefault: true})
9+
@property({type: String, reflect: true, useDefault: true} as PropertyDeclaration)
1010
variant = 'normal';
1111

1212
static styles = css`

packages/lit-dev-content/site/docs/v3/components/properties.md

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -566,7 +566,9 @@ If this behavior doesn't fit your use case, there are a couple of options:
566566

567567
Setting `reflect` to true configures a property so that whenever it changes, its value is reflected to its [corresponding attribute](#observed-attributes). Reflected attributes are useful for serializing element state and because they are visible to CSS and DOM APIs like `querySelector`.
568568

569-
Setting `useDefault` to true prevents the property's default value from initially reflecting to its [corresponding attribute](#observed-attributes). All subsequent changes are reflected; and if the attribute is removed, the property is reset to its default value. This matches web platform behavior for attributes like `id`. The default value of an element's `id` property is `''` (an empty string) and initially it does not have an `id` attribute, but if the `id` property is set (even to an empty string), the appropirate `id` attribute is reflected. If the `id` attribute is removed, the element's `id` property is set back to its initial value of `''`.
569+
Setting `useDefault` to true prevents the property's default value from initially reflecting to its [corresponding attribute](#observed-attributes). All subsequent changes are reflected; and if the attribute is removed, the property is reset to its default value.
570+
571+
This matches web platform behavior for attributes like `id`. The default value of an element's `id` property is `''` (an empty string) and initially it does not have an `id` attribute, but if the `id` property is set (even to an empty string), the appropirate `id` attribute is reflected. If the `id` attribute is removed, the element's `id` property is set back to its initial value of `''`.
570572

571573
For example:
572574

@@ -582,16 +584,24 @@ When the property changes, Lit sets the corresponding attribute value as describ
582584

583585
{% playground-example "properties/attributereflect" "my-element.ts" %}
584586

585-
Attributes should generally be considered input to the element from its owner, rather than under control of the element itself, so reflecting properties to attributes should be done sparingly. It's necessary today for cases like styling and accessibility, but this is likely to change as the platform adds features like the [`:state` pseudo selector](https://wicg.github.io/custom-state-pseudo-class/) and the [Accessibility Object Model](https://wicg.github.io/aom/spec/), which fill these gaps.
586-
587-
Reflecting properties of type object or array is not recommended. This can cause large objects to serialize to the DOM which can result in poor performance.
588-
589587
<div class="alert alert-info">
590588

591589
**Lit tracks reflection state during updates.** You may have realized that if property changes are reflected to an attribute and attribute changes update the property, it has the potential to create an infinite loop. However, Lit tracks when properties and attributes are set specifically to prevent this from happening
592590

593591
</div>
594592

593+
### Best practices when reflecting attributes
594+
595+
To ensure elements behave as expected and perform well, try to follow these best practices when reflecting attributes:
596+
597+
* Attributes should generally be considered input to the element from its owner, rather than under control of the element itself, so reflecting properties to attributes should be done sparingly. Consider instead using the [`:state` pseudo selector](https://wicg.github.io/custom-state-pseudo-class/) and the [Accessibility Object Model](https://wicg.github.io/aom/spec/) where possible.
598+
599+
* Reflecting properties should typically also set `useDefault` since this helps match expected platform behavior.
600+
601+
* Reflecting properties of type object or array is not recommended. This can cause large objects to serialize to the DOM which can result in poor performance and consume excess memory when `useDefault` is used.
602+
603+
* In general user property settings should not be changed, but if you want to do so, define a property setter. Setting `useDefault` will only reset the property value to the default when the corresponding attribute is removed.
604+
595605
## Custom property accessors {#accessors}
596606

597607
By default, LitElement generates a getter/setter pair for all reactive properties. The setter is invoked whenever you set the property:

0 commit comments

Comments
 (0)