Skip to content

Commit 39b25a0

Browse files
author
Steve Orvell
committed
Add docs for defaultValue
1 parent 7c19cc2 commit 39b25a0

File tree

2 files changed

+44
-8
lines changed

2 files changed

+44
-8
lines changed
Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,40 @@
1-
import {LitElement, html, css} from 'lit';
1+
import {LitElement, html, css, PropertyDeclaration} from 'lit';
22
import {customElement, property} from 'lit/decorators.js';
33

44
@customElement('my-element')
55
class MyElement extends LitElement {
66
@property({type: Boolean, reflect: true})
77
active: boolean = false;
88

9+
@property({type: String, reflect: true, defaultValue: 'normal'} as PropertyDeclaration)
10+
variant!: string|null;
11+
912
static styles = css`
1013
:host {
11-
display: inline-block;
14+
display: inline-block; padding: 4px;
1215
}
13-
1416
:host([active]) {
15-
border: 1px solid red;
17+
font-weight: 800;
18+
}
19+
:host([variant]) {
20+
outline: 4px solid green;
21+
}
22+
:host([variant="special"]) {
23+
border-radius: 8px; border: 4px solid red;
1624
}`;
1725

1826
render() {
1927
return html`
20-
<span>Active: ${this.active}</span>
21-
<button @click="${() => this.active = !this.active}">Toggle active</button>
28+
<div><label>active: <input type="checkbox"
29+
.value="${this.active}"
30+
@change="${(e: Event) => this.active = (e.target! as HTMLInputElement).checked}">
31+
${this.active}
32+
</label></div>
33+
<div><label>variant: <input type="checkbox"
34+
.value="${this.variant === 'special'}"
35+
@change="${(e: Event) => this.variant = (e.target! as HTMLInputElement).checked ? 'special' : 'normal'}">
36+
${this.variant}
37+
</label></div>
2238
`;
2339
}
2440
}

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

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,10 +129,13 @@ class MyElement extends LitElement {
129129
}
130130
```
131131

132-
In **JavaScript**, you **must not use class fields** when declaring reactive properties. Instead, properties must be initialized in the element constructor:
132+
In **JavaScript**, you **must not use class fields** when declaring reactive properties. Instead, properties must be either initialized in the element constructor or set with the `defaultValue` option:
133133
```js
134134
class MyElement extends LitElement {
135-
static properties = {foo: {type: String}}
135+
static properties = {
136+
foo: {type: String},
137+
bar: {type: String, defaultValue: 'Default'}
138+
}
136139
constructor() {
137140
super();
138141
this.foo = 'Default';
@@ -219,6 +222,18 @@ A [custom converter](#conversion-converter) for converting between properties an
219222
</dd>
220223
<dt>
221224

225+
`defaultValue`
226+
227+
</dt>
228+
<dd>
229+
230+
When set, the property is initialized to this value and if the `reflect` option is `true`, the initial default value does *not* reflect. Subsequent changes to the property will reflect, even if they are equal to the default value.
231+
232+
Avoid setting both a default value and defining a field initializer or setting the property in the constructor or connectedCallback.
233+
234+
</dd>
235+
<dt>
236+
222237
`hasChanged`
223238

224239
</dt>
@@ -553,11 +568,16 @@ If this behavior doesn't fit your use case, there are a couple of options:
553568

554569
You can configure a property so that whenever it changes, its value is reflected to its [corresponding attribute](#observed-attributes). Reflected attributes are useful because attributes are visible to CSS, and to DOM APIs like `querySelector`.
555570

571+
Note, if you set a `defaultValue` for the property, this value will *not* be initially reflected to the corresponding attribute. All subsequent changes will be reflected. This matches web platform behavior for attributes like `id`. For example, 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.
572+
556573
For example:
557574

558575
```js
559576
// Value of property "active" will reflect to attribute "active"
560577
active: {reflect: true}
578+
// Value of property "variant" will reflect except that it will not
579+
// be initialized to the default value of `normal`.
580+
variant: {reflect: true, defaultValue: 'normal'}
561581
```
562582

563583
When the property changes, Lit sets the corresponding attribute value as described in [Using the default converter](#conversion-type) or [Providing a custom converter](#conversion-converter).

0 commit comments

Comments
 (0)