-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
67 lines (62 loc) · 1.85 KB
/
Copy pathmain.js
File metadata and controls
67 lines (62 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import { Async, component, html } from "../../src/index.js";
const catalog = [
{
id: "keyboard",
title: "Keyboard",
description: "A compact mechanical keyboard."
},
{
id: "trackpad",
title: "Trackpad",
description: "A wireless multi-touch trackpad."
},
{
id: "display",
title: "Display",
description: "A color-accurate desktop display."
}
];
const CatalogApp = component(function CatalogApp() {
const selectedId = this.signal("selectedId", catalog[0].id);
const selectedTitle = this.computed("selectedTitle", () => {
return catalog.find((item) => item.id === selectedId.value)?.title ?? "";
});
const selectedDescription = this.computed("selectedDescription", () => {
return catalog.find((item) => item.id === selectedId.value)?.description ?? "";
});
const select = this.handler("select", function ({ element }) {
selectedId.set(element.dataset.catalogId);
});
const choices = catalog.map((item) => {
const selected = this.computed(`selected.${item.id}`, () => selectedId.value === item.id);
return html`
<button
type="button"
data-catalog-id="${item.id}"
on:click="${select}"
class:selected="${selected.id}"
signal:attr:aria-pressed="${selected.id}"
>
${item.title}
</button>
`;
});
return html`
<section aria-labelledby="catalog-title">
<h1 id="catalog-title">Catalog</h1>
<nav aria-label="Products">${choices}</nav>
<article aria-live="polite">
<h2 signal:text="${selectedTitle.id}"></h2>
<p signal:text="${selectedDescription.id}"></p>
</article>
</section>
`;
});
Async.use({ component: { CatalogApp } });
Async.start({
root: document,
router: false,
onError({ error, diagnostic }) {
console.error(`[async:${diagnostic.code}] ${diagnostic.message}`, error);
}
});