arefamer's picture
ุงู†ุดู‰ ู„ูŠ ุชุทุจูŠู‚ ุงู„ู‡ูŠูƒู„ ุฎุฑุงููŠ html css java
23d8f64 verified
Raw History Blame Contribute Delete
4.7 kB
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);