You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: packages/lit-dev-content/site/docs/components/rendering.md
+39-65Lines changed: 39 additions & 65 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,102 +8,77 @@ eleventyNavigation:
8
8
9
9
Add a template to your component to define what it should render. Templates can include _expressions_, which are placeholders for dynamic content.
10
10
11
-
```ts
12
-
render() {
13
-
returnhtml`<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:
22
12
23
-
For more information about the update cycle, see [What happens when properties change](/docs/components/properties/#update-cycle).
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.
26
16
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.
28
18
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:
34
20
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.
37
25
38
-
<divclass="alert alert-info">
26
+
For more information about writing templates, see [Templates](/docs/templates/overview/).
39
27
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
41
29
42
-
</div>
30
+
To take best advantage of Lit's functional rendering model, your `render()` method should follow these guidelines:
43
31
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.
45
36
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.
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.
49
40
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.
51
42
52
-
* Tag your template literal with the [`html`](TODO_HREF) tag function.
43
+
For more information, see [Reactive properties](/docs/components/properties/).
53
44
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
55
46
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:
57
48
58
-
For more information, see [Templates overview](/docs/templates/overview/).
In this example, the individual templates are defined as instance methods, so a subclass could extend this component and override one or more templates.
61
52
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 %}
63
54
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.
65
56
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 %}
70
58
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:
72
60
73
-
The following code manipulates the rendered DOM. This is usually an anti-pattern:
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.
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.
94
67
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.
96
69
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.
98
71
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).
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.
Copy file name to clipboardExpand all lines: packages/lit-dev-content/site/docs/components/shadow-dom.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,7 +6,7 @@ eleventyNavigation:
6
6
order: 6
7
7
---
8
8
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 code—including other web components or Lit components—functioning on the page.
0 commit comments