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

refactor(lit): update lit examples with current patterns #268

Merged
merged 4 commits into from
Oct 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion content/2-templating/3-loop/lit/colors-list.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { LitElement, html } from "lit";
import { customElement } from "lit/decorators.js";
import { repeat } from "lit/directives/repeat.js";

@customElement("colors-list")
export class ColorsList extends LitElement {
Expand All @@ -8,7 +9,11 @@ export class ColorsList extends LitElement {
render() {
return html`
<ul>
${this.colors.map((color) => html`<li>${color}</li>`)}
${repeat(
this.colors,
(color) => color,
(color) => html`<li>${color}</li>`
)}
</ul>
`;
}
Expand Down
2 changes: 1 addition & 1 deletion content/4-component-composition/1-props/lit/x-app.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export class XApp extends LitElement {
name="John"
age="20"
.favouriteColors=${["green", "blue", "red"]}
isavailable
isAvailable
></user-profile>`;
}
}
5 changes: 4 additions & 1 deletion content/6-form-input/4-select/lit/color-select.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { LitElement, html } from "lit";
import { customElement, state } from "lit/decorators.js";
import { repeat } from "lit/directives/repeat";

@customElement("color-select")
export class ColorSelect extends LitElement {
Expand All @@ -20,7 +21,9 @@ export class ColorSelect extends LitElement {
render() {
return html`
<select @change=${this.handleChange}>
${this.colors.map(
${repeat(
this.colors,
(color) => color.id,
(color) =>
html`<option
value=${color.id}
Expand Down
56 changes: 18 additions & 38 deletions content/7-webapp-features/2-fetch-data/lit/x-app.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,27 @@
import { Task } from "@lit/task";
import { LitElement, html } from "lit";
import { customElement, state } from "lit/decorators.js";
import { customElement } from "lit/decorators.js";

@customElement("x-app")
export class XApp extends LitElement {
@state()
loading = false;

@state()
error;

@state()
data;

async fetchUsers() {
this.loading = true;
try {
fetchUsers = new Task(this, {
task: async () => {
const response = await fetch("https://randomuser.me/api/?results=3");
const { results: users } = await response.json();
this.data = users;
this.error = undefined;
} catch (err) {
this.data = undefined;
this.error = err;
}
this.loading = false;
}

connectedCallback() {
super.connectedCallback();
this.fetchUsers();
}
if (!response.ok) {
throw new Error(response.status);
}
return response.json();
},
args: () => [],
});

render() {
if (this.loading) {
return html`<p>Fetching users...</p>`;
}
if (this.error) {
return html`<p>An error occurred while fetching users</p>`;
}
if (this.data) {
return html`
return this.fetchUsers.render({
pending: () => html`<p>Fetching users...</p>`,
error: (e) => html`<p>An error occurred while fetching users</p>`,
complete: (data) => html`
<ul>
${this.data.map(
${data.map(
(user) => html`
<li>
<img src=${user.picture.thumbnail} alt="user" />
Expand All @@ -50,7 +30,7 @@ export class XApp extends LitElement {
`
)}
</ul>
`;
}
`,
});
}
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
},
"dependencies": {
"@iconify-json/heroicons": "^1.2.1",
"@lit/task": "^1.0.1",
"classnames": "^2.5.1",
"radix3": "^1.1.2"
},
Expand Down
Loading
Loading