Created
April 4, 2024 01:50
-
-
Save djalmajr/8a9984d043d7e7bdc9810c4638890062 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { html, render } from 'https://cdn.skypack.dev/uhtml@3'; | |
import { effect, signal } from 'https://cdn.skypack.dev/[email protected]'; | |
const todos = signal([]); | |
const add = () => { | |
const uuid = crypto.randomUUID(); | |
const text = `Item ${uuid.split('-')[1]}`; | |
todos.value = todos.value.concat({ uuid, text }); | |
}; | |
const remove = (todo) => { | |
todos.value = todos.value.filter(t => t !== todo); | |
}; | |
const Todo = (todo) => { | |
return html`<li @click=${() => remove(todo)}>${todo.text}</li>`; | |
}; | |
const Todos = () => html` | |
<div class="app"> | |
<h1>Items (${todos.value.length})</h1> | |
<button @click=${add}>Add Item</button> | |
<ul>${todos.value.map(Todo)}</ul> | |
</div> | |
`; | |
const App = () => html`${Todos()}`; | |
effect(() => render(document.querySelector('#r00t'), App)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment