Skip to content

Instantly share code, notes, and snippets.

@rudifa
Created March 29, 2020 20:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rudifa/6a3e7e372fddc01f3c44db251e07432a to your computer and use it in GitHub Desktop.
Save rudifa/6a3e7e372fddc01f3c44db251e07432a to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<body>
<todo-app></todo-app>
<script type="module">
import { LitElement, html } from 'https://unpkg.com/lit-element?module';
const author = 'open-wc';
const homepage = 'https://open-wc.org/';
const footerTemplate = html`
<footer>Made with love by <a href="${homepage}">${author}</a></footer>
`;
class TodoApp extends LitElement {
static get properties() {
return {
todos: { type: Array }
};
}
constructor() {
super();
this.todos = [
{ text: 'Do A', finished: true },
{ text: 'Do B', finished: false },
{ text: 'Do C', finished: false },
];
}
render() {
return html`
<h1>Todo app</h1>
<input id="addTodoInput" placeholder="Name" />
<button @click=${this._addTodo}>Add</button>
<ol>
${this.todos.map(
todo => html`
<li>${todo.text} (${todo.finished ? 'Finished' : 'Unfinished'})
<button @click=${() => this._removeTodo(todo)}>X</button>
</li>
`,
)}
</ol>
${footerTemplate}
`;
}
_addTodo() {
const input = this.shadowRoot.getElementById('addTodoInput');
const text = input.value;
input.value = '';
this.todos = [
...this.todos,
{ text, finished: false },
];
}
_removeTodo(todo) {
this.todos = this.todos.filter(e => e !== todo);
}
}
customElements.define('todo-app', TodoApp);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment