Skip to content

Update Lit TodoMVC to latest Lit and TypeScript #514

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
33 changes: 19 additions & 14 deletions resources/todomvc/architecture-examples/lit/dist/index.js

Large diffs are not rendered by default.

117 changes: 77 additions & 40 deletions resources/todomvc/architecture-examples/lit/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions resources/todomvc/architecture-examples/lit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@
"@rollup/plugin-node-resolve": "^15.1.0",
"@rollup/plugin-terser": "^0.4.3",
"@rollup/plugin-typescript": "^11.1.1",
"@web/dev-server": "^0.2.1",
"rollup": "^2.79.1",
"rollup-plugin-minify-html-literals": "^1.2.6",
"@web/dev-server": "^0.2.1",
"typescript": "^5.0.4",
"wireit": "^0.9.5"
"typescript": "^5.8.3",
"wireit": "^0.14.12"
},
"dependencies": {
"lit": "^3.0.0-pre.0",
"tslib": "^2.5.2"
"lit": "^3.3.0",
"tslib": "^2.8.1"
},
"wireit": {
"serve": {
Expand Down Expand Up @@ -69,7 +69,7 @@
".tsbuildinfo"
],
"clean": false,
"command": "tsc"
"command": "tsc --pretty"
},
"rollup": {
"files": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export class TodoApp extends LitElement {

@updateOnEvent("change")
@state()
readonly todoList = new Todos();
accessor todoList = new Todos();

constructor() {
super();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export class TodoFooter extends LitElement {

@updateOnEvent("change")
@property({ attribute: false })
todoList?: Todos;
accessor todoList: Todos | undefined;

override render() {
if (this.todoList === undefined || this.todoList.all.length === 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,14 @@ export class TodoForm extends LitElement {

@updateOnEvent("change")
@property({ attribute: false })
todoList?: Todos;
accessor todoList: Todos | undefined;

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

@query("input", true) newTodoInput!: HTMLInputElement;
@query("input", true)
accessor newTodoInput!: HTMLInputElement;

#onChange() {
const { value } = this.newTodoInput;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,16 +139,16 @@ export class TodoItem extends LitElement {
];

@property()
todoId = "";
accessor todoId = "";

@property()
text = "";
accessor text = "";

@property({ type: Boolean })
completed = false;
accessor completed = false;

@state()
isEditing: boolean = false;
accessor isEditing: boolean = false;

override render() {
const itemClassList = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export class TodoList extends LitElement {

@updateOnEvent("change")
@property({ attribute: false })
todoList?: Todos;
accessor todoList: Todos | undefined;

override render() {
return html`
Expand Down
42 changes: 24 additions & 18 deletions resources/todomvc/architecture-examples/lit/src/lib/utils.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,35 @@
import type { ReactiveElement } from "lit";

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

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

const { get, set } = descriptor;
const newDescriptor = {
...descriptor,
set(this: ListenerCarryingElement, v: EventTarget) {
const listener = this.__updateOnEventListener ??= () => this.requestUpdate();
const oldValue = get!.call(this);
oldValue?.removeEventListener?.(eventName, listener);
v?.addEventListener?.(eventName, listener);
return set!.call(this, v);
},
return {
get(this: T): V {
return get.call(this);
},
set(this: T, newValue: V): void {
const listener = this.__updateOnEventListener ??= () => this.requestUpdate();
const oldValue = get.call(this);
oldValue?.removeEventListener(eventName, listener);
newValue?.addEventListener(eventName, listener);
set.call(this, newValue);
}
};
Object.defineProperty(target, propertyKey, newDescriptor);
};
};
11 changes: 4 additions & 7 deletions resources/todomvc/architecture-examples/lit/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
"compilerOptions": {
"incremental": true,
"tsBuildInfoFile": "./.tsbuildinfo",
"target": "ES2022",
"module": "ES2022",
"lib": ["DOM", "DOM.Iterable"],
"target": "ES2024",
"module": "NodeNext",
"lib": ["ES2024", "DOM", "DOM.Iterable"],
"rootDir": "./src",
"moduleResolution": "node",
"moduleResolution": "NodeNext",
"sourceMap": true,
"outDir": "./",
"importHelpers": true,
Expand All @@ -21,9 +21,6 @@
"noFallthroughCasesInSwitch": true,
"noImplicitOverride": true,
"skipLibCheck": true,
// Once standard decorators ship, we can remove these two.
"experimentalDecorators": true,
"useDefineForClassFields": false
},
"include": ["src/**/*.ts"],
"exclude": []
Expand Down