Skip to content
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

[docs] TypeScript sample on front page with JS in a code editor #491

Merged
merged 12 commits into from
Jan 31, 2019
10 changes: 10 additions & 0 deletions docs/_includes/projects/index-typescript/custom-greeting.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { LitElement, html, property } from 'lit-element';

export class CustomGreeting extends LitElement {
@property({ type: String}) name = 'World';
render() {
return html`<p>Hello, ${this.name}!</p>`;
}
}

customElements.define('custom-greeting', CustomGreeting);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's use the customElement decorator too:

@customElement('custom-greeting')
export class CustomGreeting extends LitElement { 

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you also then have to add the interface HTMLElementTagThingStuff ?

https://github.com/Polymer/lit-element/blob/0093b439fa53e26707a84690d17bf15f54baae3e/src/lib/decorators.ts#L78

Seems noisier

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@katejeffreys No, as in #291 this was changed to only be a string. Before it would actually check if the string was part of HTMLElementTagNameMap. Probably the comment in the source code should also be removed.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we then remove customElements.define('custom-greeting', CustomGreeting);?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@katejeffreys Yes, @customElement('custom-greeting') and customElements.define('custom-greeting', CustomGreeting); do the same thing. Only one is needed.

17 changes: 17 additions & 0 deletions docs/_includes/projects/index-typescript/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!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 src="/node_modules/@webcomponents/webcomponentsjs/custom-elements-es5-adapter.js"></script>
<script src="/node_modules/@webcomponents/webcomponentsjs/webcomponents-bundle.js"></script>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason to use the bundle over the loader?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Easier loading process I think? i forget

<title>lit-element code sample</title>

</head>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indent <head> to the same level as <body>


<body>
<custom-greeting></custom-greeting>
</body>

</html>
1 change: 1 addition & 0 deletions docs/_includes/projects/index-typescript/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import './custom-greeting.ts';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we have a separate file here? We can just import the tag directly into the HTML.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because stackblitz, consistency, & clarity...

  • StackBlitz requires index.ts/js & errors if I import modules from html
  • I want to show the user clear chunks of code rather than large files
  • with multiple elements in an example, writing them all in index.* becomes clunky so I have separate files for each element - for consistency, I do this even if there is only 1 element

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with separate files for elements in most cases (trying to show only how two elements can compose might be easier with two very small elements in one file).

The stackblitz limitations should be gone with the lit-html project template, which will hopefully be usable tomorrow. We can think about migrating to that later...

14 changes: 14 additions & 0 deletions docs/_includes/projects/index-typescript/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"files": [
"custom-greeting.ts",
"index.html",
"index.ts"
],
"title": "lit-element code sample",
"description": "lit-element code sample",
"template": "typescript",
"dependencies": {
"lit-element": "latest",
"@webcomponents/webcomponentsjs": "latest"
}
}
10 changes: 2 additions & 8 deletions docs/_includes/projects/index/custom-greeting.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,16 @@
import { LitElement, html } from 'lit-element';

// Create your custom component
class CustomGreeting extends LitElement {
// Declare properties
static get properties() {
return {
name: { type: String }
};
return { name: { type: String } };
}
// Initialize properties
constructor() {
super();
this.name = 'World';
}
// Define a template
render() {
return html`<p>Hello, ${this.name}!</p>`;
}
}
// Register the element with the browser

customElements.define('custom-greeting', CustomGreeting);
20 changes: 11 additions & 9 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,27 +58,29 @@ Get started
</h1>

<h2>
Define a component in JavaScript:
Define a component
</h2>

<h3 style="margin: 12px 0 0 0;">
custom-greeting.js
</h3>
It's easy to define a Web Component with LitElement:

```js
{% include projects/index/custom-greeting.js %}
{% include projects/index-typescript/custom-greeting.ts %}
```

<h2 style="margin-top: 40px;">Include the component in your web page:</h2>
<h2 style="margin-top: 40px;">Use a component</h2>

<h3 style="margin: 12px 0 0 0;">
index.html
</h3>
Then use it anywhere you use HTML:

```html
<custom-greeting></custom-greeting>
```

Click **Launch code editor** to see a live sample.

{% include project.html folder="index-typescript" openFile="custom-greeting.js" %}

See the same sample with JavaScript instead of TypeScript:

{% include project.html folder="index" openFile="custom-greeting.js" %}

</div>
Expand Down