File size: 4,700 Bytes
23d8f64 | 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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 | class TodoApp extends HTMLElement {
connectedCallback() {
this.attachShadow({ mode: 'open' });
this.shadowRoot.innerHTML = `
<style>
:host {
display: block;
max-width: 500px;
margin: 0 auto;
font-family: 'Inter', sans-serif;
}
.todo-container {
background: white;
border-radius: 12px;
padding: 24px;
box-shadow: 0 10px 20px rgba(0, 0, 0, 0.1);
}
h2 {
margin-top: 0;
color: var(--dark);
text-align: center;
}
.todo-form {
display: flex;
gap: 10px;
margin-bottom: 20px;
}
.todo-input {
flex: 1;
padding: 12px 16px;
border: 2px solid #e2e8f0;
border-radius: 8px;
font-size: 16px;
outline: none;
transition: border-color 0.3s;
}
.todo-input:focus {
border-color: var(--primary);
}
.add-btn {
background: var(--primary);
color: white;
border: none;
border-radius: 8px;
padding: 0 20px;
font-size: 16px;
font-weight: 600;
cursor: pointer;
transition: all 0.3s;
}
.add-btn:hover {
background: var(--secondary);
transform: translateY(-2px);
}
.todo-list {
list-style: none;
padding: 0;
margin: 0;
}
.todo-item {
display: flex;
align-items: center;
padding: 12px 16px;
background: #f8fafc;
border-radius: 8px;
margin-bottom: 10px;
transition: all 0.3s;
}
.todo-item:hover {
transform: translateX(5px);
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.05);
}
.todo-checkbox {
margin-right: 12px;
cursor: pointer;
}
.todo-text {
flex: 1;
margin: 0;
}
.todo-text.completed {
text-decoration: line-through;
color: #94a3b8;
}
.delete-btn {
background: none;
border: none;
color: #ef4444;
font-size: 18px;
cursor: pointer;
opacity: 0;
transition: opacity 0.3s;
}
.todo-item:hover .delete-btn {
opacity: 1;
}
</style>
<div class="todo-container">
<h2>Todo List</h2>
<form class="todo-form">
<input type="text" class="todo-input" placeholder="Add a new task..." required>
<button type="submit" class="add-btn">Add</button>
</form>
<ul class="todo-list"></ul>
</div>
`;
this.todos = JSON.parse(localStorage.getItem('todos')) || [];
this.renderTodos();
this.shadowRoot.querySelector('.todo-form').addEventListener('submit', (e) => {
e.preventDefault();
const input = this.shadowRoot.querySelector('.todo-input');
if (input.value.trim()) {
this.addTodo(input.value.trim());
input.value = '';
}
});
}
addTodo(text) {
const newTodo = {
id: Date.now(),
text,
completed: false
};
this.todos.push(newTodo);
this.saveTodos();
this.renderTodos();
}
toggleTodo(id) {
this.todos = this.todos.map(todo =>
todo.id === id ? { ...todo, completed: !todo.completed } : todo
);
this.saveTodos();
this.renderTodos();
}
deleteTodo(id) {
this.todos = this.todos.filter(todo => todo.id !== id);
this.saveTodos();
this.renderTodos();
}
saveTodos() {
localStorage.setItem('todos', JSON.stringify(this.todos));
}
renderTodos() {
const todoList = this.shadowRoot.querySelector('.todo-list');
todoList.innerHTML = '';
if (this.todos.length === 0) {
todoList.innerHTML = '<p style="text-align: center; color: #94a3b8;">No tasks yet. Add one above!</p>';
return;
}
this.todos.forEach(todo => {
const li = document.createElement('li');
li.className = 'todo-item';
li.innerHTML = `
<input type="checkbox" class="todo-checkbox" ${todo.completed ? 'checked' : ''}>
<p class="todo-text ${todo.completed ? 'completed' : ''}">${todo.text}</p>
<button class="delete-btn">×</button>
`;
li.querySelector('.todo-checkbox').addEventListener('change', () => this.toggleTodo(todo.id));
li.querySelector('.delete-btn').addEventListener('click', () => this.deleteTodo(todo.id));
todoList.appendChild(li);
});
}
}
customElements.define('todo-app', TodoApp); |