-
Notifications
You must be signed in to change notification settings - Fork 206
Update Component overview content #25
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
Changes from all commits
a8ba5f1
f1f8d89
1f1e7fd
352624f
3bd8033
5ac473a
3ffac69
ba5218e
04ecbe6
dd41e20
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<meta http-equiv="X-UA-Compatible" content="ie=edge"> | ||
|
||
<script type="module" src="simple-greeting.js"></script> | ||
<title>Lit code sample</title> | ||
</head> | ||
<body> | ||
<simple-greeting></simple-greeting> | ||
</body> | ||
</html> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"files": { | ||
"simple-greeting.ts": {}, | ||
"index.html": {} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { LitElement, css, html, property, customElement } from 'lit-element'; | ||
|
||
@customElement('simple-greeting') | ||
export class SimpleGreeting extends LitElement { | ||
// Define scoped styles right with your component, in plain CSS | ||
static styles = css` | ||
:host { | ||
color: blue; | ||
} | ||
`; | ||
|
||
// Declare reactive properties | ||
@property() | ||
name?: string = 'World'; | ||
|
||
// Render the UI as a function of component state | ||
render() { | ||
return html`<p>Hello, ${this.name}!</p>`; | ||
} | ||
} |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
--- | ||
title: Overview | ||
eleventyNavigation: | ||
key: Overview | ||
parent: Components | ||
order: 1 | ||
--- | ||
|
||
A Lit component is a reusable piece of UI. You can think of a Lit component as a container that has some state and that displays a UI based on its state. It can also react to user input, fire events—anything you'd expect a UI component to do. And a Lit component is an HTML element, so it has all of the standard element APIs. | ||
|
||
Almost all Lit components include: | ||
|
||
* *Reactive properties* that define the state of the component. Changing one or more of the components' reactive properties triggers an update cycle, re-rendering the component. | ||
|
||
* A *render method* that's called to render the component's contents. In the render method, you define a *template* for the component. | ||
|
||
Lit also provides a set of lifecycle methods that you can override to hook into the component's lifecycle (for example, to run code whenever the component updates). | ||
|
||
To define a Lit component, you create a class that extends the `LitElement` base class. | ||
|
||
Here's a sample component: | ||
|
||
{% playground-example "docs/components/overview/simple-greeting" "simple-greeting.ts" %} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @aomarks is there a way to make this sample sized to fit the contents? I think ideally we wouldn't scroll here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Blocked on #138. We can fix this when it's merged. |
||
|
||
<div class="alert alert-info"> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we have separate low priority and important note styles? These information notes look like they may be more important than other content near them, when it's the opposite. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need to ask design about this. |
||
|
||
**TypeScript or JavaScript?** Unless otherwise noted, samples are in TypeScript, but you can use plain JavaScript with Lit, too. | ||
|
||
</div> | ||
|
||
## A Lit component is an HTML element | ||
|
||
When you define a Lit component, you're defining a [custom HTML element](https://developer.mozilla.org/en-US/docs/Web/Web_Components/Using_custom_elements). So you can use the new element like you'd use any built-in element: | ||
|
||
```html | ||
<simple-greeting name="Markup"></simple-greeting> | ||
``` | ||
|
||
```js | ||
const greeting = document.createElement('simple-greeting'); | ||
greeting.name = 'Script'; | ||
document.body.appendChild(greeting); | ||
``` | ||
|
||
Create a Lit component by creating a class extending LitElement and registering your class with the browser: | ||
|
||
```ts | ||
@customElement('simple-greeting') | ||
export class SimpleGreeting extends LitElement { ... } | ||
``` | ||
|
||
Or in JavaScript: | ||
|
||
```js | ||
export class SimpleGreeting extends LitElement { ... } | ||
customElements.define('simple-greeting', SimpleGreeting); | ||
``` | ||
|
||
The [`@customElement`](/api/modules/_lit_element_.html#customelement) decorator is shorthand for calling [`customElements.define`](https://developer.mozilla.org/en-US/docs/Web/API/CustomElementRegistry/define), which registers a custom element class with the browser and associates it with an element name (in this case, `simple-greeting`). | ||
|
||
The `LitElement` base class is a subclass of `HTMLElement`, so a Lit component inherits all of the standard `HTMLElement` properties and methods. | ||
|
||
Specificially, `LitElement` inherits from `ReactiveElement`, which implements reactive properties, and in turn inherits from `HTMLElement`. | ||
|
||
 | ||
|
||
## Rendering, templates and styles | ||
|
||
A Lit component's `render` method defines a *template* for the component. The template can contain *expressions* that are updated when the component renders: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Regarding There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. re: "The template can contain expressions that are updated when the component renders:" I think the way to describe this is that I think we might want to drop the word "function" in there for the functional people of the world. ie, "The render() method returns a description of the UI as a function of the component's current state. |
||
|
||
```js | ||
render() { | ||
return html`<p>Hello, ${this.name}!</p>`; | ||
} | ||
``` | ||
|
||
By default the rendered DOM is encapsulated using [shadow DOM](/guide/components/shadow-dom/). This has three main benefits: | ||
|
||
* *DOM composition* means that users can provide child nodes without interfering with the component's internal DOM. The component author decides where or if user-provided child nodes render. | ||
|
||
* *DOM encapsulation* means that outside code (like `document.querySelector`) won't accidentally select elements in your component's shadow DOM. | ||
arthurevans marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
* *Style encapsulation* means that you scope styles to your component, and prevent outside CSS styles from leaking in. | ||
|
||
You can specify encapsulated styles for your component using the static `styles` property: | ||
arthurevans marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
```ts | ||
class MyElement extends LitElement { | ||
static styles = css`/* your CSS here */`; | ||
} | ||
``` | ||
|
||
These styles are applied to every instance of your component. | ||
|
||
Read more: | ||
|
||
* [Rendering](/guide/components/rendering/) | ||
* [Template overview](/guide/templates/overview/) | ||
* [Styles](/guide/components/styles/) | ||
|
||
## Reactive properties | ||
|
||
You can define any number of properties on your component as *reactive properties*: | ||
|
||
```ts | ||
class MyElement extends LitElement { | ||
@property() message; | ||
} | ||
arthurevans marked this conversation as resolved.
Show resolved
Hide resolved
|
||
``` | ||
|
||
Changing the value of a reactive property triggers an *update*. (The update process is asynchronous, so setting multiple properties at the same time only triggers one update.) | ||
|
||
During the update, a series of update related callbacks are called. As part of the process, the component's `render()` method is called and its UI updated. | ||
|
||
A reactive property can be set to *reflect* to an attribute. This means a corresponding attribute is set on the component when the property value changes. (Or in the case of a boolean property, the attribute is added when the property is true, and removed when the property is false.) Attribute reflection also happens during the update process. | ||
|
||
In addition to public properties, a component can have internal reactive state: private or protected properties that aren't part of the component's public API, but can still trigger an update. Use the `@state` decorator to declare internal reactive state: | ||
|
||
```ts | ||
class MyElement extends LitElement { | ||
@property() message; | ||
@state() private _language; | ||
} | ||
``` | ||
|
||
Read more: | ||
|
||
* [Reactive properties](/guide/components/properties) | ||
* [Lifecycle](/guide/components/lifecycle) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm wondering if this sentence is necessary just yet. We could get to the example faster without it. And if the update lifecycle is up there in importance with properties and render(), maybe it should have a short section on this page following "Reactive Properties"?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm happy to remove it if you think it's OK.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it makes sense for it to have its own very short section along with "Styling" as I mentioned elsewhere. Then the page outline would look like:
which is kind of a great tweet-sized summary of LitElement on its own
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm holding off on this for now because reorganizing into these buckets is going to require a fairly major rewrite to the section.