Skip to content

Commit 3f4ded3

Browse files
committed
Update Lit TodoMVC to latest Lit and TypeScript
1 parent 25eb32e commit 3f4ded3

10 files changed

Lines changed: 140 additions & 94 deletions

File tree

resources/todomvc/architecture-examples/lit/dist/index.js

Lines changed: 19 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

resources/todomvc/architecture-examples/lit/package-lock.json

Lines changed: 77 additions & 40 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

resources/todomvc/architecture-examples/lit/package.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@
1818
"@rollup/plugin-node-resolve": "^15.1.0",
1919
"@rollup/plugin-terser": "^0.4.3",
2020
"@rollup/plugin-typescript": "^11.1.1",
21+
"@web/dev-server": "^0.2.1",
2122
"rollup": "^2.79.1",
2223
"rollup-plugin-minify-html-literals": "^1.2.6",
23-
"@web/dev-server": "^0.2.1",
24-
"typescript": "^5.0.4",
25-
"wireit": "^0.9.5"
24+
"typescript": "^5.8.3",
25+
"wireit": "^0.14.12"
2626
},
2727
"dependencies": {
28-
"lit": "^3.0.0-pre.0",
29-
"tslib": "^2.5.2"
28+
"lit": "^3.3.0",
29+
"tslib": "^2.8.1"
3030
},
3131
"wireit": {
3232
"serve": {
@@ -69,7 +69,7 @@
6969
".tsbuildinfo"
7070
],
7171
"clean": false,
72-
"command": "tsc"
72+
"command": "tsc --pretty"
7373
},
7474
"rollup": {
7575
"files": [

resources/todomvc/architecture-examples/lit/src/lib/todo-app.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export class TodoApp extends LitElement {
5252

5353
@updateOnEvent("change")
5454
@state()
55-
readonly todoList = new Todos();
55+
accessor todoList = new Todos();
5656

5757
constructor() {
5858
super();

resources/todomvc/architecture-examples/lit/src/lib/todo-footer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ export class TodoFooter extends LitElement {
8585

8686
@updateOnEvent("change")
8787
@property({ attribute: false })
88-
todoList?: Todos;
88+
accessor todoList: Todos | undefined;
8989

9090
override render() {
9191
if (this.todoList === undefined || this.todoList.all.length === 0)

resources/todomvc/architecture-examples/lit/src/lib/todo-form.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,14 @@ export class TodoForm extends LitElement {
3636

3737
@updateOnEvent("change")
3838
@property({ attribute: false })
39-
todoList?: Todos;
39+
accessor todoList: Todos | undefined;
4040

4141
override render() {
4242
return html`<input @change=${this.#onChange} @keydown=${this.#onKeydown} class="new-todo" autofocus autocomplete="off" placeholder="What needs to be done?" />`;
4343
}
4444

45-
@query("input", true) newTodoInput!: HTMLInputElement;
45+
@query("input", true)
46+
accessor newTodoInput!: HTMLInputElement;
4647

4748
#onChange() {
4849
const { value } = this.newTodoInput;

resources/todomvc/architecture-examples/lit/src/lib/todo-item.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,16 +139,16 @@ export class TodoItem extends LitElement {
139139
];
140140

141141
@property()
142-
todoId = "";
142+
accessor todoId = "";
143143

144144
@property()
145-
text = "";
145+
accessor text = "";
146146

147147
@property({ type: Boolean })
148-
completed = false;
148+
accessor completed = false;
149149

150150
@state()
151-
isEditing: boolean = false;
151+
accessor isEditing: boolean = false;
152152

153153
override render() {
154154
const itemClassList = {

resources/todomvc/architecture-examples/lit/src/lib/todo-list.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ export class TodoList extends LitElement {
8888

8989
@updateOnEvent("change")
9090
@property({ attribute: false })
91-
todoList?: Todos;
91+
accessor todoList: Todos | undefined;
9292

9393
override render() {
9494
return html`
Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,35 @@
11
import type { ReactiveElement } from "lit";
22

33
interface ListenerCarryingElement extends ReactiveElement {
4+
// This property will be used by the decorator to store the event listener.
5+
// It can be dynamically added or explicitly declared by the class using the decorator.
46
__updateOnEventListener?: () => void;
57
}
68

79
/**
8-
* A property decorator that subscribes to an event on the property value and
10+
* An accessor decorator that subscribes to an event on the property value and
911
* calls `requestUpdate` when the event fires.
10-
*
11-
* If we were using this outside of just this one app we'd use the type system
12-
* to enforce that the property value is an `EventTarget`.
12+
*
13+
* The accessor type must be an `EventTarget`.
1314
*/
14-
export const updateOnEvent = (eventName: string) => (target: ListenerCarryingElement, propertyKey: string) => {
15-
const descriptor = Object.getOwnPropertyDescriptor(target, propertyKey)!;
15+
export const updateOnEvent = (eventName: string) =>
16+
function <T extends ListenerCarryingElement, V extends EventTarget | undefined>(
17+
target: ClassAccessorDecoratorTarget<T, V>,
18+
_context: ClassAccessorDecoratorContext<T, V>
19+
) {
20+
const {get, set} = target;
1621

17-
const { get, set } = descriptor;
18-
const newDescriptor = {
19-
...descriptor,
20-
set(this: ListenerCarryingElement, v: EventTarget) {
21-
const listener = this.__updateOnEventListener ??= () => this.requestUpdate();
22-
const oldValue = get!.call(this);
23-
oldValue?.removeEventListener?.(eventName, listener);
24-
v?.addEventListener?.(eventName, listener);
25-
return set!.call(this, v);
26-
},
22+
return {
23+
get(this: T): V {
24+
return get.call(this);
25+
},
26+
set(this: T, newValue: V): void {
27+
const listener = this.__updateOnEventListener ??= () => this.requestUpdate();
28+
const oldValue = get.call(this);
29+
oldValue?.removeEventListener(eventName, listener);
30+
newValue?.addEventListener(eventName, listener);
31+
set.call(this, newValue);
32+
}
2733
};
28-
Object.defineProperty(target, propertyKey, newDescriptor);
29-
};
34+
};
35+

resources/todomvc/architecture-examples/lit/tsconfig.json

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
"compilerOptions": {
33
"incremental": true,
44
"tsBuildInfoFile": "./.tsbuildinfo",
5-
"target": "ES2022",
6-
"module": "ES2022",
7-
"lib": ["DOM", "DOM.Iterable"],
5+
"target": "ES2024",
6+
"module": "NodeNext",
7+
"lib": ["ES2024", "DOM", "DOM.Iterable"],
88
"rootDir": "./src",
9-
"moduleResolution": "node",
9+
"moduleResolution": "NodeNext",
1010
"sourceMap": true,
1111
"outDir": "./",
1212
"importHelpers": true,
@@ -21,9 +21,6 @@
2121
"noFallthroughCasesInSwitch": true,
2222
"noImplicitOverride": true,
2323
"skipLibCheck": true,
24-
// Once standard decorators ship, we can remove these two.
25-
"experimentalDecorators": true,
26-
"useDefineForClassFields": false
2724
},
2825
"include": ["src/**/*.ts"],
2926
"exclude": []

0 commit comments

Comments
 (0)