Skip to content

Commit 8283b42

Browse files
Arthur EvansSteve Orvell
andauthored
Rendering update (#278)
* Updated components/rendering. * Reordered sections. * Expanded first section. Co-authored-by: Steve Orvell <[email protected]>
1 parent 9988013 commit 8283b42

File tree

3 files changed

+55
-78
lines changed

3 files changed

+55
-78
lines changed
Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,6 @@
11
import {LitElement, html} from 'lit';
22
import {customElement, property} from 'lit/decorators.js';
33

4-
function headerTemplate(title: string) {
5-
return html`<header>${title}</header>`;
6-
}
7-
function articleTemplate(text: string) {
8-
return html`<article>${text}</article>`;
9-
}
10-
function footerTemplate() {
11-
return html`<footer>Your footer here.</footer>`;
12-
}
134

145
@customElement('my-page')
156
class MyPage extends LitElement {
@@ -20,11 +11,23 @@ class MyPage extends LitElement {
2011
text: 'Some witty text.',
2112
};
2213

14+
headerTemplate() {
15+
return html`<header>${this.article.title}</header>`;
16+
}
17+
18+
articleTemplate() {
19+
return html`<article>${this.article.text}</article>`;
20+
}
21+
22+
footerTemplate() {
23+
return html`<footer>Your footer here.</footer>`;
24+
}
25+
2326
render() {
2427
return html`
25-
${headerTemplate(this.article.title)}
26-
${articleTemplate(this.article.text)}
27-
${footerTemplate()}
28+
${this.headerTemplate()}
29+
${this.articleTemplate()}
30+
${this.footerTemplate()}
2831
`;
2932
}
3033
}

packages/lit-dev-content/site/docs/components/rendering.md

Lines changed: 39 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -8,102 +8,77 @@ eleventyNavigation:
88

99
Add a template to your component to define what it should render. Templates can include _expressions_, which are placeholders for dynamic content.
1010

11-
```ts
12-
render() {
13-
return html`<div>Hello, ${name}.</div>`;
14-
}
15-
```
16-
17-
## When templates render
18-
19-
A Lit component renders its template initially when it's added to the DOM on a page. After the initial render, any change to the component's reactive properties triggers an update cycle, re-rendering the component.
20-
21-
The update cycle is _asynchronous_, so changes to multiple properties are batched into a single update. And when Lit updates, it only re-renders the changed portions of the DOM.
11+
To define a template for a Lit component, add a `render()` method:
2212

23-
For more information about the update cycle, see [What happens when properties change](/docs/components/properties/#update-cycle).
13+
{% playground-example "docs/templates/define" "my-element.ts" %}
2414

25-
## DOM encapsulation
15+
Write your template in HTML inside a JavaScript [tagged template literal](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals#tagged_templates) using Lit's `html` tag function.
2616

27-
Lit uses shadow DOM to encapsulate the DOM a component renders. Shadow DOM provides three benefits:
17+
Lit templates can include JavaScript _expressions_. You can use expressions to set text content, attributes, properties, and event listeners. The `render()` method can also include any JavaScript—for example, you can create local variables for use in expressions.
2818

29-
* DOM scoping. DOM APIs like `document.querySelector` won't find elements in the
30-
component's shadow DOM, so it's harder for global scripts to accidentally break your component.
31-
* Style scoping. You can write encapsulated styles for your shadow DOM that don't
32-
affect the rest of the page. No need for conventions like BEM to associate styles with your component.
33-
* Composition. The component's shadow DOM (managed by the component) is separate from the component's children. You can choose how children are rendered in your templated DOM. Component users can add and remove children using standard DOM APIs without accidentally breaking anything in your shadow DOM.
19+
Typically, the component's `render()` method returns a single `TemplateResult` object (the same type returned by the `html` tag function). However, it can return anything that Lit can render:
3420

35-
For more information about shadow DOM, see [Shadow DOM v1: Self-Contained Web Components
36-
](https://developers.google.com/web/fundamentals/web-components/shadowdom) on Web Fundamentals.
21+
* Primitive values like string, number, or boolean.
22+
* `TemplateResult` objects created by the `html` function.
23+
* DOM Nodes.
24+
* Arrays or iterables of any of the supported types.
3725

38-
<div class="alert alert-info">
26+
For more information about writing templates, see [Templates](/docs/templates/overview/).
3927

40-
Where native shadow DOM isn't available on older browsers, Lit can optionally use the [Shady DOM polyfill](https://github.com/webcomponents/polyfills/tree/master/packages/shadydom) to provide emulated shadow DOM support.
28+
## Writing a good render() method
4129

42-
</div>
30+
To take best advantage of Lit's functional rendering model, your `render()` method should follow these guidelines:
4331

44-
## Define a template
32+
* Avoid changing the component's state.
33+
* Avoid producing any side effects.
34+
* Use only the component's properties as input.
35+
* Return the same result when given the same property values.
4536

46-
To define a template for a Lit component, add a `render()` method:
37+
Following these guidelines keeps the template deterministic, and makes it easier to reason about the code.
4738

48-
{% playground-example "docs/templates/define" "my-element.ts" %}
39+
In most cases you should avoid making DOM updates outside of `render()`. Instead, express the component's template as a function of its state, and capture its state in properties.
4940

50-
* Write your template in HTML inside a JavaScript [template literal](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals) by enclosing the raw HTML in back-ticks (<code>``</code>).
41+
For example, if your component needs to update its UI when it receives an event, have the event listener set a reactive property that is used in `render()`, rather than manipulate the DOM directly.
5142

52-
* Tag your template literal with the [`html`](TODO_HREF) tag function.
43+
For more information, see [Reactive properties](/docs/components/properties/).
5344

54-
* The component's `render` method can return anything that Lit can render. Typically, it returns a single `TemplateResult` object (the same type returned by the `html` tag function).
45+
## Composing templates
5546

56-
Lit templates can include JavaScript _expressions_. You can use expressions to set text content, attributes, properties, and event listeners.
47+
You can compose Lit templates from other templates. The following example composes a template for a component called `<my-page>` from smaller templates for the page's header, footer, and main content:
5748

58-
For more information, see [Templates overview](/docs/templates/overview/).
49+
{% playground-example "docs/templates/compose" "my-page.ts" %}
5950

60-
### Design a performant template
51+
In this example, the individual templates are defined as instance methods, so a subclass could extend this component and override one or more templates.
6152

62-
During an update, only the parts of the DOM that change are re-rendered. Although Lit templates look like string interpolation, Lit parses and creates static HTML once, and then only updates changed values in expressions after that, making updates very efficient.
53+
{% todo %}
6354

64-
To take best advantage of Lit's functional rendering model, follow these guidelines for implementing your `render()` method:
55+
Move example to composition section, add xref.
6556

66-
* Does not change the component's state.
67-
* Does not have any side effects.
68-
* Only depends on the component's properties.
69-
* Returns the same result when given the same property values.
57+
{% endtodo %}
7058

71-
Also, avoid making DOM updates outside of `render()`. Instead, express the component's template as a function of its state, and capture its state in properties.
59+
You can also compose templates by importing other elements and using them in your template:
7260

73-
The following code manipulates the rendered DOM. This is usually an anti-pattern:
61+
{% playground-ide "docs/templates/composeimports" %}
7462

75-
```ts
76-
// Anti-pattern. Avoid!
77-
constructor() {
78-
super();
79-
this.loadStuff().then((content) => {
80-
this.shadowRoot.querySelector('#message')
81-
.innerHTML = content;
82-
});
83-
}
84-
render() {
85-
return html`
86-
<p id="message">Loading</p>
87-
`;
88-
}
89-
```
9063

91-
You can improve the template by declaring the message as a _reactive property_, and using an expression in the template instead of setting the text imperatively. Declaring a reactive property tells your component to re-render its template when the property changes.
64+
## When templates render
9265

93-
{% playground-example "docs/templates/design" "update-properties.ts" %}
66+
A Lit component renders its template initially when it's added to the DOM on a page. After the initial render, any change to the component's reactive properties triggers an update cycle, re-rendering the component.
9467

95-
For more information, see [Reactive properties](/docs/components/properties/).
68+
Lit batches updates to maximize performance and efficiency. Setting multiple properties at once triggers only one update, performed asynchronously at microtask timing.
9669

97-
## Compose a template from other templates
70+
During an update, only the parts of the DOM that change are re-rendered. Although Lit templates look like string interpolation, Lit parses and creates static HTML once, and then only updates changed values in expressions after that, making updates very efficient.
9871

99-
You can compose Lit templates from other templates. The following example composes a template for an component called `<my-page>` from smaller templates for the page's header, footer, and main content:
72+
For more information about the update cycle, see [What happens when properties change](/docs/components/properties/#when-properties-change).
10073

101-
{% playground-example "docs/templates/compose" "my-page.ts" %}
74+
## DOM encapsulation
10275

103-
You can also compose templates by importing other elements and using them in your template:
76+
Lit uses shadow DOM to encapsulate the DOM a component renders. Shadow DOM lets an element create its own, isolated DOM tree that's separate from the main document tree. It's a core feature of the web components specifications that enables interoperability, style encapsulation, and other benefits.
10477

105-
{% playground-ide "docs/templates/composeimports" %}
78+
For more information about shadow DOM, see [Shadow DOM v1: Self-Contained Web Components
79+
](https://developers.google.com/web/fundamentals/web-components/shadowdom) on Web Fundamentals.
10680

81+
For more information about working with shadow DOM in your component, see [Working with shadow DOM](/docs/components/shadow-dom/).
10782

10883
## See also
10984

@@ -113,4 +88,3 @@ You can also compose templates by importing other elements and using them in you
11388
* [Template expressions](/docs/templates/overview/)
11489

11590

116-

packages/lit-dev-content/site/docs/components/shadow-dom.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ eleventyNavigation:
66
order: 6
77
---
88

9-
Lit components use [shadow DOM](https://developers.google.com/web/fundamentals/web-components/shadowdom) to encapsulate their DOM. Shadow DOM provides a way to add a separate isolated and encapsulated DOM tree to an element. DOM encapsulation is the key to unlocking interoperability with any other code, including other web components or Lit component, functioning on the page.
9+
Lit components use [shadow DOM](https://developers.google.com/web/fundamentals/web-components/shadowdom) to encapsulate their DOM. Shadow DOM provides a way to add a separate isolated and encapsulated DOM tree to an element. DOM encapsulation is the key to unlocking interoperability with any other codeincluding other web components or Lit components—functioning on the page.
1010

1111
Shadow DOM provides three benefits:
1212

0 commit comments

Comments
 (0)