Skip to content

Commit

Permalink
refactor(lit): update lit examples with current patterns (#268)
Browse files Browse the repository at this point in the history
* fix: 🐛 spelling mistake for prop

* refactor: ♻️ use repeat to map over items

* refactor: ♻️ use lit task for async data fetching

* chore: 🚨 rebuild lock
  • Loading branch information
lloydrichards authored Oct 27, 2024
1 parent 9851bff commit 6267426
Show file tree
Hide file tree
Showing 6 changed files with 4,618 additions and 5,636 deletions.
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

0 comments on commit 6267426

Please sign in to comment.